.NET Forum / Windows Forms / WinForm General / December 2004
Populating a Combo Box with instances of a class - problem
|
|
Thread rating:  |
Martin Smith - 02 Dec 2004 00:01 GMT Hi! I'm feeling the burn of .NET; Still getting used to it :)
I'm trying to populate a combo box with instances of a class: Public Class Shortcut Private Name As String Private RealLocation As String
Public Sub New() Me.Name = "unknown" Me.RealLocation = "unknown" End Sub
Public Sub New(ByVal Name As String, ByVal Path As String) Me.Name = Name Me.RealLocation = Path End Sub
Public Function getName() As String Return (Me.Name) End Function Public Function getRealLocation() As String Return (Me.RealLocation) End Function
Public Overrides Function toString() As String Return (Me.Name) End Function End Class
My problem is the following code:
With cmb .ValueMember = "getRealLocation" .DisplayMember = "toString"
.Items.Add(New Shortcut("Label1", "Full Path1")) .Items.Add(New Shortcut("Label2", "Full Path 2")) .Items.Add(New Shortcut("Label3", "Full Path 3")) End With
When the combo list isn't activated, the combo box shows the results of the toString function, for the specific instance that is selected. However, when I click the drop-down button, it shows a list of blank entries. Selecting a blank entry again shows the results of the toString function of the instance I apparently selected.
How can I make each entry have a value whent he combobox drop-down list is showing, not just once a specific item in the list is selected. It's hard to make choices when there's only blank options =/.
Thanks again!
Robby - 02 Dec 2004 00:35 GMT The ValueMember and DisplayMember only work when the DataSource property is set. The ComboBox does not have its DataSource property set so it has nothing to call getRealLocation or toString on. When you select a blank space it defaults to the ToString method to display it as the selected item.
Robby
> Hi! I'm feeling the burn of .NET; Still getting used to it :) > [quoted text clipped - 47 lines] > > Thanks again! Martin Smith - 02 Dec 2004 03:12 GMT Hi =)
I was under the impression that the DataSource property was to specify a data set such as a collection or data table or or arraylist or whatnot... not a specific piece of data from each object in the combo box. I thought if you just added a bunch of strings to a combo box, when you pulled down the combo box to display multiple entries, it would show all the strings... not blank entries that correspond to the strings.
If I'm adding instances of a certain class to a combo box, what should the datasource be set as in order to display a specific data member of that instance, such as the data member "Name" of type string?
I'm still not catching the hang of this :)
Thanks again.
> The ValueMember and DisplayMember only work when the DataSource property > is set. The ComboBox does not have its DataSource property set so it has [quoted text clipped - 55 lines] >> >> Thanks again! Robby - 02 Dec 2004 03:32 GMT The String type is a basic .Net datatype and the ComboBox knows how to handel these types. Your Shortcut class is not a basic .Net datatype so the ComboBox does not know what to do with it except for calling its ToString method when it is the selected item. Your code tells the ComboBox what ValueMember and DisplayMember to use but you don't give it a data source to use them on. Put your objects in a data source assign that to the DataSource property. All will work well once that is done.
Robby
> Hi =) > [quoted text clipped - 72 lines] >>> >>> Thanks again! Martin Smith - 02 Dec 2004 03:39 GMT Thank you very much for all of your assistance.
I'm doing this now:
Dim shortcutList As New ArrayList
shortcutList.Add(New Shortcut("Label1", "Full Path1")) shortcutList.Add(New Shortcut("Label2", "Full Path 2")) shortcutList.Add(New Shortcut("Label3", "Full Path 3"))
With cmbGo .DataSource = shortcutList .ValueMember = "getRealLocation" .DisplayMember = "toString" End With
However, I also tried just adding strings to a combo box... such as cmb.Items.Add("testing") and it still appears blank until I select it =(
I'm so befuddled =(
> The String type is a basic .Net datatype and the ComboBox knows how to > handel these types. Your Shortcut class is not a basic .Net datatype so [quoted text clipped - 82 lines] >>>> >>>> Thanks again! Matt Berther - 02 Dec 2004 06:25 GMT Hello Martin,
First off, I dont believe you can set ValueMember or DisplayMember to functions. I believe they need to be properties.
Try this (in C#):
class Shortcut { private string name; private string realLocation;
public Shortcut(string name, string path) { this.name = name; this.realLocation = path; }
public string Name { get { return name; } } public string RealLocation { get { return realLocation; } } }
ArrayList shortcutList = new ArrayList(); shortcutList.Add(new Shortcut("Label1", "Full Path1")); shortcutList.Add(new Shortcut("Label2", "Full Path2")); shortcutList.Add(new Shortcut("Label3", "Full Path3"));
cmbGo.DataSource = shortcutList; cmbGo.DisplayMember = "Name"; cmbGo.ValueMember = "RealLocation";
If you dont assign a datasource, display/value member should not be used. So, if you want to use items.add, make sure you remove those two properties.
Hope this helps!
-- Matt Berther http://www.mattberther.com
> Thank you very much for all of your assistance. > [quoted text clipped - 100 lines] >>>>> >>>>> Thanks again! Martin Smith - 02 Dec 2004 18:36 GMT Hey again --
Just an update. I found a page
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_2121 8837.html
where someone else is having the exact same problem I have. I can't view the answers as it is subscription-required :(
But basically, that post explains my problem pretty well.
- Martin
> Hello Martin, > [quoted text clipped - 141 lines] >>>>>> >>>>>> Thanks again! Claes Bergefall - 02 Dec 2004 09:16 GMT Actually, you don't have to specify a DataSource to use DisplayMember (for ValueMember you need it though)
/claes
> The ValueMember and DisplayMember only work when the DataSource property is > set. The ComboBox does not have its DataSource property set so it has > nothing to call getRealLocation or toString on. When you select a blank > space it defaults to the ToString method to display it as the selected item. > > Robby Claes Bergefall - 02 Dec 2004 09:17 GMT 1. DisplayMemebr and ValueMember must be set to properties, not functions like you're doing
2. You're not useing databinding so why bother with DisplayMember and ValueMember anyway? Just get rid of them and it should work just fine
/claes
> Hi! I'm feeling the burn of .NET; Still getting used to it :) > [quoted text clipped - 47 lines] > > Thanks again! Martin Smith - 02 Dec 2004 16:37 GMT Hi there ---
My classes uses properties now, not functions, and the ValueMember and DisplayMember are properly assigned to those properties (I think).
I guess my real problem here is even if I add a plain String to a combo box, it doesn't show up when I open the drop-down list, only once the item is selected. If I just start a new project, and cmb.Items.Add("item1"), and so forth, they aren't listed in the box when I drop it down. That is the problem.
I'm tempted to go rewrite this in Java or worse... eeek!
Thanks.
> 1. DisplayMemebr and ValueMember must be set to properties, > not functions like you're doing [quoted text clipped - 62 lines] >> >> Thanks again! Matt Berther - 02 Dec 2004 17:18 GMT Hello Martin,
It sounds to me as if you're adding strings once you have set ValueMember and DisplayMember properties.
It's not going to easily do that. A String doesnt have the same properties as your Shortcut object.
Now, if you really need to do that, remove both the ValueMember and DisplayMember settings. At this point, the combobox will rely on the ToString() method.
-- Matt Berther http://www.mattberther.com
> Hi there --- > [quoted text clipped - 83 lines] >>> >>> Thanks again! Martin Smith - 02 Dec 2004 17:29 GMT Hey!
It's even simpler than that. A new form with nothing except:
cmbTest.Items.Add("test") cmbTest.Items.Add("test 2") ...etc
When I pull down the combo box, they're blank. Without setting any datasource, no anything. Just pure strings. No data binding. When I click on a blank entry, "test whatever" shows up.
Strings added to a combo box with no other things coded shouldn't be blank, right?
- Martin
> Hello Martin, > [quoted text clipped - 99 lines] >>>> >>>> Thanks again! Matt Berther - 03 Dec 2004 08:00 GMT Hello Martin,
Sounds like there may be some other things going on there... Can you post the smallest complete working sample that reproduces this problem?
-- Matt Berther http://www.mattberther.com
> Hey! > [quoted text clipped - 117 lines] >>>>> >>>>> Thanks again! Stuart Celarier - 04 Dec 2004 02:11 GMT Martin,
Here is some minimal code that creates a ComboBox and populates it with strings. When I run it, I pull down the ComboBox and see the strings. Does this work for you?
Cheers, Stuart Celarier, Fern Creek
---- ComboBox sample ----
Public Class Form1 Inherits System.Windows.Forms.Form
Public Sub New() MyBase.New() InitializeComponent() ComboBox1.Items.Add("Curly") ComboBox1.Items.Add("Moe") ComboBox1.Items.Add("Larry") End Sub
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ComboBox1 = New System.Windows.Forms.ComboBox Me.SuspendLayout() 'ComboBox1 Me.ComboBox1.Location = New System.Drawing.Point(24, 24) Me.ComboBox1.Name = "ComboBox1" Me.ComboBox1.Size = New System.Drawing.Size(121, 21) 'Form1 Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.ComboBox1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False)
End Sub
End Class
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|