如何将ComboBox重置为默认文本i在属性中设置。假设我有一个带有默认文本的#ComboBox" Rub y"当在TextBox中打印SelectedIndexChanged之后,它会重置为默认文本" Ruby "但是SelectedItem。我希望它能够阅读" Ruby "之后或所有时间,如果不可能。谢谢
答案 0 :(得分:0)
Dim _originalComboText As String
Sub New()
)中:_originalComboText = ComboBox1.Text
ComboBox1.Text = _originalComboText
如果您检查属于您表单的.designer.vb
文件的内容,您实际上可以通过作业(=
)看到控件文本的初始化 - 所以vb.net不会这里特别神奇。如果要保留文本,则必须在丢失之前将其保存在某处。
以下是完整的最低范例:
Public Class Form1
Dim _originalComboText As String
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_originalComboText = ComboBox1.Text
End Sub
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1.Text = _originalComboText
End Sub
Private Sub ComboBox1_LocationChanged(sender As Object, e As EventArgs) Handles ComboBox1.LocationChanged
ComboBox1.Text = _originalComboText
End Sub
End Class
如果你的两个处理程序没有区别,那么只用一个替换它们 - 在Handles子句中有两个事件:
Private Sub ComboBox1_RestoreText(sender As Object, e As EventArgs) _
Handles ComboBox1.Leave, ComboBox1.LocationChanged
ComboBox1.Text = _originalComboText
End Sub
答案 1 :(得分:0)
使用此:
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1.Text = "Ruby"
End Sub
Private Sub ComboBox1_LocationChanged(sender As Object, e As EventArgs) Handles ComboBox1.LocationChanged
ComboBox1.Text = "Ruby"
End Sub
由于用户会选择,从下拉列表中说出print <<"EOF"; ---mytext---EOF
,这将自动在文本框上打印。他或她必须去文本框或其他地方。
我知道回答自己的问题不是常规问题,但是如果有人需要帮助的话。这是