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
Related
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;
static public String Titl;
Now in simple programming language suppose i got a dropdown list on my form. when i debugged i found out that dropdowlist with the id dropdownlist1 when written
dropdownlist1.text;
contains the value i selected in the dropdown menu. However when i did something like this:
titl = dropdownlist1.text;
i was not getting the value in the titl variable. Help needed. Thank You!
check this if you want text..then
title=dropdownlist1.selecteditem.text;
and if you want value then
title=dropdownlist1.selecteditem.value;
int selectedIndex=dropdownlist1.SelectedIndex;//get index
string selectedText=dropdownlist1.SelectedItem.Text;//get text
string selectedValue=dropdownlist1.SelectedItem.Value;//get value
Title = !string.IsNullorEmpty(dropdownlist1.SelectedItem.Text) ?
dropdownlist1.SelectedITem.Text : "";
Titl = dropdownlist1.SelectedValue;
Works! Thank You!
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();
I have a problem filling my ComboxBox. Here´s my code:
string[] test = { "Ausgaben", "Eingaben" };
foreach (string r in test)
{
cbEinAus.Items.Add(r);
}
The values from the string array are in the ComboBox, but the selected item is a empty string.
I want one of this two string from the array to be my selected item.
I already tried with the SelectedItem property, but that doesn´t work.
Maybe its a simple solution... Can anybody help me please?
Use:
cbEinAus.SelectedIndex = 0;
You can replace the 0 with the zero based index of whichever item you want to select.
Try
cbEinAus.Items[vbEinAus.SelectedIndex]
instead of SelectedItem. usually works for me.
Try setting the SelectedItem as cbEinAus.Items[0]
You can set the Text property.
cbEinAus.Text = test[0];
More examples can be found at How do I set the selected item in a comboBox to match my string?
cbEinAus.SelectedIndex = 0;
replace item to index
I filled up a combobox with the values from an Enum.
Now a combobox is text right? So I'm using a getter and a setter. I'm having problems reading the text.
Here's the code:
public BookType type
{
get
{
return (BookType)Enum.Parse(typeof(BookType), this.typeComboBox.Text);
}
set
{
this.typeComboBox.Text = value.ToString();
}
}
For some reason, this.typeComboBox.Text always returns an empty string when I select an item on the combobox.
Does someone see what I'm doing wrong?
EDIT: I have come to the conclusion that the problem lies in timing.
The point in time at which I summon the text is indeed after I changed the combobox, but still before that value is parsed as a value.
Problem fixed in a different way now, thanks for all the ideas.
string selectedText = this.ComboBox.GetItemText(this.ComboBox.SelectedItem);
The GetItemText method analyzes the item and returns the text of the bound to that item.
Set the DropDownStyle of the ComboBox to DropDownList. This will ensure that only the elements already in the list can be selected (no need to check that the text actually is a valid value).
Then if you use Enum.GetValues(typeof(BookType)) to fill the combobox then typeComboBox.SelectedItem property will be a value of BookType. So you can use this in the property getter and setter.
So to summarize. You don't have to bind the combobox to a list of text values as long as you use the DropDownList style. Use the SelectedItem property to get an item of the wanted type instead of checking the Text property.
Edit: You may have to check the SelectedItem property for null
The combobox starts at index -1, which has no text, thus an empty string: ""
I then change the index to a BookType that I need and then I get the wrong output...
Have you tried using this.typeComboBox.SelectedText instead of typeComboBox.Text ?
this.typeComboBox.SelectedItem.ToString()
I just created a simple windows form, and everything worked okay for me. Here is the code.
public enum Test
{
One, Two, Three
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.comboBox1.DataSource = Enum.GetNames(typeof(Test));
}
public Test Test
{
get
{
return (Test)Enum.Parse(typeof(Test), this.comboBox1.Text);
}
set
{
this.comboBox1.Text = value.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.Test.ToString());
this.Test = Test.Two;
MessageBox.Show(this.Test.ToString());
}
}