小编EMa*_*x84的帖子

更改继承的.net控件上的属性的DefaultValue

在.net中,我有一个继承的控件:

public CustomComboBox : ComboBox
Run Code Online (Sandbox Code Playgroud)

我只是想将DropDownStyle属性的默认值更改为另一个值(ComboBoxStyle.DropDownList),除了在基类(ComboBoxStyle.DropDown)中指定的默认值.

有人可能认为你可以添加构造函数:

public CustomComboBox()
{
     this.DropDownStyle = ComboBoxStyle.DropDownList;
}
Run Code Online (Sandbox Code Playgroud)

但是,这种方法会使Visual Studio Designer感到困惑.在Visual Studio中设计自定义控件时,如果为DropDownStyle选择ComboBoxStyle.DropDown,则认为您选择的属性仍然是默认值(来自基本ComboBox类中的[DevaultValue()]),因此它不会将customComboBox.DropDownStyle = ComboBoxStyle.DropDown行添加到Designer.cs文件中.令人困惑的是,您发现一旦运行屏幕就不会按预期运行.

好吧,你不能覆盖DropDownStyle属性,因为它不是虚拟的,但你可以这样做:

[DefaultValue(typeof(ComboBoxStyle), "DropDownList")]
public new ComboBoxStyle DropDownStyle
{
      set { base.DropDownStyle = value; }
      get { return base.DropDownStyle; }
}
Run Code Online (Sandbox Code Playgroud)

但是你会因使用"新"声明的细微差别而遇到麻烦.我已经尝试了它,它似乎不正常,因为视觉工作室设计师也从这种方法混淆并强制ComboBoxStyle.DropDown(基类的默认值).

有没有其他方法可以做到这一点?对于冗长的问题很抱歉,很难详细描述.

.net c# controls default properties

10
推荐指数
1
解决办法
5653
查看次数

强制选项卡到扩展.net控件中的下一个控件

假设我在.net中扩展名为CustomTextBox的TextBox.在某些情况下,我想强制选项卡到表单上的下一个TabIndex.有没有办法做到这一点,除了获取CustomTextBox的父级中包含的所有控件,按TabIndex排序,然后聚焦下一个序数?

.net c# events controls winforms

6
推荐指数
1
解决办法
5332
查看次数

GUI在.net中使用PrintDialog和PrintPreviewDialog时锁定

我正在使用.net的PrintPreviewDialog,每当它生成预览时,它会在后台锁定我的GUI并使其看起来像已经崩溃直到预览完成.看到弹出的.net页面进度窗口不是一个对话框,可以选择背景,然后以半绘制的锁定方式进入前面.当用户单击预览对话框上的实际"打印"按钮时,以及刚运行PrintDocument.Print()时,也会发生这种情况.有一种简单的方法可以修改以下代码,以便在用户等待.net绘制打印页面时停止GUI挂起:

//just showing a preview, hangs up background GUI on generating the preview
// and when user prints straight from the preview
this.printPreviewDialog.ShowDialog(this);

//just trying to print a .net PrintDocument class, GUI hangs in background
// when .net is drawing the pages
this.printDocument.Print();
Run Code Online (Sandbox Code Playgroud)

.net c# printing user-interface multithreading

5
推荐指数
1
解决办法
3478
查看次数