|
-
April 28th, 2011, 10:19 AM
#1
Adding value and display to a ComboBox
Hello,
When I add an item to a combo box like this:
combobox1.items.add("My Folder");
Then I get My Folder in the combobox but the value of this item is also My Folder.
How can I add a value AND a display item so for example when I ask for the value of the item that displays My Folder I get "c:\\users\\documents"
-
May 1st, 2011, 02:21 PM
#2
Re: Adding value and display to a ComboBox
you using visual studio?
it would be "C:\Users\currentuser\My Documents" note double slashes are not needed and would be incorrect.
You just want it to display this info so technically speaking it would be the "My Documents" folder rather then "My Folder"
I would suggest you use it in this manner to preserve good clean code from what I have read it is a very important part of coding is to keep everything easily readable.
Why not just have it say "My Documents C:\users\"(CurrentUser)""\My Documents"
From this you would have to have code which would get the current user and save it as a string
of the CurrentUser.
If you are just trying to show the location of a particular folder I do not know what you are trying to do really with your program so to be of more help I would need to know more about your program. There also may be a built in function for currentuser of windows I do not really know I have only wrote one program and read almost nothing on C# programming other then the basics the hello world program the res was trial and error and occasional google search, so I surely am not the most effective coder but I would reason to believe that with a little bit of reading I can now accomplish or create a program.
Last edited by infringer; May 1st, 2011 at 02:29 PM.
-
May 4th, 2011, 01:11 PM
#3
Re: Adding value and display to a ComboBox
There are some interesting strategies that you could use with the DataSource, DisplayMember, and ValueMember of a ComboBox when binding to ADO objects such as DataTable. I recommend searching for some tutorials that cover these concepts if your values come from a database.
However, based on your example there may be a simpler solution. Since ComboBox.Items.Add accepts an object, create a custom class that has a DisplayValue property and a DataValue property. See the following code snippet:
internal class DataItem
{
internal string DisplayValue { get; set; }
internal string DataValue { get; set; }
internal DataItem(string displayValue, string dataValue)
{
this.DataValue = dataValue;
this.DisplayValue = displayValue;
}
public override string ToString()
{
return this.DisplayValue;
}
}
Note that the ToString() method is overridden. This is necessary because the ComboBox will display the value returned by this method, and we want the DisplayValue property of DataItem to display.
To add items to your ComboBox, use code similar to the following:
comboBox1.Items.Add(new DataItem("My Folder", @"c:\users\documents"));
or
DataItem di = new DataItem("My Folder", @"c:\users\documents");
comboBox1.Items.Add(di);
To retrieve your value, do something like the following:
DataItem di = DataItemsComboBox.SelectedItem as DataItem;
string data = di.DataValue;
string display = di.DisplayValue;
-
May 4th, 2011, 01:25 PM
#4
Re: Adding value and display to a ComboBox
*Forgive the double post, but I was unable to edit the last one. Added code tags.
There are some interesting strategies that you could use with the DataSource, DisplayMember, and ValueMember of a ComboBox when binding to ADO objects such as DataTable. I recommend searching for some tutorials that cover these concepts if your values come from a database.
However, based on your example there may be a simpler solution. Since ComboBox.Items.Add accepts an object, create a custom class that has a DisplayValue property and a DataValue property. See the following code snippet:
Code:
internal class DataItem
{
internal string DisplayValue { get; set; }
internal string DataValue { get; set; }
internal DataItem(string displayValue, string dataValue)
{
this.DataValue = dataValue;
this.DisplayValue = displayValue;
}
public override string ToString()
{
return this.DisplayValue;
}
}
Note that the ToString() method is overridden. This is necessary because the ComboBox will display the value returned by this method, and we want the DisplayValue property of DataItem to display.
To add items to your ComboBox, use code similar to the following:
Code:
comboBox1.Items.Add(new DataItem("My Folder", @"c:\users\documents"));
or
Code:
DataItem di = new DataItem("My Folder", @"c:\users\documents");
comboBox1.Items.Add(di);
To retrieve your value, do something like the following:
Code:
DataItem di = DataItemsComboBox.SelectedItem as DataItem;
string data = di.DataValue;
string display = di.DisplayValue;
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|