[英]C#.net WinForms how to causes text in textbox to disappear when textbox is on focus?
[英]how to make a textbox and/or a listbox disappear when not needed in Visual Basic .NET
對於我在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
不太了解您要做什么,但是使listbox
和button
消失的代碼是:
ListBox1.visible = false
Button1.visible = false
然后,當您希望它們再次出現時:
ListBox1.visible = true
Button1.visible = true
當您使用列表框作為輸出時,為WinForms添加
ListBox1.Visible=True 'make sure user can see output
TextBox1.Visible=False
Button1.Visible=False 'textbox button
Button2.Visible=False 'textbox button
或用於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
如果將TextBox用作輸出,請使用類似的代碼。
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
只需添加這兩個子項,您所需的內容就可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.