【问题标题】:Disabling or making it readonly a part of the values in a Text Box (.net)禁用或使其只读成为文本框 (.net) 中值的一部分
【发布时间】:2011-05-06 11:44:19
【问题描述】:

我有一个具有初始值的文本框(多行),现在当用户尝试将值附加到它时,他应该无法修改初始值。有没有可能?

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    因此,如果您希望用户能够添加但不能删除该文本的框中有“lorem ipsum”?如果是这样,那么使用 RichTextBox 您可以通过选择的.SelectionProtected 属性来执行此操作,该属性会将区域标记为有效地只读。

    rtBox.Select(0, (rtBox.Text = "I am fixed content").Length);
    rtBox.SelectionProtected = true;
    rtBox.AppendText(Environment.NewLine);
    

    【讨论】:

      【解决方案2】:

      另一种选择是使用蒙版文本框。这样,您可以根据需要拥有多个保护区。

      例如,您可以将掩码设置为:

      "This c\annot be ch\anged. But this c\an\: CCCCCCCCCC"
      

      会显示为:

      "This cannot be changed. But this can: __________"
      

      输入与您希望用户能够输入的字符一样多的“C”。如果您愿意,也可以将提示字符更改为空格而不是“_”。

      为了方便……

      这里是屏蔽字符的列表和说明

      (取自http://www.c-sharpcorner.com/uploadfile/mahesh/maskedtextbox-in-C-Sharp/)。

      0 - Digit, required. Value between 0 and 9.
      9 - Digit or space, optional.
      # - Digit or space, optional. If this position is blank in the mask, it will be rendered as a space in the Text property.
      L - Letter, required. Restricts input to the ASCII letters a-z and A-Z.
      ? - Letter, optional. Restricts input to the ASCII letters a-z and A-Z.
      & - Character, required.
      C - Character, optional. Any non-control character.
      A - Alphanumeric, required.
      a - Alphanumeric, optional.
      .  - Decimal placeholder.
      , - Thousands placeholder.
      : - Time separator.
      / - Date separator.
      $ - Currency symbol.
      < - Shift down. Converts all characters that follow to lowercase.
      > - Shift up. Converts all characters that follow to uppercase.
      | - Disable a previous shift up or shift down.
      \ - Escape. Escapes a mask character, turning it into a literal. "\\" is the escape sequence for a backslash.
      

      所有其他字符 - 文字。所有非掩码元素都将在 MaskedTextBox 中显示为自身。文字在运行时始终占据掩码中的静态位置,用户无法移动或删除。

      【讨论】:

        【解决方案3】:

        您可以使用 RichTextBox SelectionProtected 属性。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-08
          • 2014-11-23
          • 2020-03-26
          • 2015-10-23
          • 1970-01-01
          • 2014-08-15
          • 2012-06-26
          • 1970-01-01
          相关资源
          最近更新 更多