简体   繁体   English

无法访问使用设计器外部的代码创建的菜单项的选中属性

[英]Can't access checked property of menu item created with code outside the designer

private void Form_Shown(object sender, EventArgs e)
{
    // Load Settings
    this.tsmiDuplexEnabled.Checked = Properties.Settings.Default.DuplexEnabled;
    this.tsmiRemoveBlanks.Checked = Properties.Settings.Default.AutoDiscardBlanks;

    this.tsmiColorMode.DropDownItems[Properties.Settings.Default.ColorMode].Checked = true;
}

The last line does not work because it doesn't find the checked property, although there are many available properties. 最后一行不起作用,因为尽管有许多可用的属性,但它没有找到选中的属性。 Any idea how I can get at that property? 知道我如何获得该物业吗?

You need to cast it as a ToolStripMenuItem to get the Checked property. 您需要将其ToolStripMenuItem转换为ToolStripMenuItem以获得Checked属性。 Note that separators are not ToolStripMenuItem so you can't blindly cast every DropDownItem as a ToolStripMenuItem . 请注意,分隔符不是ToolStripMenuItem因此您不能盲目地将每个DropDownItem投射为ToolStripMenuItem

For example: 例如:

foreach (ToolStripItem tsi in item.DropDownItems)
{
    if (tsi is ToolStripMenuItem)
        ((ToolStripMenuItem)tsi).Checked = true;
}

In your case it looks like you won't accidentally get a separator, so this should work: 在您的情况下,您似乎不会偶然得到分隔符,因此这应该可以工作:

((ToolStripMenuItem)this.tsmiColorMode.DropDownItems[Properties.Settings.Default.ColorMode]).Checked = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将属性绑定到上下文菜单中动态创建的项目 - Binding property to dynamicaly created item of context menu 动态创建的带有附加代码的菜单项? - Dynamically Created Menu Item with attached code? 无法访问文字后面的代码中创建的HTML输入控件 - Can't access HTML input controls created in code behind in a literal 在VS设计器中公开类型列表的属性<class>限制/隐藏对成员的访问权限或将属性显示为可扩展菜单? - Expose A Property Of Type List<class> In VS Designer Limiting/Hiding Access To Members Or Show Property As Expandable Menu? 我如何在课外访问属性 - How can I access the property outside of the class 在Designer中创建的ImageList - 代码在哪里? - ImageList created in Designer - Where is the code? 为什么我们不能访问派生 class 中方法之外的基本属性? - Why can't we access a base property outside of a method in derived class? UseCompatibleTextRendering属性设置为false时,不是由设计者创建的 - UseCompatibleTextRendering property not created by designer when it is set to false 无法获得所选项目的上下文菜单 - Can't get selected item context menu 无法选择禁用或不可选择的菜单项 - Can't select a disabled or unselectable menu item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM