vb.net 在visual basic中使用复选框启用和禁用TextBox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14876677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:40:33  来源:igfitidea点击:

Enable and disable TextBox with a checkbox in visual basic

vb.netcheckboxtextbox

提问by Simbox97

I've 6 TextBox and 6 CheckBox. Now I want to disable the TextBox1 with a CheckBox1 and reactivate it with the Same CheckBox. How a can do it?

我有 6 个文本框和 6 个复选框。现在我想用 CheckBox1 禁用 TextBox1 并用相同的 CheckBox 重新激活它。怎么办呢?

Edit1 15.55 14/02/2013

编辑 1 15.55 14/02/2013

I have done so to solve my problem!

我这样做是为了解决我的问题!

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox1.Enabled = False
ElseIf CheckBox1.Checked = False Then
TextBox1.Enabled = True End If End Sub
`

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox1.Enabled = False
ElseIf CheckBox1.Checked = False Then
TextBox1.Enabled = True End If End Sub
`

回答by MonkeyDoug

This will work, just add more for the other check boxes

这将起作用,只需为其他复选框添加更多

Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            TextBox1.Enabled = True
        Else
            TextBox1.Enabled = False
        End If
End Sub

What this does: if checkbox1 is check, the checked_changed event fires and the code inside is ran. The if statement looks to see if the checkbox is checked or not. If it is checked, then it sets the textbox1 to enabled, if not it sets it to disabled. Be sure to set the enabled property to either enabled or disabled when you create your program. If you want it to be enabled from the start, that is the default....otherwise set it to disabled in its properties view.

这是做什么的:如果 checkbox1 被选中,则触发 checked_changed 事件并运行里面的代码。if 语句查看复选框是否被选中。如果选中,则将 textbox1 设置为启用,否则将其设置为禁用。创建程序时,请确保将 enabled 属性设置为启用或禁用。如果您希望从一开始就启用它,这是默认设置……否则在其属性视图中将其设置为禁用。

回答by SysDragon

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    TextBox1.Enabled = CheckBox1.Checked
End Sub

回答by Lacey

This works if you have a layer built in that you can send objects behind (therefore hide things). I use this as a way to make text boxes and other items appear and disappear depending on other selections.

如果您有一个内置的层,您可以将对象发送到后面(因此隐藏事物),则此方法有效。我用它来让文本框和其他项目根据其他选择出现和消失。

Private Sub checkbox_Click()
    If (checkbox = True) Then

    ActiveSheet.Shapes("textbox").ZOrder msoSendToFront
    ActiveSheet.Shapes("textbox").ZOrder msoSendToFront

    Else

    ActiveSheet.Shapes("textbox").ZOrder msoSendToBack
    ActiveSheet.Shapes("textbox").ZOrder msoSendToBack

    End If

End Sub

回答by Joshua Bittner

This worked for me:

这对我有用:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


        TextBox1.Enabled = False

        If Not TextBox1.Enabled Then
            TextBox1.BackColor = Color.White
        End If
    End Sub
    Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            TextBox1.Enabled = True
        Else
            TextBox1.Enabled = False
        End If
    End Sub
End Class

回答by Luke Wyatt

Take a look at this tutorial below. After, look at the checkbox control's events and choose the most fitting one. The property you will be changing on the textbox is Enabled.

看看下面的这个教程。之后,查看复选框控件的事件并选择最合适的事件。您将在文本框中更改的属性是Enabled

http://www.youtube.com/watch?v=4PbUryXqZ50

http://www.youtube.com/watch?v=4PbUryXqZ50