vb.net 如何清除组合框文本

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

How to clear ComboBox Text

vb.netcombobox

提问by Sky Scraper

when i use ComboBox1.Items.Clear()it clears all the items in the combobox and when i use ComboBox1.SelectedIndex = -1it doesn't show any text and when i choose any of the entry, it doesn't show, it's blank.

当我使用ComboBox1.Items.Clear()它时会清除组合框中的所有项目,当我使用ComboBox1.SelectedIndex = -1它时不显示任何文本,当我选择任何条目时,它不显示,它是空白的。

this is my code to show my database fields into a ComboBox, and

这是将我的数据库字段显示到 ComboBox 中的代码,以及

Imports MySql.Data.MySqlClient
Public Class Form4

Dim con As New MySqlConnection
Dim cmd As New MySqlCommand
Dim da As New MySqlDataAdapter
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    con.ConnectionString = ("server=localhost;user id=root;database=db")
    Try
        con.Open()

        With cmd
            .Connection = con
            .CommandText = "SELECT CONCAT_WS(' ', cfname, cmname, clname,'from', cparty,'party') as names, " & _
                       "cpos, cid from candidate WHERE cpos='President'"
With ComboBox7
            Dim dv6 = New DataView(dt, "cpos='President'", "", DataViewRowState.CurrentRows)
            .DisplayMember = "names"
            .ValueMember = "names"
            .DataSource = dv6
        End With
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    con.Close()
End Sub
End Class

回答by ElektroStudios

How to clear ComboBox Text

如何清除组合框文本

You should use:

你应该使用:

ComboBox1.ResetText()

Or else:

要不然:

ComboBox1.Text = String.Empty

回答by Elton Costa

In Windows Forms and WPF, you can say:

在 Windows 窗体和 WPF 中,您可以说:

ComboBox1.Text = String.Empty

ComboBox1.Text = String.Empty

回答by Anthony

While setting the text to ""might work, it will only clear the text and if your combo box populates some text boxes then these will remain with the previous values. Try:

虽然将文本设置为""可能有效,但它只会清除文本,如果您的组合框填充了一些文本框,那么这些文本框将保留以前的值。尝试:

ComboBox1.SelectedItem = Nothing

This will also work if your combo box is grayed out such that you can't type anything into it.

如果您的组合框变灰以至于您无法在其中输入任何内容,这也将起作用。