Calling SelectedIndexChanged from initial comboBox fill

T

tshad

In my VS 2003 Windows Forms page, when I initially fill my ComboBox
(SystemList), it goes to the SelectedIndexChanged event which calls the
Loademails() function. I then call it again in the Form1Load function.

How do I get it not to call it in the SelectedIndexChanged from the
Form1Load function? Normally, I want it to call it but not when I initally
fill the ComboBox.

*********************************************************
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Call LoadSystems()
Call Loademails(0)
End Sub

Private Sub LoadSystems()
Dim da As New SqlDataAdapter("COM_GET_SYSTEMS_SP", dbConn)
Dim ds As New DataSet
Dim dr As DataRow

da.Fill(ds, "Systems")
dr = ds.Tables("Systems").NewRow
dr(0) = 0
dr(1) = "All Emails"
ds.Tables("Systems").Rows.InsertAt(dr, 0)
SystemList.ValueMember = "id"
SystemList.DisplayMember = "name"
SystemList.DataSource = ds.Tables("Systems")
End Sub

Private Sub Loademails(ByVal systemID As Integer)
Dim da As New SqlDataAdapter("GetEmailMessages", dbConn)
da.SelectCommand.CommandType = CommandType.StoredProcedure
With da.SelectCommand.Parameters
If systemID <> 0 Then .Add("@SystemID", SqlDbType.Int).Value =
systemID
End With
Try
If Not ds.Tables("Emails") Is Nothing Then
ds.Tables.Remove("Emails")
da.Fill(ds, "Emails")
EmailDataGrid.DataSource = ds.Tables("Emails")
' EmailDataGrid.AllowSorting = False
EmailDataGrid.ReadOnly = True
Catch ex As Exception
Dim temp As String = ex.Message
End Try
End Sub

Private Sub SystemList_SelectedIndexChanged(ByVal sender As
System.Object, _
ByVal e As System.EventArgs) Handles
SystemList.SelectedIndexChanged
Loademails(sender.SelectedValue)
End Sub
**********************************************************************

Thanks,

Tom
 
G

Guest

Either;

Use RemoveHandler and AddHandler to turn the remove the event handler for
SelectIndexChanged while you do your thing in the load event then add the
event handler back in before you exit the load event.

Use a form scoped variable to detect when the load event is being processed.
 
H

Harry

tshad said:
In my VS 2003 Windows Forms page, when I initially fill my ComboBox
(SystemList), it goes to the SelectedIndexChanged event which calls the
Loademails() function. I then call it again in the Form1Load function.

How do I get it not to call it in the SelectedIndexChanged from the
Form1Load function? Normally, I want it to call it but not when I
initally fill the ComboBox.

*********************************************************
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Call LoadSystems()
Call Loademails(0)
End Sub

Private Sub LoadSystems()
Dim da As New SqlDataAdapter("COM_GET_SYSTEMS_SP", dbConn)
Dim ds As New DataSet
Dim dr As DataRow

da.Fill(ds, "Systems")
dr = ds.Tables("Systems").NewRow
dr(0) = 0
dr(1) = "All Emails"
ds.Tables("Systems").Rows.InsertAt(dr, 0)
SystemList.ValueMember = "id"
SystemList.DisplayMember = "name"
SystemList.DataSource = ds.Tables("Systems")
End Sub

Private Sub Loademails(ByVal systemID As Integer)
Dim da As New SqlDataAdapter("GetEmailMessages", dbConn)
da.SelectCommand.CommandType = CommandType.StoredProcedure
With da.SelectCommand.Parameters
If systemID <> 0 Then .Add("@SystemID", SqlDbType.Int).Value =
systemID
End With
Try
If Not ds.Tables("Emails") Is Nothing Then
ds.Tables.Remove("Emails")
da.Fill(ds, "Emails")
EmailDataGrid.DataSource = ds.Tables("Emails")
' EmailDataGrid.AllowSorting = False
EmailDataGrid.ReadOnly = True
Catch ex As Exception
Dim temp As String = ex.Message
End Try
End Sub

Private Sub SystemList_SelectedIndexChanged(ByVal sender As
System.Object, _
ByVal e As System.EventArgs) Handles
SystemList.SelectedIndexChanged
Loademails(sender.SelectedValue)
End Sub
**********************************************************************

Thanks,

Tom
Maybe I am an old dude with this, but in VB6 I always used a "loading"
variable to stop events firing until the load finished. In VB.Net 2005, I
still use the same construct, eg

_loading = true 'module level var

With MyCombo
.Datasource = MyObject.GetData()
.DisplayValue = "Description"
.ValueMember = "Pointer"
.SelectedIndex = -1
_loading = false
If .Items.Count > 0 Then
.SelectedIndex = 0 'or whatever you want it to be
End if
End With

and in the cbo.SelectedIndexChanged event add code something like:

If Not _loading AndAlso (cbo.SelectedIndex > -1) Then
'do whatever
End If

As I say, old dudes code, but it works real fine for me. Maybe someone can
suggest a better way to do this?

Harry
 

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