2010-03-23 57 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