I am building a String and the code looks like
String status = "The status of my combobox is " + comboBoxTest.SelectedText
I am using WinForm in VS2010
The result looks like
"The status of my combobox is "
I think you want to use
String status = "The status of my combobox is " + comboBoxTest.Text
SelectedText property from MSDN
Gets or sets the text that is selected in the editable portion of a
ComboBox.
while Text property from MSDN
Gets or sets the text associated with this control.
From the documentation:
You can use the SelectedText property to retrieve or change the currently selected text in a ComboBox control. However, you should be aware that the selection can change automatically because of user interaction. For example, if you retrieve the SelectedText value in a button Click event handler, the value will be an empty string. This is because the selection is automatically cleared when the input focus moves from the combo box to the button.
When the combo box loses focus, the selection point moves to the beginning of the text and any selected text becomes unselected. In this case, getting the SelectedText property retrieves an empty string, and setting the SelectedText property adds the specified value to the beginning of the text.
I face this problem 5 minutes before.
I think that a solution (with visual studio 2005) is:
myString = comboBoxTest.GetItemText(comboBoxTest.SelectedItem);
Forgive me if I am wrong.
I think you dont need SelectedText but you may need
String status = "The status of my combobox is " + comboBoxTest.Text;
To get selected item, you have to use SELECTEDITEM property of comboBox. And since this is an Object, if you wanna assign it to a string, you have to convert it to string, by using ToString() method:
string myItem = comboBox1.SelectedItem.ToString(); //this does the trick
Try this:
String status = "The status of my combobox is " + comboBoxTest.text;
Here's how I would approach the problem, assuming you want to change the text of say, a label
private void comboBoxtest_SelectedIndexChanged(object sender, EventArgs e)
{
var combotext = comboBoxtest.Text;
var status = "The status of my combo box is" + combotext;
label1.Text = status;
}
All of the previous answers explain what the OP 'should' do. I am explaining what the .SelectedText property is.
The .SelectedText property is not the text in the combobox. It is the text that is highlighted. It is the same as .SelectedText property for a textbox.
The following picture shows that the .SelectedText property would be equal to "ort".
If you bind your Combobox to something like KeyValuePair, with properties in the constructor like so...:
DataSource = dataSource,
DisplayMember = "Value",
ValueMember = "Key"
so dataSource is of type KeyValuePair...
You end up with having to do this...
string v = ((KeyValuePair)((ComboBox)c).SelectedItem).Value;
(I had a Dynamic form - where c was of type Control - so had to cast it to ComboBox)
If you just want to know the text in the ComboBox with the editable text box (or the ComboBoxStyle.DropDown style) you can use this:
string str = comboBox.SelectedItem != null ?
comboBox.GetItemText(comboBox.SelectedItem) : comboBox.Text;
or try this code
String status = "The status of my combobox is " + comboBoxTest.SelectedItem.ToString();
Related
I want to get data from selected line in List Box. I use command:
string selected = ListBox1.SelectedItems[0].ToString();
But the result is:
ListVievItem: {here is correct value}
What should i do with this: "ListVievItem: {}"
string urItemText = ListBox1.SelectedItem.Text;
http://msdn.microsoft.com/fr-fr/library/system.windows.forms.listbox_properties(v=vs.80).aspx
EDIT As suggested by John Willemse, ListBox can't have ListViewItems so it seems like this question relates to ListView rather than to a ListBox so the code in the answer is changed accordingly.
When you call it like this listView1.SelectedItems[0].ToString(); you are actually calling the ToString() method of the ListViewItem object which gives the unwanted result (first prints the class name and then the value). Each ListViewItem object has Text property from which you can get its text.
string selected = listView1.SelectedItems[0].Text;
Try something like this:
string selected = ListBox1.SelectedItems[0].Text;
Did you Try just: ListBox1.SelectedItem.Value
I had a databound combobox in my windows form I populate it by a function deptload() IN FORM LOAD
public void DeptcomboLoad()
{
DataTable dt = depttrans.getDeptName();
Cmb_Department.DataSource = dt;
Cmb_Department.DisplayMember = "DepartmentName"; //CHAR
Cmb_Department.ValueMember = "DepartmentPK"; //INT
}
Now when an employee of a department (say accounts DepartmentName="Accounts " , DepartmentPK=23 ) login I want the ComboBox text to be selected as "acounts "
and when I go to get the selected value of the ComboBox I should get 23
I tried
Cmb_Department.selectedtext="Accounts"
Cmb_Department.Text="Accounts"
but its not giving the selected value
Can anyone give a suggestion
Instead of trying to put a value INTO the combobox, try to GET the SelectedItem like this:
string txt= Cmb_Department.SelectedItem.Text
or just:
string txt= Cmb_Department.SelectedText
To change selected value of the combobox you can use
SelectedItem property or SelectedIndex.
Index must be exact number in your data sourse, and Item must be exact object from datasource
You can get it to select the right item by issuing something like this:
Cmb_Department.SelectedValue = 23;
Where 23 comes from some other variable, maybe on another object, maybe from a local variable, whatever works in your case.
Now, to get the selected value you can use this statement:
var val = Cmb_Department.SelectedValue;
To get the selected text (which would be the text associated with the value):
var text = ((DataRow)Cmb_Department.SelectedItem)["DepartmentName"];
The reason I'm prescribing the aforementioned is because the SelectedText property is volatile, and the Text property doesn't always work based on how the DropDownStyle is set.
However, some would probably argue to get the same as the aforementioned you could issue this statement:
var text = Cmb_Department.Text;
i have bounded DataSource to a combox box and then set this Code
Combox1.ValueMember = "CapacityID";
Combox1.DisplayMember = "Capacitys";
it show Data with no problem , but when i want gain selectedtext it returned me "" and using selectedItem , return name of combo box !
selectedvalue return correct data .
Combox1.SelectedItem.ToString(); //return "Combox1"
Combox1.SelectedValue.ToString(); //Work Correctly
Combox1.SelectedText.ToString(); // return ""
Combox1.SelectedItem retunrs you selected ListItem object not text value of selected item
its should be like :
ListItem li = Combox1.SelectedItem;
or
Object selectedItem = comboBox1.SelectedItem;
MessageBox.Show("Selected Item Text: " + selectedItem.ToString() );
From MSDN : http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem.aspx
Combox1.SelectedText - Check Msdn for this : http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedtext.aspx
FROM MSdn why its return empty string - if you retrieve the SelectedText value in a button Click event handler, the value will be an empty string. This is because the selection is automatically cleared when the input focus moves from the combo box to the button.
ComboBox.Text.Tostring() returned selected text and solved my problem
String status = "The status of my combobox is " + comboBoxTest.Text
SelectedText property from MSDN
Gets or sets the text that is selected in the editable portion of a ComboBox.
while Text property from MSDN
Gets or sets the text associated with this control.
Use
Combox1.SelectedItem.Text // To get SelectedText
Combox1.SelectedItem.Value // To get SelectedValue
Instead of
Combox1.SelectedItem.ToString()
So altough, your question is not really gramatically clear, to get the calue of your selected item always use
Combox1.SelectedValue
Why?
Because:
Combox1.SelectedItem
returns a string that represents the currently selected text in the combo box. If DropDownStyle is set to DropDownList, the return value is an empty string ("").
Sir/madam now my problem is this that I want to filter the Grid View of a page using a Drop Down list and a text box.
I mean to say like we write a SQL such as:
Select * from student where roll_no = 101;
Right,
Now I what that the column (roll_no in above statement) should be selected by the drop down list and the value (101 in the above statement) should be entered by the Text box.
In short I want to populate my grid view using Drop Down list and the value of text box by clicking a button..
For developing i am using dataset and table adapters.
Please, help me for this..
I use a drop-down list (combo-box) and a textbox to filter my DataGridView the following way and I think this is what you are looking for.
First, populate your DataGridView. You state you are using a DataSet and TableAdapters. I am guessing that you are using a BindingSource to tie your Data to your DataGridView. If that is the case, then you can Filter your data via the BindingSource.
My set up is similar to this:
My combobox contains the fields that I want to use in my Filter and the textbox is the value that I will be applying. The values in the combobox are user-friendly names so they will understand which field they are filtering on.
The code to apply the filter is:
private void ApplyFilter()
{
var filterEntered = FilterTextBox.Text.Trim().ToLower();
MyBindingSource.RemoveFilter(); // remove previous filter
string filterText = string.Empty;
string filterComboText = string.Empty;
switch (FilterComboBox.Text)
{
case "Profile":
filterComboText = "TSProfile"; // column name in the query
break;
case "User Id":
filterComboText = "TSUserId";
break;
case "Center":
filterComboText = "TSCenter";
break;
case "Prefix":
filterComboText = "TSPrefix";
break;
}
filterComboText = filterComboText + " = '";
filterText += (string.IsNullOrEmpty(filterComboText) ? string.Empty : filterComboText);
filterText += (!string.IsNullOrEmpty(filterText) && !string.IsNullOrEmpty(filterEntered) ? filterEntered + "'" : string.Empty);
MyBindingSource.Filter = filterText;
}
Basically what it is doing, is getting the text name of the combo-box and then the text in the textbox and applying the Filter to the BindingSource.
MSDN has an article on Filtering thats contains full sample code.
The one thing that I recommend is to provide the user with a way to easily remove the filter, I use a Remove Filter button.
it would be helpful if you showed us a little code first..
you could try something like this tho:
in your codebehind, add items to your dropdownlist.
List<yourObject> list = new List<yourObject>();
foreach (yourObject i in list)
{
DropdownList1.Items.Add(new ListItem("" i.name, "" + i.id));
}
im just giving an example here, i.name could be the name of a certain student, i.id would be the id associated with that given student.
Make sure you have the autopostback attribute of your dropdownlist set to true, like this:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
Then in the selected Index Changed event of your dropdownlist, do the following:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
yourDataControl.DataSource = someMethod(Convert.toInt32(DropDownList1.SelectedValue));
yourDatacontrol.DataBind();
}
as i said, im not entirely sure what you're trying to do or how you're trying to do it.
the way i'm describing, you dont need the textbox to enter a certain value, by selecting an item in the dropdownlist you wil automatically get a value: in this case the ID associated with the selected item in the dropdownlist.
I know now normally you can get the value of a text input using the following:
txtName.Text
But because my input is inside of a LoginView I am using FindControl like this:
LoginView1.FindControl("txtComment")
This successfully find the text input but returns its type rather than the value. Adding the Text function at the end does not work.
Try casting that Control to TextBox. FindControl returns a Control which doesn't have the Text property
TextBox txtName = LoginView1.FindControl("txtComment") as TextBox;
if (txtName != null)
{
return txtName.Value;
}
It has been a while since I used the controls, but i believe it is:
string text = ((TextBox)LoginView1.FindControl("txtComment")).Text;