2

So I have a project where there is some automatic initialization going on through some classes that are created automatically as global variables (yeah they are static instances). At a point inside this (it has no relation with the C# GUI for the user, so it isn't derived from any C# class) I need to see if a flag is set or not.

I use toolstrip menu with checked and unchecked status in order to set or unset the flag. The problem is that I have difficulties to see if the flag is checked or not from this static class. My class is inside a different project/namespace and a DLL is created what later is linked to the GUI of the application. The GUI depends from this Manager class so making the Manager class to depend from the GUI is not an option. Nevertheless, I should be able to see its state knowing its name or through some other means. I have tried the following:

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

Now the problem is that on the upper code snippet I get a nasty error. So how do I do this?

The toolstrip menu: alt text

The error:

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

With 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.
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Yeti
  • 841
  • 5
  • 26
  • could you copy the error message you are getting? – FOR Mar 23 '10 at 14:37
  • I will post the error message later on cause I cannot reproduce the problem at my laptop and I need to get in the office but basically is a nasty unhandled exception sort of complaining that he cannot find it. – Yeti Mar 23 '10 at 14:51

2 Answers2

2

Okay sort of I managed to do what I wanted however it does not looks good as any modification to the menu path will cause a non-functionality.

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;

Any cleaner solution ?

Yeti
  • 841
  • 5
  • 26
1

Consider a more top-down approach, rather than bottom-up. Instead of trying to have your settings class read a value from the GUI, instead have the GUI set the value in the settings class when the GUI value changes. I use a similar approach in an application of my own and my settings class has a public ReloadValues() method that can be invoked if I make changes to the backing data store.

Nathan Taylor
  • 24,423
  • 19
  • 99
  • 156
  • Not really sure I follow you. I have two questions. How will I see the settings class from my project? The change is already working just not sure how to see the current value across the projects. Could you be a little more elaborate ? – Yeti Mar 23 '10 at 14:44
  • How are you exposing the settings class/library to the rest of your project currently? – Nathan Taylor Mar 23 '10 at 19:50
  • Now I do not expose it at all, only the main application can see it. – Yeti Mar 24 '10 at 22:16