且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

禁用窗体中的所有文本框

更新时间:2023-12-06 16:33:22

for each ctrl as control in me.controls
 if typeof ctrl is textbox then
  if not ctrl.name = sender.name then
    ctrl.enable = false
  end if
 end if
next



您将需要在每个文本框textchanged或类似的事件中放入这样的内容



you will need to put something like this in each textboxes textchanged or similar event


这就是我的做法:

首先,我为所有文本框创建了一个通用的TextChanged事件.
This is how I did:

First I created a common TextChanged event for all the textboxes.
Private Sub TextChanged(sender As Object, e As EventArgs)
	Dim txtEdit As TextBox = DirectCast(sender, TextBox)
	For Each cntrl As Control In Me.Controls
		If TypeOf cntrl Is TextBox AndAlso cntrl.Name <> txtEdit.Name Then
			cntrl.Enabled = False
		End If
	Next
End Sub
}



然后注册:



And then register it :

Public Sub New()
    InitializeComponent();
    AddHandler(textBox1.TextChanged, AddressOf(TextChanged))
    AddHandler(textBox2.TextChanged, AddressOf(TextChanged))
    AddHandler(textBox3.TextChanged, AddressOf(TextChanged))
End Sub



希望对您有所帮助.



Hope it helped.


尝试一下:
try this:
protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        TextBox1.Enabled = false;
        TextBox2.Enabled = false;

        TextBox3.Enabled = false;
        TextBox4.Enabled = false;
    }