请帮我我的朋友们 [英] please help me my friends

查看:60
本文介绍了请帮我我的朋友们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何用C#中的按钮做两项不同的事情

how can I do two different thing with a button in C#

推荐答案

告诉你想做什么.一种简单的解决方案是在单击按钮时设置标志变量,然后在下次检查此值并执行所需的操作.

假设您有一个带有Text ="Save"和ID ="btnSave"的按钮.现在,在单击事件"中,您可以更改文本,然后使用相同的按钮进行更新.这是代码

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

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




如果您现在明白我的意思,则可以扩展此行为并在同一按钮上执行多项操作.使用ViewState在每个click事件上保存一些变量值,然后调用相应的函数.
谢谢




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


您不知道要做什么,但是您可以从button事件中异步调用委托方法.因此,您可以执行多个操作.例如;

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;

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);
}


您可以按照解决方案4中所述进行操作.有关更多信息,如果您使用的是ToolStripButton,还可以执行其他操作.有一个称为CeckOnClick的属性.如果将其设置为true,则您的按钮的行为类似于在字处理器中用于加粗或取消加粗文本的那些按钮.当用户按下按钮时,按钮保持在按下状态.再次单击该按钮将释放到其初始位置.在按钮的Click事件下,可以从对象的Checked属性检查状态,还可以使用条件语句进行相应的编程.
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.


这篇关于请帮我我的朋友们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆