所以我有一个项目,通过一些类自动初始化,这些类是作为全局变量自动创建的(是的,它们是静态实例)。在这一点上(它与用户的C# GUI没有关系,所以它不是从任何C#类派生出来的),我需要看看是否设置了一个标志。
我使用工具条菜单的选中和未选中状态,以便设置或取消设置标志。问题是,我很难从这个静态类中查看是否检查了标志。我的类在一个不同的项目/命名空间中,并创建了一个DLL,稍后链接到应用程序的GUI。GUI依赖于此Manager类,因此使Manager类依赖于GUI并不是一个选项。尽管如此,我应该能够看到它的状态知道它的名字或通过其他方式。我尝试了以下几点:
if(Application.OpenForms[0].Owner.Controls["_useLocalImageForInitToolStripMenuItem"].Enabled)
{ };现在的问题是,在上面的代码片段中,我得到了一个严重的错误。那我该怎么做?
工具条菜单:

错误:
有关调用即时调试(JIT)而不是此对话框的详细信息,请参阅此消息的结尾。
*例外文本* System.ArgumentOutOfRangeException:索引超出了范围。必须是非负的,并且小于集合的大小。参数名称:在C:\Dropbox\My Dropbox\Public\Program Code\RoboCup\Manager\MyMainManager\MyMainManager.cs:line 494 at mainApp.MainForm.ButtonInitClick(对象发送方)中的System.Collections.ArrayList.get_Item索引(Int32索引))在C:\Dropbox\My Dropbox\Public\Program Code\RoboCup\mainApp\MainForm.cs:line 389 at System.Windows.Forms.Control.WmMouseUp(Message& e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Control.WmMouseUp(Message& m,MouseButtons按钮,( System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) ( System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

用private System.Windows.Forms.ToolStripMenuItem _useLocalImageForInitToolStripMenuItem;
this._useLocalImageForInitToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked;
this._useLocalImageForInitToolStripMenuItem.Name = "_useLocalImageForInitToolStripMenuItem";
this._useLocalImageForInitToolStripMenuItem.Size = new System.Drawing.Size(242, 22);
this._useLocalImageForInitToolStripMenuItem.Text = "Use local image for Initialization";
this._useLocalImageForInitToolStripMenuItem.Click += new System.EventHandler(this.发布于 2010-03-23 19:30:06
好吧,某种程度上,我成功地做了我想做的事情,但是它看起来不太好,因为对菜单路径的任何修改都会导致非功能性。
var alfa = ((((Application.OpenForms[0].Controls["_menustripMenu"]
as System.Windows.Forms.MenuStrip).
Items["_settingsToolStripMenuItem"]
as System.Windows.Forms.ToolStripMenuItem).
DropDownItems["_cameraToolStripMenuItem"]
as System.Windows.Forms.ToolStripMenuItem).
DropDownItems["_useLocalImageForInitToolStripMenuItem"]
as System.Windows.Forms.ToolStripMenuItem).Checked;有更干净的解决方案吗?
发布于 2010-03-23 14:39:58
考虑一种更自上而下的方法,而不是自下而上。不要试图让您的设置类从GUI中读取一个值,而是让GUI在GUI值更改时在设置类中设置该值。我在我自己的应用程序中使用了类似的方法,我的设置类有一个公共ReloadValues()方法,如果我对后台数据存储进行更改,就可以调用该方法。
https://stackoverflow.com/questions/2500704
复制相似问题