简体   繁体   English

为什么在WinForm中的Visual C#.NET中,除非选择了tabPage1,否则tabPage1不会更新控件内容的更改?

[英]Why in Visual C# .NET in WinForm, tabPage1 will not update control content changes unless tabPage1 is selected?

So in a basic WinForm in Visual C# .NET I have tabControl1 with two pages: tabPage1 and tabPage2. 因此,在Visual C#.NET的基本WinForm中,我具有带有两个页面的tabControl1:tabPage1和tabPage2。

Let's say i have label1 in tabPage1, and a timer which updates every 10 seconds and changes the label1 to the current time: 假设我在tabPage1中有label1,并且有一个计时器,它每10秒更新一次并将label1更改为当前时间:

private void timer1_Tick(object sender, EventArgs e)
{
    button1.PerformClick();
}
private void button1_Click(object sender, EventArgs e)
{
     label1.Text = "Last updated: " + DateTime.Now.ToString() + "(local time)";
}

Now, if I have tabPage1 selected, the label1.Text updates just fine, even if i have the application minimized. 现在,如果我选择了tabPage1,即使我已将应用程序最小化,label1.Text的更新也很好。 However, if tabPage2 is selected, then label1.Text never updates until I select the tabPage1 which contains the label1. 但是,如果选择了tabPage2,则label1.Text永远不会更新,直到我选择包含label1的tabPage1。

Since in my application I have multiple tabs, which need to update content from the internet and populate labels and listviews, I need it to be able to update content on the tabs without me selecting. 由于在我的应用程序中,我有多个选项卡,它们需要从Internet更新内容并填充标签和列表视图,因此我需要它能够更新选项卡上的内容而无需我进行选择。

How would I go about doing this, without programmatically forcing the user to select the tab to update content, which would disturb the user if they were using a different tab? 在不以编程方式强制用户选择选项卡来更新内容的情况下(如果使用其他选项卡时会打扰用户),我将如何进行?

Note: I am trying to avoid this cludgy solution (which does work, but is very annoying to the user): 注意:我正在尝试避免使用这种笨拙的解决方案(它确实有效,但对用户而言却很烦人):

private void timer1_Tick(object sender, EventArgs e)
{
    int x = tabControl1.SelectedIndex;
    if (tabControl1.SelectedIndex != 0)
    {
        tabControl1.SelectedIndex = 0;
        label1.Text = "Last updated: " + DateTime.Now.ToString() + "(local time)";
        tabControl1.SelectedIndex = x;
    }
}

Edit: I have tried tabPage1.Update(); 编辑:我尝试了tabPage1.Update(); and it does not work, still does not update the time so when I select tabPage1 after it should have updated a couple times, it shows the same time as before. 并且它不起作用,仍然没有更新时间,因此当我选择tabPage1应该更新两次之后,它显示的时间与以前相同。

Edit2: I left out information critical to the problem. Edit2:我遗漏了对该问题至关重要的信息。 in timer1 I was doing: button1.PerformClick(); 在timer1中,我正在这样做:button1.PerformClick(); However, PerformClick(); 但是,PerformClick(); will not work if the button is hidden. 如果按钮隐藏,将不起作用。 So when I'm in tabPage2 it is not properly clicking the button because the button is hidden in tabPage1. 因此,当我位于tabPage2中时,由于按钮隐藏在tabPage1中,因此无法正确单击该按钮。 So the solution is to take the button code, put it into a method, and instead use the timer to do the method rather than PerformClick(), then all works fine no matter you are in tabPage1 or not. 因此,解决方案是将按钮代码放入方法中,然后使用计时器而不是PerformClick()来执行该方法,无论您是否在tabPage1中,一切都可以正常工作。

Along with the code that makes the changes, try calling tabPage1.Update() . 连同进行更改的代码,尝试调用tabPage1.Update() Maybe it is not updating because it isn't visible. 也许它没有更新,因为它不可见。 Either way, when the user changes to the tab to see its content it will probably call the method above and display its content updated. 无论哪种方式,当用户更改选项卡以查看其内容时,它都可能会调用上述方法并显示其内容已更新。 So it's not really necessary to update it if it is in the background. 因此,如果它在后台,则不必真正更新它。 I cannot test it myself as I no longer have visual studio :( 我无法自己测试它,因为我不再有Visual Studio了:(

看到问题中的编辑,它不起作用的原因是因为我使用了button.PerformClick()而不是正常的方法调用。

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

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