【问题标题】:set string in TextEdit which allows only int在 TextEdit 中设置字符串,它只允许 int
【发布时间】:2012-10-24 12:38:01
【问题描述】:

我正在使用 DevExpress。 在我的项目中,我有控件(textEdit),其中 EditValue 绑定到“int”类型的属性。 问题是控件只允许输入数字。

我的任务是:当表单处于编辑模式时,textEdit 应该显示单词“Automatic”,并且只有在按下安全按钮后才会生成数字。 现在在编辑模式下文本框显示“0”,是否可以在“0”的情况下使其显示“自动”。

文本框绑定的属性有:

int fEventNr;
public int EventNr {
    get { return fEventNr; }
    set { SetPropertyValue<int>("EventNr", ref fEventNr, value); }
}

一切正常,除了它显示“0”而且我不知道如何让他显示“自动” 也许有人有什么想法?

【问题讨论】:

    标签: c# textbox devexpress


    【解决方案1】:

    这是解决您问题的方法:

    textEdit1.Properties.CustomDisplayText += new Properties_CustomDisplayText;
    
    
    void Properties_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
    {
        if (yourCondition)
            e.DisplayText = "Automatic";
    } 
    

    【讨论】:

      【解决方案2】:
      txtEdit.Properties.NullText = "Automatic";
      txtEdit.EditValue = null;
      

      考虑将public int EventNr 更改为public int? EventNr,这样您就可以确定如果EditValue 为空,用户没有提供任何值,您应该“自动”生成它:) 我认为将 0 视为 [未设置值] 是一种不好的做法。这就是他们发明 null 的原因。

      【讨论】:

        【解决方案3】:

        在属性面板上转到属性 -> Mask . Set "MaskType" to RegEx 并将 "EditMask" 设置为 \d*。如果您不希望整数以零开头,请将"EditMask" 设置为[1-9]+\d*。 或者,您可以通过代码来完成:

        this.textEditIntegersOnly.Properties.Mask.EditMask = "[1-9]+\\d*";
        this.textEditIntegersOnly.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx; 
        

        【讨论】:

          猜你喜欢
          • 2010-12-09
          • 1970-01-01
          • 2012-10-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多