Click here to Skip to main content
15,881,876 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
how can I do two different thing with a button in C#
Posted
Comments
veenusethi 20-Oct-12 6:12am    
Please Explain Your Question clearly?
Saraniraj 20-Oct-12 6:16am    
Explain question clearly
Yatin chauhan 20-Oct-12 9:08am    
Can You Explain more about your Problem..So we can help you...!!!
Shahriar Iqbal Chowdhury/Galib 20-Oct-12 14:31pm    
did u tried anything?

You can do completely two different things when have something to tell what you want to do. One simple solution is to set a flag variable on button click and for the next time check this value and do what ever you need.

Let us say you have a button with Text ="Save" and ID = "btnSave". Now On Click Event of this, you can change the Text and then Update using the same button. here is the code

C#
protected void btnSave_Click(object sender, EventArgs e)
{    
   if(btnSave.Text == "Save")
   {
      btnSave.Text = "Update";
      SaveData();
   }
   else
   {
      btnSave.Text = "Save";
      UpdateData();
   }
}



If you are getting my point now, you can extend this behavior and do multiple thing on the same button. Use ViewState to save some variable value on each click event and call the appropriate function.
Thanks
 
Share this answer
 
What do you want to do, I don't know, but you can call a delegate method asynchronously from the button event. So you can do more than one operation. For example;

C#
private delegate string LongTaskWorkHandler(string value1, string value2);

private void button1_click(object sender, EventArgs e)
{
    LongTaskWorkHandler longTaskDelegate = DoLongTask;
    IAsyncResult ar = longTaskDelegate.BeginInvoke("THE", "CODEPROJECT", null, null);
    while(true)
    {
        Console.Write(".");
        if(ar.AsyncWaitHandle.WaitOne(50, false))
        {
            Console.WriteLine("Can get the result now.");
            break;
        }
    }
    // Waits until the delegate has completed its work.
    string result = longTaskDelegate.EndInvoke(ar);
    MessageBox.Show(result);
}

// running in a background thread.
private string DoLongTask(string value1, string value2)
{
    string output = null;
    try
    {
        output = LongTask(value1, value2);
    }
    catch (Exception ex)
    {
        output = ex.Message;
    }
    return output;
}

private string LongTask(string value1, string value2)
{
    Console.WriteLine("LongTask started.");
    Thread.Sleep(5000);
    Console.WriteLine("LongTask completed.");
    return String.Format("{0} {1}", value1, value2);
}
 
Share this answer
 
You can do as stated in Solution 4. For your further information you can do something else too if you are using a ToolStripButton. There is a property known as CeckOnClick. If turn this true your button will behave similar to those buttons which you use to bold or unbold your text in a wordprocessor. When the user presses the button once the button stays in a pressed state. After another click the button is released into its initial position. Under Click event of the button you can check the state from the Checked property of the object and you can program accordingly using condition statements.
 
Share this answer
 
Actually the Dot.NET framework is giving you a great tool if you want to react in many different ways to the same event. Actually the event concept in .net is the implementation of a design pattern, that allows you to add multiple event handlers to the same event. Here is a comprehensive example: http://www.dotnetperls.com/event[^]. In the case of a control event, the runtime will fire the event, but every subscribed handler will be called. Your task is just to add a second or a third handler to the same button click event for example. The best of it is, that these handlers are independent. But be aware of thread-safety if that's involved.
 
Share this answer
 
salam amirmohammad jan,age lotf koni code sho vasam mifrestti? kheily mamnoon.
 
Share this answer
 
many things u can do in a button as for example save user registration data into database and sent the confirmation mail to the user, select data from one table and store it another table....................so on.......many things u can do a single button.....for more clarification u can put your question here...............
 
Share this answer
 
well can you send me an example for using only a button-onclick to add data two different forms,thanks a lot.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900