WinForms 菜单工具条获取状态

发布于 2024-08-26 05:42:20 字数 2713 浏览 3 评论 0原文

所以我有一个项目,其中通过一些作为全局变量自动创建的类(是的,它们是静态实例)进行一些自动初始化。在其中的某个时刻(它与用户的 C# GUI 没有关系,因此它不是从任何 C# 类派生的),我需要查看是否设置了标志。

我使用具有选中和未选中状态的工具条菜单来设置或取消设置标志。问题是我很难从这个静态类中查看是否检查了该标志。我的类位于不同的项目/命名空间内,并创建了一个 DLL,稍后链接到应用程序的 GUI。 GUI 依赖于该 Manager 类,因此不能选择让 Manager 类依赖于 GUI。尽管如此,我应该能够通过它的名字或其他方式看到它的状态。我已经尝试了以下操作:

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

现在的问题是,在上面的代码片段中我遇到了一个严重的错误。那么我该怎么做呢?

工具条菜单: 替代文本

错误:

请参阅此消息的末尾 有关即时调用的详细信息 (JIT) 调试而不是此对话框 框。

**************** 异常文本 ************** System.ArgumentOutOfRangeException: 指数超出范围。必须是 非负且小于 集合。参数名称:索引 在 System.Collections.ArrayList.get_Item(Int32 索引)在 System.Windows.Forms.FormCollection.get_Item(Int32 索引)在 Manager.MyMainManager.MyMainManager.RealTimeInit() 在 C:\Dropbox\My Dropbox\公共\程序 代码\RoboCup\Manager\MyMainManager\MyMainManager.cs:行 494 在 mainApp.MainForm.ButtonInitClick(对象 发送者、EventArgs e) 位于 C:\Dropbox\My Dropbox\公共\程序 代码\RoboCup\mainApp\MainForm.cs:行 389 在 System.Windows.Forms.Control.OnClick(EventArgs 吃 System.Windows.Forms.Button.OnClick(EventArgs 吃 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 梅事件)在 System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons 按钮,Int32 单击) 在 System.Windows.Forms.Control.WndProc(Message& 米)在 System.Windows.Forms.ButtonBase.WndProc(Message& 米)在 System.Windows.Forms.Button.WndProc(Message& 米)在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& 米)在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& 米)在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd、Int32 消息、IntPtr wparam、IntPtr l参数)

替代文本

使用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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽梦忆笙歌 2024-09-02 05:42:20

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

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;

有更清洁的解决方案吗?

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 ?

疑心病 2024-09-02 05:42:20

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文