vb.net 在 Visual Basic .NET 中不需要时如何使文本框和/或列表框消失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21015331/
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-17 16:23:42  来源:igfitidea点击:

how to make a textbox and/or a listbox disappear when not needed in Visual Basic .NET

.netvb.net

提问by user3176688

For my midterm in Visual Basic .NET i need to show my proficiency in using various common controls, and this includes a listbox and a textbox w/ 2 buttons as an output. Basically i need the textbox with the two buttons to disappear when i am using the listbox as an output and vice versa. image : code:

对于我在 Visual Basic .NET 中的期中考试,我需要展示我使用各种常用控件的熟练程度,这包括一个列表框和一个带有 2 个按钮的文本框作为输出。基本上,当我使用列表框作为输出时,我需要带有两个按钮的文本框消失,反之亦然。图像:代码:

Public Class Form1


  Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    'Dim total As Integer
    'Dim numone As Integer
    'Dim numtwo As Integer
    '
    'numone = Val(TextBox1.Text)
    'numtwo = Val(TextBox3.Text)
    'total = numone + numtwo
    '
    'TextBox5.Text = total

  End Sub

Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
    'Dim total As Integer
    'Dim numone As Integer
    'Dim numtwo As Integer
    '
    'numone = Val(TextBox1.Text)
    'numtwo = Val(TextBox3.Text)
    'total = numone - numtwo
    '
    'TextBox5.Text = total
End Sub

Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
    'Dim total As Integer
    'Dim numone As Integer
    'Dim numtwo As Integer
    '
    'numone = Val(TextBox1.Text)
    'numtwo = Val(TextBox3.Text)
    'total = numone / numtwo

    'TextBox5.Text = total
End Sub

Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
    'Dim total As Integer
    'Dim numone As Integer
    'Dim numtwo As Integer
    '
    'numone = Val(TextBox1.Text)
    'numtwo = Val(TextBox3.Text)
    'total = numone * numtwo
    '
    'TextBox5.Text = total
End Sub

Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
    'Dim FirstName As String
    'Dim LastName As String
    'Dim WholeName As String
    '
    'FirstName = TextBox2.Text
    'LastName = TextBox4.Text
    '
    ' WholeName = FirstName & " " & LastName
    '
    'TextBox5.Text = WholeName

End Sub

Private Sub CheckBox7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox7.CheckedChanged
    If CheckBox7.CheckState = 1 Then
        ComboBox1.Items.Add("Homer Simpson")
        ComboBox1.Items.Add("Marge Simpson")
        ComboBox1.Items.Add("Bart Simpson")
        ComboBox1.Items.Add("Lisa Simpson")
        ComboBox1.Items.Add("Maggie Simpson")

        'TextBox5.Text = Val(ComboBox1)


    End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If CheckBox1.CheckState = 1 Then
        Dim total As Integer
        Dim numone As Integer
        Dim numtwo As Integer

        numone = Val(TextBox1.Text)
        numtwo = Val(TextBox3.Text)
        total = numone + numtwo

        TextBox5.Text = total


    End If
    If CheckBox2.CheckState = 1 Then
        Dim total As Integer
        Dim numone As Integer
        Dim numtwo As Integer

        numone = Val(TextBox1.Text)
        numtwo = Val(TextBox3.Text)
        total = numone - numtwo

        TextBox5.Text = total


    End If
    If CheckBox3.CheckState = 1 Then
        Dim total As Integer
        Dim numone As Integer
        Dim numtwo As Integer

        numone = Val(TextBox1.Text)
        numtwo = Val(TextBox3.Text)
        numone = total / numtwo

        TextBox5.Text = total



    End If
    If CheckBox4.CheckState = 1 Then
        Dim total As Integer
        Dim numone As Integer
        Dim numtwo As Integer

        numone = Val(TextBox1.Text)
        numtwo = Val(TextBox3.Text)
        total = numone * numtwo

        TextBox5.Text = total


    End If
    If CheckBox5.CheckState = 1 Then
        Dim FirstName As String
        Dim LastName As String
        Dim WholeName As String

        FirstName = TextBox2.Text
        LastName = TextBox4.Text

        WholeName = FirstName & " " & LastName

        TextBox5.Text = WholeName


    End If
    If CheckBox6.CheckState = 1 Then

        With ListBox1
            .Enabled = True 'if the listox is enable or disabled
            .Sorted = True ' if you want ti list sorted
            .BorderStyle = BorderStyle.Fixed3D ' the border style
            .Visible = True
            .ScrollAlwaysVisible = True 'presence of scroll all time
            .MultiColumn = False 'add a new column if number of items reach max height
        End With

        ListBox1.Items.Add("Homer Simpson")
        ListBox1.Items.Add("Marge Simpson")
        ListBox1.Items.Add("Bart Simpson")
        ListBox1.Items.Add("Lisa Simpson")
        ListBox1.Items.Add("Maggie Simpson")
        'ListBox1.Items.Add("allo1")
        'ListBox1.Items.Add("allo2")
        'ListBox1.Items.Add("allo3")
        'ListBox1.Items.Add("allo4")
        'ListBox1.Items.Add("allo5")
        'ListBox1.Items.Add("allo1")
        'ListBox1.Items.Add("allo2")
        'ListBox1.Items.Add("allo3")
        'ListBox1.Items.Add("allo4")
        'ListBox1.Items.Add("allo5")
    End If

    If CheckBox7.CheckState = 1 Then
        'ComboBox1.Items.Add("Homer Simpson")
        'ComboBox1.Items.Add("Marge Simpson")
        'ComboBox1.Items.Add("Bart Simpson")
        'ComboBox1.Items.Add("Lisa Simpson")
        'ComboBox1.Items.Add("Maggie Simpson")

        TextBox5.Text = ComboBox1.Text


    End If

End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    TextBox5.Clear()

End Sub


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    TextBox1.Clear()
    TextBox2.Clear()
    TextBox3.Clear()
    TextBox4.Clear()
    TextBox5.Clear()
    ListBox1.Items.Clear()
    Me.ComboBox1.Items.Clear()
    ' text2.Clear()
    ' text1.Clear()
    ' text2.Clear()

    ComboBox1.Items.Clear()
    CheckBox1.Checked = False
    CheckBox2.Checked = False
    CheckBox3.Checked = False
    CheckBox4.Checked = False
    CheckBox5.Checked = False
    CheckBox6.Checked = False
    CheckBox7.Checked = False

End Sub
End Class

采纳答案by srka

When you use ListBox as output add for WinForms

当您使用 ListBox 作为 WinForms 的输出添加时

ListBox1.Visible=True 'make sure user can see output
TextBox1.Visible=False
Button1.Visible=False 'textbox button
Button2.Visible=False 'textbox button

or for WPF

或用于 WPF

ListBox1.Visibility=Visibility.Visible 'make sure user can see output
TextBox1.Visibility=Visibility.Hidden
Button1.Visibility=Visibility.Hidden 'textbox button
Button2.Visibility=Visibility.Hidden 'textbox button

Use similar code if you use TextBox as output.

如果您使用 TextBox 作为输出,请使用类似的代码。

回答by j_t_fusion

Don't really understand what you are trying to do but the codes for making the listboxand buttondisappear are:

不太明白您要做什么,但制作listboxbutton消失的代码是:

ListBox1.visible = false
Button1.visible = false

then when you want them to appear again:

然后当您希望它们再次出现时:

ListBox1.visible = true
Button1.visible = true

回答by Hoh

Private Sub TextBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
    Me.ListBox1.visible = False
End Sub

Private Sub ListBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
    Me.TextBox1.Visible = False
    Me.Button1.Visible = False
    Me.Button2.Visible = False
End Sub

Just add these two subs and what you want should work properly.

只需添加这两个子程序,您想要的就可以正常工作。