【问题标题】:Run Event after button get pressed按下按钮后运行事件
【发布时间】:2014-03-04 12:49:02
【问题描述】:

我有一个code,它从文件中读取信息并将它们显示给用户....现在我想在向用户显示信息后STOP我的codeWAIT 用于buttonpress 启动我的event,因为该按钮清除了textbox 并返回一些信息给用户/管理员...

但我不知道如何阻止我的代码运行并等待button 被按下...

非常感谢

StringBuilder strbuildsection = new StringBuilder();
        StringBuilder strbuildbetreff = new StringBuilder();
        StringBuilder strbuildinhalt = new StringBuilder();
        StringBuilder strbuilduser = new StringBuilder(System.Environment.UserName);
        StringBuilder strbuildusercheck = new StringBuilder();


        foreach (string Ccat in this.ini.IniGetCategories())
        {
            string readval = ini.IniReadValue(Ccat, "Read");
            string usercheckvar = (this.ini.IniReadValue(Ccat, "SpecifyUser"));
            string user = System.Environment.UserName;

            if (readval == "0")
            {
                if (usercheckvar == user || usercheckvar.Equals("All"))
                {
                    strbuildsection.Append(Ccat + Environment.NewLine);
                    foreach (string cat in this.ini.IniGetKeys(Ccat))
                    {
                        strbuildinhalt.Clear();
                        strbuildusercheck.Clear();
                        strbuildbetreff.Clear();

                        strbuildbetreff.Append(this.ini.IniReadValue(Ccat, "Betreff") + Environment.NewLine);
                        strbuildinhalt.Append(this.ini.IniReadValue(Ccat, "Inhalt") + Environment.NewLine);
                    }

                    textBox1.AppendText(strbuildsection.ToString() + strbuildbetreff.ToString() + strbuildinhalt.ToString() + strbuildusercheck.ToString() + Environment.NewLine);
                        strbuildsection.Clear();
                // HERE  I want to stop my process and wait until the User
                // press the button and start my event
                // but i don't know how to do this
                // After this the loop continue and so on
            }

private void BT_Bestaetigung_Click(object sender, EventArgs e)
{
  CODE
}

所以如果button 被按下,我想开始我的“事件”,如果没有,代码应该等待这个

【问题讨论】:

  • 欢迎来到stackoverflow。请在提问之前表现出你的挣扎。
  • 我该怎么办?我的英语不太好...@ToanVo
  • 英文没问题。向我们展示您的问题所在以及您当前的代码。
  • 你能给我们一些代码吗?
  • @brothers28 我在我的问题中添加了一些代码...

标签: c# winforms events loops button


【解决方案1】:
 Random rnd = new Random();
            bool stop = false;
            Thread thread = null;

            btnShow.Click += (source, e) =>
            {
                stop = true;
                MessageBox.Show("Show something");
            };

            btnClose.Click += (source, e) =>
            {
                stop = false;
            };

            if (thread == null)
            {
                thread = new Thread(() =>
                {
                    while (true) // your working loop
                    {
                        while (stop)
                        {}

                        // action of your loop 
                        Console.WriteLine(rnd.Next());

                    }
                });
                thread.Start();
           }

【讨论】:

  • btn1 是按钮的名称。试试看。
  • 这实际上不会停止循环运行。
  • 没错,所以在循环中实现一个while(textboxshown)。
【解决方案2】:

您似乎将代码放置在向用户显示的表单中,这会阻止您停下来等待用户响应,因为您仍在循环中。

解决方案是使用单独的模态对话框:

创建一个单独的Form,您在循环中构建它并在必要时将其显示给用户 - 等待表单关闭 - 处理结果并重复直到完成。

在这个新表单中,您可以放置​​用户需要与之交互的控件和按钮,并在向他展示之前填充它们。

        frmConfirmationDialog myConfirmationDialog = new frmConfirmationDialog()

        //Fill in information to show to the user
        myConfirmationDialog.textBox1.AppendText(strbuildsection.ToString() + strbuildbetreff.ToString() + strbuildinhalt.ToString() + strbuildusercheck.ToString() + Environment.NewLine);

        //Open Form modally (this will stop your loop until the dialog is closed)
        DialogResult myResult = myConfirmationDialog.ShowDialog();

        if (myResult == System.Windows.Forms.DialogResult.OK)
        {
            //Do Stuff here
        }
        else //catch further type of results here (you could also work with a switch statement
        {
            //Do Stuff here
        }

顺便说一句,要在关闭表单时获取 DialogResult,请将“确认”或“取消”按钮的 DialogResult 属性设置为您喜欢的值。这将导致模态表单使用按钮的 DialogResult 自动关闭。如果您需要在关闭表单之前捕获内容,您可以为 FormClosing 实现 EventHandler 或处理按钮的 Click 事件。

【讨论】:

  • 你有代码示例吗?
  • @Maewie:我添加了一个计时器来每次(每次)读取文件,但现在新的对话框经常显示并且停止计时器并在最后开始不工作......
  • 请在新问题中更详细地描述您的问题。
【解决方案3】:

你必须这样想:你有一个连续操作,你正在等待用户交互。

在 winforms 应用程序中,您无法在 UI 线程中执行连续操作(因为您的窗口需要不断接收来自操作系统的消息,即用户输入或重绘请求并且必须对它们做出反应,否则您将有 滞后 甚至冻结 UI),典型的解决方案是:

  • 将操作拆分为子操作(简短且不会导致 UI 滞后);
  • 将操作放入线程中。

所以,基本上,走这条路(有一种模式可以避免在委托步骤中使用 switch,但为简单起见,我们展示了经典方式):

_timer = new Timer();
_timer.Tick += timer_Tick;
...
// in timer tick
switch(_operation)
{
    case operation1:
        _operation = nextoperation; // _operation++
        ...
        break;
    case operation2:
        // do nothing, wait for button press
        if(_buttonPressed)
            _operation = nextoperation;
        break;
    case operation3:
        // continue after button press
        ..
    ...
}

或者这样

_thread = new Thread(...)
thread.Start();
...
// in thread
// start operation
...
// wait for button press
while(_buttonPressed)
    Thread.Sleep(0);
// continue operation
...

在这两种情况下

// in button click event
_buttonPressed = true;

【讨论】:

    猜你喜欢
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多