简体   繁体   English

可见窗体后Button.Text更改

[英]Button.Text is changing after Form is visible

I have a button that loads a Form, as it gets lots of data from a database and takes a few seconds, I want to advise the user to wait. 我有一个加载表格的按钮,因为它从数据库中获取大量数据并花费几秒钟,我想建议用户等待。

When I click the button the button text does not change. 当我单击按钮时,按钮文本不会更改。

This is the button Click code I am using: 这是我正在使用的按钮Click代码:

private void btnItemConfigForm_Click(object sender, EventArgs e)
            {
                var itemConfigBtnText = btnItemConfigForm.Text;
                btnItemConfigForm.Text = "Waiting...";

                ItemConfigForm form = new ItemConfigForm();
                form.Show();

                if (form.Created)
                {
                    btnItemConfigForm.Text = itemConfigBtnText;
                }
            }

If I Comment out 如果我注释掉

if (form.Created)
{
    btnItemConfigForm.Text = itemConfigBtnText;
}

Then the button text changes to waiting after the new form window is visible. 然后,在新表单窗口可见之后 ,按钮文本将变为等待。

What am I missing to get the button text to change before the form window is visible. 我缺少在窗体窗口可见之前更改按钮文本的功能。

the simple solution is to add this row: 简单的解决方案是添加以下行:

btnItemConfigForm.Refresh();

after this row 在这一行之后

btnItemConfigForm.Text = "Waiting...";

Otherwise the text of the button will be changed only when the function ends, this function will redraw the form display! 否则,仅当函数结束时,按钮的文本才会更改,该函数将重绘表单显示!

Ps If you want the form will not be blocked - you can use in asynchronic running to the function "Show" (or New) then you will need Event to notify the first form when the form will be loaded ps如果您希望表单不会被阻止-您可以异步运行到函数“显示”(或新建),那么当表单加载时,您将需要Event来通知第一个表单

sorry for my English... :) 对不起我的英语不好... :)

Added 添加

  btnItemConfigForm.Update();

under

var itemConfigBtnText = btnItemConfigForm.Text;
btnItemConfigForm.Text = "Waiting...";

This updates the button Control before moving on to initialising and showing the form. 在继续初始化和显示表单之前,这将更新“控件”按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM