ComboBox (cbo)
Combobox - Allows the user to either select an item from a drop-down box or enter a different item. |
Both the ComboBox and ListBox controls are derived from the ListControl class
Examples
Adding to single column
this.ComboBox1.Items.Add "Item 1";
this.ComboBox1.Items.Add "Item 2";
this.ComboBox1.SelectedIndex = 0;
Currently selected item
Obtaining the currently selected item in a combo box.
System.Windows.Forms.MessageBox.Show(this.cboComboBox1.Value) ;
Select a value
You can use FindString to return the index of the first item that starts with a specified string.
this.cboComboBox1.Text = "Item 2";
this.cboComboBox1.SelectedIndex = this.cboComboBox1.FindString("Item 1");
Check if a value has been chosen
if (this.cboMyComboBox.SelectedItem == null)
{}
DataSource
objDataTable = new System.Data.DataTable;
objDataTable.TableName = "Applications";
objDataTable.Columns.Add("ArticleApplication", GetType(System.String));
foreach (objDataRow in clsDatabase.mobjDataSet.Tables("ApplicationNames").Rows)
{
objDataRowNew = objDataTable.NewRow();
objDataRowNew("ArticleApplication") = objDataRow("ArticleApplication");
objDataTable.Rows.Add(objDataRowNew);
}
this.cboComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
this.cboComboBox1.DataSource = objDataTable;
this.cboComboBox1.DisplayMember = "ArticleApplication";
Properties
AllowSelection | Gets a value indicating whether the list enables selection of list items. (Inherited from ListControl.) |
AutoCompleteMode | (Added in 2.0) Gets or sets an option that controls how automatic completion works for the ComboBox. |
AutoCompleteSource | Gets or sets a value specifying the source of complete strings used for automatic completion. |
DataManager | Gets the CurrencyManager associated with this control. (Inherited from ListControl.) |
DataSource | Gets or sets the data source for this ComboBox. |
DisplayMember | Gets or sets the property to display for this ListControl. (Inherited from ListControl.) |
DropDownHeight | Gets or sets the height in pixels of the drop-down portion of the ComboBox. |
DropDownStyle | Gets or sets a value specifying the style of the combo box. |
DropDownWidth | Gets or sets the width of the of the drop-down portion of a combo box. |
DroppedDown | Gets or sets a value indicating whether the combo box is displaying its drop-down portion. |
FlatStyle | Gets or sets the appearance of the ComboBox. |
Focused | Overridden. Gets a value indicating whether the ComboBox has focus. |
FormatString | Gets or sets the format-specifier characters that indicate how a value is to be displayed. (Inherited from ListControl.) |
FormattingEnabled | Gets or sets a value indicating whether formatting is applied to the DisplayMember property of the ListControl. (Inherited from ListControl.) |
IntegralHeight | Gets or sets a value indicating whether the control should resize to avoid showing partial items. |
ItemHeight | Gets or sets the height of an item in the combo box. |
Items | Gets an object representing the collection of the items contained in this ComboBox. |
MaxDropDownItems | Gets or sets the maximum number of items to be shown in the drop-down portion of the ComboBox. |
MaxLength | Gets or sets the number of characters a user can type into the ComboBox. |
PreferredHeight | Gets the preferred height of the ComboBox. |
SelectedIndex | Sets the index specifying the currently selected item. Problems when populating before control is displayed. Overridden. |
SelectedItem | Sets the currently selected item in the ComboBox. There is no selection when the focus is taken away from the combobox. For example when you press a button nothing will be selected. |
SelectedText | Sets the text that is currently selected in the editable portion of a ComboBox. There is no selection when the focus is taken away from the combobox. For example when you press a button nothing will be selected. |
SelectedValue | Sets the value of the member property specified by the ValueMember property. Problems when populating before control is displayed. |
SelectionLength | Gets or sets the number of characters selected in the editable portion of the combo box. |
SelectionStart | Gets or sets the starting index of text selected in the combo box. |
Sorted | Gets or sets a value indicating whether the items in the combo box are sorted. |
Text | Overridden. Gets or sets the text associated with this control. |
ValueMember | Gets or sets the property to use as the actual value for the items in the ListControl. (Inherited from ListControl.) |
Methods
Add | Adds an item to the list of items |
AddRange | Adds an array of items to the list of items |
AddItemsCore | Adds the specified items to the combo box. |
BeginUpdate | Maintains performance when items are added to the ComboBox one at a time. |
Dispose | Overloaded. Releases the resources used by the ComboBox. |
EndUpdate | Resumes painting the ComboBox control after painting is suspended by the BeginUpdate method. |
FilterItemOnProperty | Overloaded. Returns the current value of the ListControl item, if the item is a property of an instance of the ListControl class. (Inherited from ListControl.) |
FindString | Overloaded. Finds the first item in the ComboBox that starts with the specified string. |
FindStringExact | Overloaded. Finds the item that exactly matches the specified string. |
GetItemHeight | Returns the height of an item in the ComboBox. |
GetItemText | Returns the text representation of the specified item. (Inherited from ListControl.) |
OnClick | Raises the Click event. (Inherited from Control.) |
RefreshItem | Overridden. Refreshes the item contained at the specified location. |
RefreshItems | Overridden. Refreshes all ComboBox items. |
ResetText | Overridden. |
Select | Overloaded. |
SelectAll | Selects all the text in the editable portion of the ComboBox. |
SetBoundsCore | Overridden. Sets the size and location of the ComboBox. |
ToString | Overridden. Returns a string that represents the ComboBox control. |
Events
BackgroundImageChanged | Occurs when the value of the BackgroundImage property changes. |
BackgroundImageLayoutChanged | Occurs when the BackgroundImageLayout property changes. |
DrawItem | Occurs when a visual aspect of an owner-drawn ComboBox changes. |
DropDown | Occurs when the drop-down portion of a ComboBox is shown. |
DropDownClosed | Occurs when the drop-down portion of the ComboBox is no longer visible. |
DropDownStyleChanged | Occurs when the DropDownStyle property has changed. |
Format | Occurs when the control is bound to a data value. (Inherited from ListControl.) |
FormatInfoChanged | Occurs when the value of the FormatInfo property changes. (Inherited from ListControl.) |
FormatStringChanged | Occurs when value of the FormatString property changes (Inherited from ListControl.) |
FormattingEnabledChanged | Occurs when the value of the FormattingEnabled property changes. (Inherited from ListControl.) |
MeasureItem | Occurs each time an owner-drawn ComboBox item needs to be drawn and when the sizes of the list items are determined. |
SelectedIndexChanged | Occurs when the SelectedIndex property has changed. |
SelectedValueChanged | Occurs when the SelectedValue property changes. (Inherited from ListControl.) |
SelectionChangeCommitted | Occurs when the selected item has changed and that change is displayed in the ComboBox. |
TextUpdate | Occurs when the control has formatted the text, but before the text is displayed. |
ValueMemberChanged | Occurs when the ValueMember property changes. (Inherited from ListControl.) |
private void cboComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox myCombo;
myComboBox = (ComboBox)sender;
myComboBox = this.cboComboBox1;
string theValue = (string)myCombo.SelectedItem;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.comboBox1.ValueMember = "Property_DayID";
this.comboBox1.DisplayMember = "Property_DayDisplayName";
DayDisplayName oDay;
oDay = new DayDisplayName("1", "Mon", "Monday");
this.comboBox1.Items.Add(oDay);
oDay = new DayDisplayName("2", "Tue", "Tuesday");
this.comboBox1.Items.Add(oDay);
oDay = new DayDisplayName("3", "Wed", "Wednesday");
this.comboBox1.Items.Add(oDay);
//this.comboBox1.Items.Add("Wednesday");
this.comboBox1.Text = "Tuesday";
//this.comboBox1.SelectedIndex = this.comboBox1.FindString("Tuesday");
}
private void btnComboBox_GetValue_Click(object sender, EventArgs e)
{
string selectedvalue;
selectedvalue = this.comboBox1.Text;
System.Windows.Forms.MessageBox.Show(selectedvalue);
}
}
public class DayDisplayName
{
public string Property_DayID { get; set; }
public string Property_DayName { get; set; }
public string Property_DayDisplayName { get; set; }
public DayDisplayName(
string property_DayID,
string property_DayName,
string property_DayDisplayName)
{
this.Property_DayID = property_DayID;
this.Property_DayName = property_DayName;
this.Property_DayDisplayName = property_DayDisplayName;
}
}
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext