2

我在一个 vb 表单中有 2 个文本框。如果 txtMaterial 被填充,我想禁用 txtPackage,反之亦然。我使用下面的代码,但它不起作用。有人可以修复它吗?真的很感激。tq。

Private Sub txtMaterial_TextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged
    txtMaterial.Enabled = True
    txtPackage.Enabled = False

End Sub

Private Sub txtPackage_TextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPackage.TextChanged

    txtPackage.Enabled = True
    txtMaterial.Enabled = False

End Sub
4

3 回答 3

3

让两个 TextBox 触发相同的处理程序,然后根据另一个 TextBox 中是否有内容来简单地设置每个的 Enabled() 状态:

Private Sub txtChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged, txtPackage.TextChanged
    txtMaterial.Enabled = (txtPackage.TextLength = 0)
    txtPackage.Enabled = (txtMaterial.TextLength = 0)
End Sub

*请注意,第一行的结尾处列出了两个控件Handles txtMaterial.TextChanged, txtPackage.TextChanged

于 2013-07-02T05:09:50.813 回答
0

根据您添加的评论

Blockquote 我想让它作为第二个禁用。但在我的情况下,如果我填充 txtMaterial txtPackage 也可以同时填充文本。

您的问题的解决方案必须是这样的:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    txtPackage.Enabled = False
End Sub


Private Sub txtMaterial_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged
    If Not String.IsNullOrEmpty(txtMaterial.Text) Then
        txtPackage.Enabled = True
    Else
        txtPackage.Enabled = False
    End If
End Sub
于 2013-07-03T11:23:49.267 回答
0
Private Sub txtMaterial_TextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged
    If txtMaterial.Text <> "" Then
        txtPackage.Enabled =False
    End If
End Sub
于 2013-07-02T04:44:23.923 回答