kejpa Posted June 23, 2004 Posted June 23, 2004 Hi, I'm furious! :mad: After finding out that you don't have the ItemValue property any more which disables you from hardcoding the Item/Itemvalue to a combobox I decided to go with the samples in the help and assign a arraylist to the datasource. When I read my database I get the ItemValue for the combobox. How the f**ing h*ll am I to set the item shown in the combobox? cboType.SelectedValue = oItm.Type will not work, SelectedValue is nothing! Plz help and exsqueeze my dirty language in this post. /Kejpa Quote
pelikan Posted June 23, 2004 Posted June 23, 2004 repeat after me - OOOOM.... your frustration is baseless. With the Managed Combo control you actually get a great deal more flexibility than you had in the dark ages of VB6 or C++. Then, you had an ItemValue field for each list element (a DWORD) - it's still there in the underlying Windows control, but there are better, type safe ways to accomplish the kinds of things this field was intended for (back in the days when windows were written with giant switch statements). It took me a while to get used to incredible binding possibilities of managed controls, so I sympathize with your confusion (except for the "hard coding" bit - never a good idea) Here's what you have to work with: DataSource DisplayMember ValueMember SelectedValue Think about what happens when you set the first 3 properties to a table that looks like this: itemkey, displayValue Then bind the SelectedValue property to something with: cbo.DataBindings.Add("SelectedValue", SomeOtherTable, "itemkey") As to the hard coding bit, if all you want is a rigid (itemValue:itemString), remember that combobox items are objects - not strings. you can define a Pair object with two fields (itemData, itemString) + overridden ToString method which simply returns the itemString. :eek: Quote IN PARVUM MULTUM
Mutley Posted June 23, 2004 Posted June 23, 2004 Hi, I'm furious! :mad: After finding out that you don't have the ItemValue property any more which disables you from hardcoding the Item/Itemvalue to a combobox I decided to go with the samples in the help and assign a arraylist to the datasource. When I read my database I get the ItemValue for the combobox. How the f**ing h*ll am I to set the item shown in the combobox? cboType.SelectedValue = oItm.Type will not work, SelectedValue is nothing! Plz help and exsqueeze my dirty language in this post. /Kejpa Hello kejpa, You can use the cboType.Items.Add(oItm.Type) Hope it helped Quote
kejpa Posted June 23, 2004 Author Posted June 23, 2004 You can use the cboType.Items.Add(oItm.Type)Hope it helped No, it's not putting the items into the combobox that's the problem. It's not getting the currently selected either. It's selecting the item based on a value, which is not the text of the items. When initializing the Combo I do... Dim oType As New ArrayList() ' ccboItem contains members Item, Value oType.Add(New ccboItem(30, "0..20mA")) oType.Add(New ccboItem(31, "4..20mA")) cboType.DataSource = oADDAType cboType.DisplayMember = "Value" cboType.ValueMember = "Item" cboType.SelectedIndex = -1 Reading my database gives me the value of 31 for type cboType.SelectedValue = oItm.Type ' oItm.Type=31 And I want to see "4..20mA" in the combobox repeat after me - OOOOM.... your frustration is baseless. OOOOM?!?! Typo? OOP.... OOP.... OOP... Still pissed. :mad: /Kejpa Quote
pelikan Posted June 23, 2004 Posted June 23, 2004 (edited) Just grokked your flok. (by the way, did mean OOOM ) Sounds like you want a simple lookup combo fella. Proof-of-concept scenario: Table1 ("Item", "Value") Table2 ("Blah", Blah1", Blah2", "Item") - "Item" is foreign key to Table1("Item") UI: ComboBox .DataSource = ds.Tables("Table1") .DisplayMember = "Value" .ValueMember = "Item" DataGrid .DataSource = ds .DataMember = "Table2" finally: cboTable1.DataBindings.Add("SelectedValue", ds, "Table2.Item") test it: change the selected row in the datagrid, the combo stays in sync because SelectedValue is 'simple bound' to Table2.Item get the CurrencyManager: myCurrencyManager = CType(me.BindingContext(ds.Tables("Table2")), CurrencyManager) change the SelectedValue via myCurrencyManager.Position - voila, SelectedIndex in combo changes. Edited June 23, 2004 by pelikan Quote IN PARVUM MULTUM
kejpa Posted June 24, 2004 Author Posted June 24, 2004 Just grokked your flok. (by the way, did mean OOOM ) Sounds like you want a simple lookup combo fella. Sure, simple it should be. Could you just complete the following code (merely test code) Sub Form_Load (...Removed) Dim oType As New ArrayList() ' ccboItem contains members Item, Value oType.Add(New ccboItem(30, "0..20mA")) oType.Add(New ccboItem(31, "4..20mA")) cboType.DataSource = oType cboType.DisplayMember = "Value" cboType.ValueMember = "Item" cboType.SelectedIndex = -1 End Sub Sub txtType_Leave(...Removed) cboType.SelectedValue= val(txtType) '<-- Here! End Sub When I enter 30 in the textbox I want the combo to show me 0..20mA When I enter 31 in the textbox I want the combo to show me 4..20mA New day, not so mad anymore. Yet. Regards /Kejpa Quote
LionelBrown Posted June 26, 2004 Posted June 26, 2004 Hope this helps... I'm a VB6 programmer that starting using .net 2003 a couple of days ago, so there may be a better way of doing what you want, but I'll give it a try. If I understand correctly, you have a combobox that displays text but has a key value behind the scenes (used to be able to put in itemdata). You want to select an item in the combobox by specifying the KEY value and not the text that is displayed by the combo. The combo will then display the correct text that is associated with the key value. I think you will have to loop through the combobox and check for a key match, then retrieve the index of the combo and use it to select the correct entry. Here's an example: 'I have a class with 2 properties that I use to load my combo: ' PCType is a string, and ID is an integer which stores the key Dim iIndex As Integer With cboPCType 'loop through the objects in the combobox For iIndex = 0 To .Items.Count - 1 'compare my "key" field of the combobox to the key I want to select 'NOTE: .ID returns my class ID property value, I think you use ITEM If CType(.Items.Item(iIndex).ID, Long) = (your value here) Then 'I've found the key, so set the combo to the correct entry and exit .SelectedIndex = iIndex Exit For End If Next If iIndex >= .Items.Count Then 'I've looped through every item in my combobox, and key not found .SelectedIndex = -1 End If End With Good Luck! Quote
LionelBrown Posted June 26, 2004 Posted June 26, 2004 One more thing that might be of use to you. I usually use the same stucture for my objects that I attach to a combobox. The members I use are: strText (a string to contain what I want to display), and lngID (a long integer to hold the key value). So I wrote a sub to handle selecting an item of a combobox based on a key, and use it for all my comboboxes: Private Sub SetCboValue(ByRef cbo As ComboBox, ByVal lID As Long) Dim iIndex As Integer With cbo For iIndex = 0 To .Items.Count - 1 If CType(.Items.Item(iIndex).lngID, Long) = lID Then .SelectedIndex = iIndex Exit For End If Next If iIndex >= .Items.Count Then .SelectedIndex = -1 End With End Sub I then call the sub anytime I want to select an item. For example: SetCboValue(cboPCtype, myDataRow("PcTypeID")) or SetCboValue(cboPCtype, 5) or SetCboValue(cboType, val(txtType)) I saw you were using an array, but I think this should work with a little tweaking Let me know if you have problems and I'll try it with arrays. Good Luck, Lionel Quote
LionelBrown Posted June 26, 2004 Posted June 26, 2004 Your Code Since I'm spending time trying to get a handle on the differences between vb6 and vb.net anyway, I thought I would give your situation a try as a learning experience. Here's your code (at least what I think you are trying to do): Public Class ccboItem Private m_Item As Long Private m_Value As String Public Sub New(ByVal theDisplay As String, ByVal theID As Long) m_Value = theDisplay m_Item = theID End Sub Public Overrides Function ToString() As String Return m_Value End Function Public ReadOnly Property Item() As String Get Return m_Item End Get End Property End Class Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim oType As New ArrayList ' ccboItem contains members Item, Value cboType.DisplayMember = "Value" cboType.ValueMember = "Item" oType.Add(New ccboItem("0..20mA", 30)) oType.Add(New ccboItem("4..20mA", 31)) cboType.DataSource = oType cboType.SelectedIndex = -1 End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtType.TextChanged End Sub Private Sub txtType_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtType.Leave 'cboType.SelectedValue = Val(txtType) '<-- Here! SetCboValue(cboType, Val(txtType.Text)) End Sub Private Sub SetCboValue(ByRef cbo As ComboBox, ByVal lID As Long) Dim iIndex As Integer With cbo For iIndex = 0 To .Items.Count - 1 If CType(.Items.Item(iIndex).Item, Long) = lID Then .SelectedIndex = iIndex Exit For End If Next If iIndex >= .Items.Count Then .SelectedIndex = -1 End With End Sub Hope this is what you need. Lionel Quote
kejpa Posted June 28, 2004 Author Posted June 28, 2004 Thanx Lionel, that's what I needed, I think it's the line CType(.Items.Item(iIndex).Item, Long) = lID that I wasn't finding. Item (after iIndex parenthis) isn't showing up in the intellisense and so I never thought I could use it there. Regards /Kejpa Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.