Tab Control and Combo Box (bound)

G

Guest

I have a combo box that is on a tab page in the tab control. The combo box
is bound to a dataset - I already know about the problem of having to set the
selected index twice to set it at -1. At runtime, when I go to another tab
page, the selected index is still at -1, but when I go back to the first tab
page where the combo box is, the selected index of the combo box is set back
to 0, and there is text in the combo box. The SelectedIndexChanged event
does NOT fire. Does anybody have a workaround for this?
 
G

Guest

I'm having the same problem. Did you find a solution David?

Below is some sample code to produce the problem.

Add Tab control to form.
Add 2 Tabs to tabcontrol.
Add Combo to tab
Paste the following code into form:

Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim aStatus() As Status = {New Status("OPN", "Open"), New
Status("CLS", "Closed")}
ComboBox1.DataSource = aStatus
ComboBox1.DisplayMember = "StatusName"
ComboBox1.ValueMember = "StatusCode"

'Clear selection.
ComboBox1.SelectedIndex = -1
End Sub

End Class

Public Class Status
Private m_strStatusCode As String
Private m_strStatusName As String

Public ReadOnly Property StatusCode() As String
Get
StatusCode = m_strStatusCode
End Get
End Property

Public ReadOnly Property StatusName() As String
Get
StatusName = m_strStatusName
End Get
End Property

Sub New(ByVal StatusCode As String, ByVal StatusName As String)
m_strStatusCode = StatusCode
m_strStatusName = StatusName
End Sub
End Class
 
G

Guest

One solution was to save off each selected index value in a variable when the
TabPage1.BindingContextChange event fires (which it does when going back to
the first tab), and reset the selected index of the combobox when a textbox
on the first tab page becomes visible again (when textboxcontrol.Visible=true
on the textboxcontrol.VisibleChanged event) - but when the tab control has
several pages, each with several combo boxes, this becomes rather
overwhelming. There are also other factors to take into account when the
form is first loaded and displayed which makes the need for more boolean flag
variables necessary.

The solution that I am using now is to add a blank record at the first
position (selected index =0), so that when mickymousesoft changes the
combobox index, a blank will still be shown in the combobox. Of course, this
means adding code to all the saving and maintenance sections for the lists
and watching the sort order of the lists very carefully.

MycGyver said:
I'm having the same problem. Did you find a solution David?

Below is some sample code to produce the problem.
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top