如何禁用文本框中的某些字符串?

时间:2017-09-13 03:49:40

标签: asp.net vb.net checkbox textbox

我在下面的代码中所做的是显示文本框中的所有数据都已禁用。

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"的某个字符串。我应该添加什么?

请参考此图片:

Please refer this image

先谢谢你了!

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

我这样做了:

 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

答案 2 :(得分:0)

写入数据验证程序以删除&#34; 1 X 40 HQ STC&#34;,以便只显示想要的字符串。