How to re-acquire the Text from a combo box

  • Thread starter Thread starter Mark L. Breen
  • Start date Start date
M

Mark L. Breen

Hello Guys and Galls,

I use combos on my forms.

The code to initialise the combos is as follows

Dim dsPIDTypes As DataSet
dsPIDTypes = PartDB.GetPIDTypes ' Returns a dataset object
cboPIDType.DataSource = dsPIDTypes
cboPIDType.DisplayMember = "tlkpPIDType.PT_Type"
cboPIDType.ValueMember = "tlkpPIDType.PT_ID"


I then have an object that stores the data that is currently displayed on
the form, it stores the selected value from the combo's, ie,
mycombobox.SelectedValue

When the user clicks the add button, I make the text of the combo to be = ""

However when the user clicks cancel, I want to revert to the values that
were previously displayed.

For the text boxes, this is easy,

Me.txtDate = objFormData.Date
Me.txtQty = objFormData.Qty

But then I need to update the text displayed in the combo box.

I can easily do

Me.MyComboBox.SelectedValue = objForm.Product Id

but I want the combo box to display the matching Selected Text.

Is there a way to do this without having to write a query to filter for that
Product Id ??

I guess what I want is something like

Me.MyComboBox.Text = Me.MyComboBox.Text that corresponds to the selected
value

Any suggestions?

Marko
 
If you are relying on the SelectedValue of the combo box to be set the best
way to perform your action is to use the SelectedIndex property of the combo
box. When the SelectedIndex property changes the combo updates itself as
though that item had been "selected" by the user (it even fires the event
associated with it), in other words both the SelectedValue and the
SelectedItem are updated in tandum.

Typically the easiest way to get the correct SelectedIndex is to enumerate
the datasource of the combo box and determine the appropriate index to use.
Remeber that SelectedIndex is the index of the ITEM IN THE LIST, it is NOT
the index or key you may have bound using the ValueMember.

For example:

say the data source looks like this (where column A is the Primary Key):
A | B
1 | Joe
3 | Jim
5 | Jon
6 | Jaq

If you wanted "Jim" displayed in the combo box you would set the
SelectedIndex property to 1, Because "Jim" is the second item in the list.
SelectedIndex begins counting at Zero (0), hence the items are indexed by
the combo list as 0, 1, 2, 3 and so on.

Thanks,

Matt
 
Hi,

Mark L. Breen said:
Hello Guys and Galls,

I use combos on my forms.

The code to initialise the combos is as follows

Dim dsPIDTypes As DataSet
dsPIDTypes = PartDB.GetPIDTypes ' Returns a dataset object
cboPIDType.DataSource = dsPIDTypes
cboPIDType.DisplayMember = "tlkpPIDType.PT_Type"
cboPIDType.ValueMember = "tlkpPIDType.PT_ID"


I then have an object that stores the data that is currently displayed on
the form, it stores the selected value from the combo's, ie,
mycombobox.SelectedValue

When the user clicks the add button, I make the text of the combo to be =
""

However when the user clicks cancel, I want to revert to the values that
were previously displayed.

For the text boxes, this is easy,

Me.txtDate = objFormData.Date
Me.txtQty = objFormData.Qty

But then I need to update the text displayed in the combo box.

I can easily do

Me.MyComboBox.SelectedValue = objForm.Product Id

but I want the combo box to display the matching Selected Text.

It will show the matching display value when you assign the (old)
SelectedValue. But only if you don't use CombBox.Text="" to clear it like
you're doing, instead to clear it you must use:

ComboBox.SelectedIndex = -1 (Twice in NET1.1 because of a bug.)


HTH,
Greetings
 
Hello Guys,

I got it working anyway, I will post the results here and see what comments
you have to make

Please note a couple of points,

I had to use the GetItemText method of the combo box to retrive the text
from the value
I then have to pass back into the method it's own selected item, this seems
confusing to me, should it already know it? Especially in a combo box where
there is only one selected item
For what ever reason, if I do not initialise the Selected Text first, I get
two sets of text.

The confirms what I saw in debug mode, I could see the selected text was
correct in debug mode, but the form would not display the selected text.

Here is the routine that updates my form

Private Sub S_PopulateControls()
Me.txtSessionId.Text = mobjRecord.SessionId

S_RefreshComboBox(mobjRecord.PIDTypeId, Me.cboPIDType)

S_RefreshComboBox(mobjRecord.LocalisedTestId, Me.cboLocalisedText)

Me.txtPartNum.Text = mobjRecord.PartNum

End Sub


and here is the code for RefreshCombo

Private Sub S_RefreshComboBox(ByVal int As Integer, ByVal cbo As ComboBox)

cbo.SelectedValue = int

cbo.SelectedText = ""

cbo.SelectedText = cbo.GetItemText(cbo.SelectedItem)

End Sub



Finally, an alternative to this problem is

Dim strText As String
strText = Me.cboPIDType.SelectedText
Me.cboPIDType.SelectedText = ""
Me.cboPIDType.SelectedText = strText

Why do I have to set selected text to "" before it can display it?

Thanks all for any comments, I hope this might be of some help to you guys.

Mark
 
Hello Matt,

Thanks for that,

I have just found another, my third solution.

This seems silly, but now I have

Me.cboPIDType.SelectedText = Me.cboPIDType.SelectedText

I know, I am saying let 1=1, but this action seems to refresh the combo.

Mark
 
Back
Top