TabControl.SelectedIndex 被更改,但 SelectedIndexChanged 甚至没有触发

发布于 2024-10-08 20:03:04 字数 1521 浏览 2 评论 0原文

所有,

我在应用程序中有一个 TabControl 开始表现奇怪。一些背景...

该程序从 VB6 转换为 VB .NET 2008,并用于使用类名来引用表单。换句话说,我可能有一个名为 frmFoo 的表单类。在程序的代码中,您可能会看到:

frmFoo.Show()

或者

frmFoo.UserDefinedProperty = True

在最近的一些更改期间,我创建了变量来表示表单的实例,就像这样:

Public MyForm as frmFoo

MyForm = New frmFoo
MyForm.Show()

在这样做时,我还从表单的 Load 事件处理程序中删除了代码,并将其放入表单的构造函数中。

当加载表单或加载文档并影响 TabControl 的选定索引时,类似以下内容不一定会触发 SelectedIndexChanged 事件。

MyForm.tbsForm.SelectedIndex = ValueReadFromFile

...或者...

MyForm.tbsForm.Tabs(ValueReadFromFile).Select

抱歉说得这么啰嗦,但还有更多。如果我打开表单并查看 TabControl 以验证其设置是否正确,则一切都会按预期进行。行为不当的 TabControl 包含在另一个 TabControl 中,因此我必须单击父 TabControl 才能看到它。如果我可以看到它并运行测试,那么测试总是有效的。如果我看不到它并运行测试,我运行的第一个测试将不会触发该事件。 ...寻呼海森堡博士...

这几乎就像必须首先通过更改值或使其在屏幕上可见来初始化控件一样...我完全迷失了这个。这是我见过的最不寻常的行为。在我开始使用变量来表示表单并将 Load 事件代码放入表单构造函数之前,一切都运行良好。

谁能帮助我,或者至少让我摆脱痛苦? SH

------------------------------------------------- ------------- 编辑#2
我刚刚在尝试消除行为中的一些可变性后进行了测试。但我想确认之前所说的行为。

我打开程序并读取一个文件。该文件包含一个应该触发事件处理程序的值。在不使控件可见的情况下,我可以更改选项卡控件的 SelectedIndex 属性,而无需触发事件。

我再次关闭该程序,然后重新打开它。这一次,选择了允许子选项卡(我关注其事件的选项卡)变得可见的父选项卡。然后,我在父控件中选择了另一个选项卡,这意味着子控件不再可见。当我打开与以前相同的文件时,它触发了该事件。

我很想实现一个标志来确认控件已被重新绘制或父选项卡是否已显示。如果未设置标志,我可能必须在代码中触发该事件。

我想重申,当程序通过类名引用表单并且表单上的大部分控件排列都是在加载事件中完成时,一切都有效。现在,程序创建变量,并在窗体的构造函数中完成控件的排列。我确信这与我遇到的问题有关,但我不明白如何。有什么智慧可以分享吗?

All,

I have a TabControl in an application that started behaving strangely. Some background...

This program was converted from VB6 to VB .NET 2008, and used to refer to forms using their class names. In other words, I might have a form class called frmFoo. In the code for the program you might see:

frmFoo.Show()

or

frmFoo.UserDefinedProperty = True

During some recent changes, I created variables to represent instances of my forms much like these:

Public MyForm as frmFoo

MyForm = New frmFoo
MyForm.Show()

In doing so, I also removed code from the form's Load event handler and put it in the form's constructor.

When the form loads, or when a document is loaded and should influence the TabControl's selected index, something like the following will not necessarily fire the SelectedIndexChanged event.

MyForm.tbsForm.SelectedIndex = ValueReadFromFile

...or...

MyForm.tbsForm.Tabs(ValueReadFromFile).Select

Sorry to be so wordy, but there's more. If I open the form and look at the TabControl to verify that it's been set properly, everything works like it's supposed to. The misbehaving TabControl is contained within another TabControl, so I have to click the parent TabControl to see it. If I can see it, and run a test, the test always works. If I can't see it, and run a test, the first test I run will not fire the event. ...paging Dr. Heisenberg...

It's almost as if the control has to be initialized first by changing the value or making it visible onscreen...I'm totally lost on this one. It's the most unusual behavior I've ever seen. And everything worked perfectly before I began using variables to represent forms and placed the Load event code into the form constructors.

Can anyone help, or at least put me out of my misery?
SH

-------------------------------------------------------------- Edit #2
I just performed a test after having attempted to eliminate some of the variability in the behavior. But I wanted to confirm the previously-stated behavior.

I opened the program and read a file. This file contained a value that should have triggered the event handler. Without making the control visible, I can change the SelectedIndex property of the tab control without the event firing.

I closed the program down again, and reopened it. This time, selected the parent tab that allowed the child tab (the one whose event I'm concerned with) to become visible. I then selected a different tab in the parent control, meaning that the child control was no longer visible. When I opened the same file as before, it fired the event.

I'm tempted to implement a flag that confirms that the control has been repainted or whether the parent tab has been displayed. I may have to fire the event in code if the flag isn't set.

I want to reiterate that everything worked when the program referred to the forms by their class names and much of the arrangement of controls on the forms was done in the load event. Now the program creates variables and the arrangement of the controls is done in the form's constructor. I'm sure this has something to do with the problem I'm having, but I can't understand how. Any wisdom to share?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的呼吸 2024-10-15 20:03:04
MyForm.tbsForm.SelectedIndexChanged = ValueReadFromFile

没有多大意义。是否尝试将处理程序分配给 SelectedIndexChanged 事件?或者 ValueReadFromFile 是选项卡的名称?

您所说的是,您有两个选项卡控件,例如 A 和 B。选项卡控件 B 包含在 A 的选项卡中,除非 A 选择了包含选项卡控件 B 的选项卡页,否则 B 的 SelectedIndexChanged 事件如果您以编程方式更改其选项卡,则不会触发?

您尝试以哪些不同的方式在子选项卡控件中选择选项卡,以及何时执行此代码?

MyForm.tbsForm.SelectedIndexChanged = ValueReadFromFile

doesn't make a lot of sense. Is tha trying to assign a handler to the SelectedIndexChanged event? or is ValueReadFromFile the name of the tab?

What you're saying is that you have two tab controls, say, A and B. Tab control B is contained within a tab of A, and unless A has the tab page selected that contains the tab control B, the SelectedIndexChanged event of B will not fire if you change its tab programatically?

In which different ways have you tried to select a tab within the child tab control, and when is this code being executed?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文