Export (0) Print
Expand All

ComboBox.Text Property

Gets or sets the text associated with this control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

'Declaration
<BindableAttribute(True)> _
<LocalizableAttribute(True)> _
Public Overrides Property Text As String
'Usage
Dim instance As ComboBox
Dim value As String

value = instance.Text

instance.Text = value
/** @property */
public String get_Text ()

/** @property */
public void set_Text (String value)

public override function get Text () : String

public override function set Text (value : String)

Not applicable.

Property Value

The text associated with this control.

When setting the Text property, a null reference (Nothing in Visual Basic) or an empty string ("") sets the SelectedIndex to -1.

The following code example demonstrates how to initialize a ComboBox control by setting the Text property and using the AddRange method to populate the ComboBox. It also demonstrates handling the DropDown event. To run the example, paste the following code in a form and call the InitializeComboBox method in the form's constructor or Load event.


' Declare ComboBox1.
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox

' Initialize ComboBox1.
Private Sub InitializeComboBox()
    Me.ComboBox1 = New ComboBox
    Me.ComboBox1.Location = New System.Drawing.Point(128, 48)
    Me.ComboBox1.Name = "ComboBox1"
    Me.ComboBox1.Size = New System.Drawing.Size(100, 21)
    Me.ComboBox1.TabIndex = 0
    Me.ComboBox1.Text = "Typical"
    Dim installs() As String = New String() _
        {"Typical", "Compact", "Custom"}
    ComboBox1.Items.AddRange(installs)
    Me.Controls.Add(Me.ComboBox1)
End Sub

' Handles the ComboBox1 DropDown event. If the user expands the  
' drop-down box, a message box will appear, recommending the
' typical installation.
Private Sub ComboBox1_DropDown _ 
    (ByVal sender As Object, ByVal e As System.EventArgs) _ 
    Handles ComboBox1.DropDown
    MessageBox.Show("Typical installation is strongly recommended.", _
    "Install information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

// Declare comboBox1.
System.Windows.Forms.ComboBox comboBox1;

// Initialize comboBox1.
private void InitializeComboBox()
{
    this.comboBox1 = new ComboBox();
    this.comboBox1.set_Location(new System.Drawing.Point(128, 48));
    this.comboBox1.set_Name("comboBox1");
    this.comboBox1.set_Size(new System.Drawing.Size(100, 21));
    this.comboBox1.set_TabIndex(0);
    this.comboBox1.set_Text("Typical");
    String installs[] = new String[] { "Typical", "Compact", "Custom" };
    comboBox1.get_Items().AddRange(installs);
    this.get_Controls().Add(this.comboBox1);
    // Hook up the event handler.
    this.comboBox1.add_DropDown(new System.EventHandler(
        comboBox1_DropDown));
} //InitializeComboBox

// Handles the comboBox1 DropDown event. If the user expands the  
// drop-down box, a message box will appear, recommending the
// typical installation.
private void comboBox1_DropDown(Object sender, System.EventArgs e)
{
    MessageBox.Show("Typical installation is strongly recommended.", 
        "Install information", MessageBoxButtons.OK, 
        MessageBoxIcon.Information);
} //comboBox1_DropDown

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft