我正在开发一个C#windows窗体应用程序,我在其中实现TabControl
对象和MenuStrip
。某些ToolStripMenuItems的CheckStates特定于每个单独的选项卡。选项卡在运行时动态生成。我希望每次用户切换到新选项卡时能够加载/设置适当的CheckState
ToolStripMenuItems。
在我的代码中,我在SelectedIndexChanged
设置了一个TabControl
事件处理程序,以适当地切换ToolStripMenuItems的CheckStates。当我调试时,事件处理程序按预期触发,并且菜单项的CheckStates的运行时值完全按照它们应该更改为“已选中”或“未选中”。但是,Windows窗体上的菜单项永远不会显示为已选中。功能如下:
public void Update_Real_Checkstates_on_Tab_Change(Object sender, EventArgs args)
{
//if a config has not yet been loaded on this tab, return
if (G_Tab.Active_CFG_Names[G_Tab.myTabCtrl.SelectedIndex] == "") return;
//uncheck all the items
foreach (ToolStripMenuItem item in this.selectCRCTypeToolStripMenuItem.DropDownItems) item.Checked = false;
// "load in" checks from the appropriate place where they are stored
int j = 0;
foreach(ToolStripMenuItem item in this.selectCRCTypeToolStripMenuItem.DropDownItems)
{
item.Checked = G_Tab.old_checkstates[G_Tab.myTabCtrl.SelectedIndex].states[j];
j++;
}
}
TabMgr
构造函数,其中添加了事件处理程序:
注意:G_Tab
是此类的实例
public class TabMgr : Template_Editor //Template_Editor is the base class/Title of the application
{
/*
irrelevant declarations
*/
public TabMgr(Form myform)
{
//Create a reference to the TabControl on the form inside the TabMgr Class instance G_Tab
TabControl buff = myform.Controls.Find("tabControl1", true).FirstOrDefault() as TabControl;
myTabCtrl = buff;
/*
More stuff initialized that's not relevant
*/
//add event handler to TabControl, The event handler is a member function of Base Class Template Editor
myTabCtrl.SelectedIndexChanged += new EventHandler(Update_Real_Checkstates_on_Tab_Change);
//initialize all the checkstate arrays
for (int i = 0; i < old_checkstates.Length; i++) old_checkstates[i].states = new bool[4];
}//TabMgr
//More functions implemented here that are not relevant to this post
}//Public Class TabMgr
我花了很多天尝试解决方案并在其他地方寻找答案,但我看到的每个例子似乎都像我在这里做的那样简单。似乎没有任何东西,但显然我在这里遗漏了一些东西。有没有人知道任何阻止UI正确反映这些ToolStripMenuItems的“Checked”和/或“CheckSate”值的东西?我认为,不知何故,正在改变的价值并没有引用实际的对象,但我不知道如何确定。我已经尝试将ToolStripMenuItems的修饰符设置为设计器中的protected
或public
,并且没有任何帮助。
提前谢谢!
其他信息:
上面的函数Update_Real_Checkstates_on_Tab_Change
是基类Template_Editor
的成员。 G_Tab
是类TabMgr
的实例,它继承自基类。MenuStrip
是基类的成员。
修改:我添加了代码以显示TabMgr
初始化G_Tab
的构造函数