【问题标题】:How to disable certain string inside text box?如何禁用文本框中的某些字符串?
【发布时间】:2018-02-21 15:12:57
【问题描述】:

我在下面的代码中所做的表明文本框内的所有数据都被禁用了。

Private Sub chkSTC_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkSTC.CheckedChanged
        Select Case chkSTC.Checked
            Case True
                txtCargoDescription.Enabled = False
               
            Case False
                txtCargoDescription.Enabled = True
               
        End Select
    End Sub

End Class

如何从文本框中仅禁用某些字符串 "1 X 40 HQ STC"。我应该添加什么?

请参考这张图片:

谢谢你!

【问题讨论】:

  • 似乎此解决方案适合您在客户端所需的内容:stackoverflow.com/a/21982192。您可以参考 textarea 中的keydownkeypress 事件,然后使用event.keyCode 查看是否按下了键盘上的退格/删除按钮。
  • @TetsuyaYamamoto tq 我会试试的
  • 看看我的回答

标签: asp.net vb.net checkbox textbox


【解决方案1】:

编写数据验证程序以删除“1 X 40 HQ STC”,以便只显示想要的字符串。

【讨论】:

    【解决方案2】:

    您不能禁用文本框中的字符串,但可以阻止用户在该字符串中进行更改。例如,如果用户尝试在该字符串中添加、删除或进行任何更改,请不要让它们发生。

    我这样做了:

     Private Sub TextBox1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
        'see if any change is made between the first 13 characters (13 characters because "1 X 40 HQ STC" had 13 chars)
        If sender.selectionstart <= 14 Then
    
            'see if a character is deleted (tried to press backspace or delete key)
            If e.KeyCode = 8 Or e.KeyCode = 46 Then
                'replace the first 12 characters with our orignal text
                TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text.Substring(0, 12), "1 X 40 HQ STC")
                'if left or right key is pressed
            ElseIf e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Then
                'do nothing
            ElseIf e.KeyCode = Keys.Enter Then
                TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text.Substring(0, 15), "1 X 40 HQ STC")
            Else
                'it means other text is added, remove 14 characters and put our original string
                TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text.Substring(0, 14), "1 X 40 HQ STC")
            End If
            'set the cursor to 15th character, i.e. next line
            sender.selectionstart = 15
    
        End If
    End Sub
    

    【讨论】:

    • 嗨 Subaz,你能从我上面的代码中显示出来吗?因为"1 X 40 HQ STC" 从数据库中检索。
    • 什么意思?
    【解决方案3】:

    您只能禁用控件。您不能禁用控件中的字符串。您可以做的最好的事情是编写事件处理程序(例如 OnChange),如果它会导致字符串被修改,则取消该事件。不过,更常见的解决方案是将字符串放在单独的控件中。

    【讨论】:

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