【问题标题】:WinForms Menu Toolstrip Get StatusWinForms 菜单工具条获取状态
【发布时间】:2010-03-23 14:34:52
【问题描述】:

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

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

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

现在的问题是,在上面的代码 sn-p 上我得到了一个讨厌的错误。那我该怎么做呢?

工具条菜单:

错误:

请参阅此消息的末尾以获取 即时调用的详细信息 (JIT) 调试而不是此对话框 盒子。

**************异常文本************** System.ArgumentOutOfRangeException: 指数超出范围。一定是 非负且小于 集合。参数名称:索引 在 System.Collections.ArrayList.get_Item(Int32 指数)在 System.Windows.Forms.FormCollection.get_Item(Int32 指数)在 Manager.MyMainManager.MyMainManager.RealTimeInit() 在 C:\Dropbox\我的 Dropbox\公共\程序 代码\RoboCup\Manager\MyMainManager\MyMainManager.cs:line 494 在 mainApp.MainForm.ButtonInitClick(对象 发件人,EventArgs e) 在 C:\Dropbox\My Dropbox\公共\程序 代码\RoboCup\mainApp\MainForm.cs:line 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(消息& m) 在 System.Windows.Forms.ButtonBase.WndProc(消息& m) 在 System.Windows.Forms.Button.WndProc(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr 参数)

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.

【问题讨论】:

  • 你能复制你得到的错误信息吗?
  • 我稍后会发布错误消息,因为我无法在笔记本电脑上重现该问题,我需要去办公室,但基本上是一种令人讨厌的未处理异常,抱怨他找不到它。

标签: c# winforms visibility toolstrip


【解决方案1】:

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

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;

任何更清洁的解决方案?

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 不太确定我是否关注你。我有两个问题。我将如何从我的项目中看到设置类?更改已经生效,只是不确定如何查看项目中的当前值。能不能详细一点?
    • 您目前如何将设置类/库公开给项目的其余部分?
    • 现在我完全不暴露,只有主应用可以看到。
    猜你喜欢
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多