C# Checking if button was clicked

前端 未结 4 1903
情书的邮戳
情书的邮戳 2020-12-30 12:03

I am making a program that should just continue if 2 conditions are given.

The first one, 2 TextBoxs have the same word in and a Button wa

相关标签:
4条回答
  • 2020-12-30 12:14

    These helped me a lot: I wanted to save values from my gridview, and it was reloading my gridview /overriding my new values, as i have IsPostBack inside my PageLoad.

    if (HttpContext.Current.Request["MYCLICKEDBUTTONID"] == null)
    {
       //Do not reload the gridview.
    
    }
    else
    {
       reload my gridview.
    }
    

    SOURCE: http://bytes.com/topic/asp-net/answers/312809-please-help-how-identify-button-clicked

    0 讨论(0)
  • 2020-12-30 12:24

    Click is an event that fires immediately after you release the mouse button. So if you want to check in the handler for button2.Click if button1 was clicked before, all you could do is have a handler for button1.Click which sets a bool flag of your own making to true.

    private bool button1WasClicked = false;
    
    private void button1_Click(object sender, EventArgs e)
    {
        button1WasClicked = true;
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
        if (textBox2.Text == textBox3.Text && button1WasClicked)
        { 
            StreamWriter myWriter = File.CreateText(@"c:\Program Files\text.txt");
            myWriter.WriteLine(textBox1.Text);
            myWriter.WriteLine(textBox2.Text);
            button1WasClicked = false;
        }
    }
    
    0 讨论(0)
  • 2020-12-30 12:32

    button1, button2 and button3 have same even handler

    private void button1_Click(Object sender, EventArgs e)
        {
            Button btnSender = (Button)sender;
            if (btnSender == button1 || btnSender == button2)
            {
                //some code here
            }
            else if (btnSender == button3)
                //some code here
        }
    
    0 讨论(0)
  • 2020-12-30 12:33

    i am very new to this website. I am an undergraduate student, doing my Bachelor Of Computer Application. I am doing a simple program in Visual Studio using C# and I came across the same problem, how to check whether a button is clicked? I wanted to do this,

    if(-button1 is clicked-) then
    {
    this should happen;
    }
    if(-button2 is clicked-) then
    {
    this should happen;
    }
    

    I didn't know what to do, so I tried searching for the solution in the internet. I got many solutions which didn't help me. So, I tried something on my own and did this,

    int i;
    private void button1_Click(object sender, EventArgs e)
            {
                i = 1;
                label3.Text = "Principle";
                label4.Text = "Rate";
                label5.Text = "Time";
                label6.Text = "Simple Interest";
            }
    
    
    private void button2_Click(object sender, EventArgs e)
            {
                i = 2;
                label3.Text = "SI";
                label4.Text = "Rate";
                label5.Text = "Time";
                label6.Text = "Principle";
            }
    private void button5_Click(object sender, EventArgs e)
            {
    
                try
                {
                    if (i == 1)
                    {
                        si = (Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox2.Text) * Convert.ToInt32(textBox3.Text)) / 100;
                        textBox4.Text = Convert.ToString(si);
                    }
                    if (i == 2)
                    {
                        p = (Convert.ToInt32(textBox1.Text) * 100) / (Convert.ToInt32(textBox2.Text) * Convert.ToInt32(textBox3.Text));
                        textBox4.Text = Convert.ToString(p);
                    }
    

    I declared a variable "i" and assigned it with different values in different buttons and checked the value of i in the if function. It worked. Give your suggestions if any. Thank you.

    0 讨论(0)
提交回复
热议问题