2010-03-23 61 views
2

所以我有一个项目,通过一些自动创建的类作为全局变量进行自动初始化(是的,它们是静态实例)。在这里面的一点(它与用户的C#GUI没有关系,所以它不是从任何C#类派生的)我需要看看是否设置了一个标志。WinForms菜单工具条获取状态

我使用带有选中和未选中状态的工具栏菜单来设置或取消设置标志。问题是,我很难从该静态类中查看标志是否被选中。我的类位于不同的项目/名称空间中,并且创建了一个DLL,该DLL随后会链接到应用程序的GUI。 GUI依赖于此Manager类,因此使Manager类不依赖于GUI而不是选项。不过,我应该能够看到其状态知道它的名字或通过其他方式。我曾尝试以下:

if(Application.OpenForms[0].Owner.Controls["_useLocalImageForInitToolStripMenuItem"].Enabled) 
{ }; 

现在的问题是,在上代码片段,我得到一个讨厌的错误。那么,我该如何做到这一点?

的工具条菜单: alt text http://img251.imageshack.us/img251/6473/imagetoolstrip.jpg

错误:

See the end of this message for details on invoking just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text ************** System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.Collections.ArrayList.get_Item(Int32 index) at System.Windows.Forms.FormCollection.get_Item(Int32 index) at Manager.MyMainManager.MyMainManager.RealTimeInit() in C:\Dropbox\My Dropbox\Public\Program Code\RoboCup\Manager\MyMainManager\MyMainManager.cs:line 494 at mainApp.MainForm.ButtonInitClick(Object sender, EventArgs e) in C:\Dropbox\My Dropbox\Public\Program Code\RoboCup\mainApp\MainForm.cs:line 389 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

alt text http://img185.imageshack.us/img185/2734/20100323095952.png

随着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. 
+0

你能复制你收到的错误信息吗? – FOR 2010-03-23 14:37:02

+0

我稍后会发布错误消息,因为我无法在笔记本电脑上重现问题,而且我需要进入办公室,但基本上是一种令人讨厌的未处理异常,抱怨他无法找到它。 – Yeti 2010-03-23 14:51:59

回答

2

好吧,我设法做我想做的事情,但它看起来不太好,因为菜单路径的任何修改都会导致无法使用。

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; 

任何更清洁的解决方案?

1

考虑一个自上而下的方法,而不是博TTOM行动。而不是试图让您的设置类从GUI读取值,而是在GUI值更改时让GUI在设置类中设置值。我在自己的应用程序中使用了类似的方法,我的设置类有一个公共的ReloadValues()方法,如果我对后备数据存储进行更改,则可以调用该方法。

+0

不太清楚我跟着你。我有两个问题。我将如何看到我的项目中的设置类?这一变化已经在实施,但并不确定如何看到项目中的当前价值。你能更精致一点吗? – Yeti 2010-03-23 14:44:23

+0

如何将设置类/库暴露给当前项目的其余部分? – 2010-03-23 19:50:55

+0

现在我根本不公开它,只有主应用程序才能看到它。 – Yeti 2010-03-24 22:16:53