PC Review


Reply
Thread Tools Rate Thread

Combo Box SelectedValueChanged

 
 
=?Utf-8?B?TmVpbCBTdGV2ZW50b24=?=
Guest
Posts: n/a
 
      15th Nov 2006
Hi fill a combo box with items as the form loads but this calles the
SelectedValueCahnged event. I dont want it to call the event until I select a
value in the combo box, whats the best way around the problem.

I think I can get around it by adding the handler after I have populated the
data, but just wondering if there is a better solution.
 
Reply With Quote
 
 
 
 
Morten Wennevik
Guest
Posts: n/a
 
      15th Nov 2006
Hi Neil,

Just filling the ComboBox with values shouldn't fire any
SelectedValueChanged event. How are you filling it?



On Wed, 15 Nov 2006 10:53:01 +0100, Neil Steventon
<(E-Mail Removed)> wrote:

> Hi fill a combo box with items as the form loads but this calles the
> SelectedValueCahnged event. I dont want it to call the event until I
> select a
> value in the combo box, whats the best way around the problem.
>
> I think I can get around it by adding the handler after I have populated
> the
> data, but just wondering if there is a better solution.




--
Happy Coding!
Morten Wennevik [C# MVP]
 
Reply With Quote
 
=?Utf-8?B?TmVpbCBTdGV2ZW50b24=?=
Guest
Posts: n/a
 
      15th Nov 2006
I basically read the values with a reader from SQL and then create a
datatable. I then make the combo box reference the table.

Example code.

Do while myReader.Read
Dim currentRow as DataRow = myTable.NewRow
currentRow("FieldID") = myReader("FieldID")
currentRow("FieldDisplayName") = myReader("FieldDisplayName")
myTable.Rows.Add(currentRow)
Loop

ME.cboTest.DataSource = myTable
Me.cboTest.DisplayMember = "FieldDisplayName"
Me.cboTest.ValueMember = "FieldID"


Thanks

Neil

"Morten Wennevik" wrote:

> Hi Neil,
>
> Just filling the ComboBox with values shouldn't fire any
> SelectedValueChanged event. How are you filling it?
>
>
>
> On Wed, 15 Nov 2006 10:53:01 +0100, Neil Steventon
> <(E-Mail Removed)> wrote:
>
> > Hi fill a combo box with items as the form loads but this calles the
> > SelectedValueCahnged event. I dont want it to call the event until I
> > select a
> > value in the combo box, whats the best way around the problem.
> >
> > I think I can get around it by adding the handler after I have populated
> > the
> > data, but just wondering if there is a better solution.

>
>
>
> --
> Happy Coding!
> Morten Wennevik [C# MVP]
>

 
Reply With Quote
 
Morten Wennevik
Guest
Posts: n/a
 
      15th Nov 2006
Interesting, it seems that ValueChanged fires whenever you set the
DataSource, DisplayMember or ValueMember. I'm not sure you can avoid
firing the event, but you can use a flag to determine when to not process
the event.


ignoreFlag = True
Me.cboTest.DataSource = myTable
Me.cboTest.DisplayMember = "FieldDisplayName"
Me.cboTest.ValueMember = "FieldID"
ignoreFlag = False


And in your event

If(ignoreFlag)
Return
End If


On Wed, 15 Nov 2006 11:46:03 +0100, Neil Steventon
<(E-Mail Removed)> wrote:

> I basically read the values with a reader from SQL and then create a
> datatable. I then make the combo box reference the table.
>
> Example code.
> Do while myReader.Read
> Dim currentRow as DataRow = myTable.NewRow
> currentRow("FieldID") = myReader("FieldID")
> currentRow("FieldDisplayName") = myReader("FieldDisplayName")
> myTable.Rows.Add(currentRow)
> Loop
>
> ME.cboTest.DataSource = myTable
> Me.cboTest.DisplayMember = "FieldDisplayName"
> Me.cboTest.ValueMember = "FieldID"
>
>
> Thanks
>
> Neil
>
> "Morten Wennevik" wrote:
>
>> Hi Neil,
>>
>> Just filling the ComboBox with values shouldn't fire any
>> SelectedValueChanged event. How are you filling it?
>>
>>
>>
>> On Wed, 15 Nov 2006 10:53:01 +0100, Neil Steventon
>> <(E-Mail Removed)> wrote:
>>
>> > Hi fill a combo box with items as the form loads but this calles the
>> > SelectedValueCahnged event. I dont want it to call the event until I
>> > select a
>> > value in the combo box, whats the best way around the problem.
>> >
>> > I think I can get around it by adding the handler after I have

>> populated
>> > the
>> > data, but just wondering if there is a better solution.

>>
>>
>>
>> --
>> Happy Coding!
>> Morten Wennevik [C# MVP]
>>




--
Happy Coding!
Morten Wennevik [C# MVP]
 
Reply With Quote
 
=?Utf-8?B?UmFkIFtWaXN1YWwgQyMgTVZQXQ==?=
Guest
Posts: n/a
 
      15th Nov 2006
Alternatively, you could remove the handler of the event before you assign
the datasource and then reattach it after you're done.

So you can modify your code to do this:

'
' Remove the handler
'
RemoveHandler cboTest.SelectedValueChanged, AddressOf
cboTest_SelectedValueChanged
'
' Bind the data
'
Me.cboTest.DataSource = myTable
Me.cboTest.DisplayMember = "FieldDisplayName"
Me.cboTest.ValueMember = "FieldID"

'
' Re-attach the handler
'
AddHandler cboTest.SelectedValueChanged, AddressOf
cboTest_SelectedValueChanged

--


Bits. Bytes.
http://bytes.thinkersroom.com
------------------------------


"Morten Wennevik" wrote:

> Interesting, it seems that ValueChanged fires whenever you set the
> DataSource, DisplayMember or ValueMember. I'm not sure you can avoid
> firing the event, but you can use a flag to determine when to not process
> the event.
>
>
> ignoreFlag = True
> Me.cboTest.DataSource = myTable
> Me.cboTest.DisplayMember = "FieldDisplayName"
> Me.cboTest.ValueMember = "FieldID"
> ignoreFlag = False
>
>
> And in your event
>
> If(ignoreFlag)
> Return
> End If
>
>
> On Wed, 15 Nov 2006 11:46:03 +0100, Neil Steventon
> <(E-Mail Removed)> wrote:
>
> > I basically read the values with a reader from SQL and then create a
> > datatable. I then make the combo box reference the table.
> >
> > Example code.
> > Do while myReader.Read
> > Dim currentRow as DataRow = myTable.NewRow
> > currentRow("FieldID") = myReader("FieldID")
> > currentRow("FieldDisplayName") = myReader("FieldDisplayName")
> > myTable.Rows.Add(currentRow)
> > Loop
> >
> > ME.cboTest.DataSource = myTable
> > Me.cboTest.DisplayMember = "FieldDisplayName"
> > Me.cboTest.ValueMember = "FieldID"
> >
> >
> > Thanks
> >
> > Neil
> >
> > "Morten Wennevik" wrote:
> >
> >> Hi Neil,
> >>
> >> Just filling the ComboBox with values shouldn't fire any
> >> SelectedValueChanged event. How are you filling it?
> >>
> >>
> >>
> >> On Wed, 15 Nov 2006 10:53:01 +0100, Neil Steventon
> >> <(E-Mail Removed)> wrote:
> >>
> >> > Hi fill a combo box with items as the form loads but this calles the
> >> > SelectedValueCahnged event. I dont want it to call the event until I
> >> > select a
> >> > value in the combo box, whats the best way around the problem.
> >> >
> >> > I think I can get around it by adding the handler after I have
> >> populated
> >> > the
> >> > data, but just wondering if there is a better solution.
> >>
> >>
> >>
> >> --
> >> Happy Coding!
> >> Morten Wennevik [C# MVP]
> >>

>
>
>
> --
> Happy Coding!
> Morten Wennevik [C# MVP]
>

 
Reply With Quote
 
=?Utf-8?B?TmVpbCBTdGV2ZW50b24=?=
Guest
Posts: n/a
 
      15th Nov 2006
Yep I used the addhandler , just thought it was a bit strange that this event
is raised in this manner.

Thanks for all the replies


"Rad [Visual C# MVP]" wrote:

> Alternatively, you could remove the handler of the event before you assign
> the datasource and then reattach it after you're done.
>
> So you can modify your code to do this:
>
> '
> ' Remove the handler
> '
> RemoveHandler cboTest.SelectedValueChanged, AddressOf
> cboTest_SelectedValueChanged
> '
> ' Bind the data
> '
> Me.cboTest.DataSource = myTable
> Me.cboTest.DisplayMember = "FieldDisplayName"
> Me.cboTest.ValueMember = "FieldID"
>
> '
> ' Re-attach the handler
> '
> AddHandler cboTest.SelectedValueChanged, AddressOf
> cboTest_SelectedValueChanged
>
> --
>
>
> Bits. Bytes.
> http://bytes.thinkersroom.com
> ------------------------------
>
>
> "Morten Wennevik" wrote:
>
> > Interesting, it seems that ValueChanged fires whenever you set the
> > DataSource, DisplayMember or ValueMember. I'm not sure you can avoid
> > firing the event, but you can use a flag to determine when to not process
> > the event.
> >
> >
> > ignoreFlag = True
> > Me.cboTest.DataSource = myTable
> > Me.cboTest.DisplayMember = "FieldDisplayName"
> > Me.cboTest.ValueMember = "FieldID"
> > ignoreFlag = False
> >
> >
> > And in your event
> >
> > If(ignoreFlag)
> > Return
> > End If
> >
> >
> > On Wed, 15 Nov 2006 11:46:03 +0100, Neil Steventon
> > <(E-Mail Removed)> wrote:
> >
> > > I basically read the values with a reader from SQL and then create a
> > > datatable. I then make the combo box reference the table.
> > >
> > > Example code.
> > > Do while myReader.Read
> > > Dim currentRow as DataRow = myTable.NewRow
> > > currentRow("FieldID") = myReader("FieldID")
> > > currentRow("FieldDisplayName") = myReader("FieldDisplayName")
> > > myTable.Rows.Add(currentRow)
> > > Loop
> > >
> > > ME.cboTest.DataSource = myTable
> > > Me.cboTest.DisplayMember = "FieldDisplayName"
> > > Me.cboTest.ValueMember = "FieldID"
> > >
> > >
> > > Thanks
> > >
> > > Neil
> > >
> > > "Morten Wennevik" wrote:
> > >
> > >> Hi Neil,
> > >>
> > >> Just filling the ComboBox with values shouldn't fire any
> > >> SelectedValueChanged event. How are you filling it?
> > >>
> > >>
> > >>
> > >> On Wed, 15 Nov 2006 10:53:01 +0100, Neil Steventon
> > >> <(E-Mail Removed)> wrote:
> > >>
> > >> > Hi fill a combo box with items as the form loads but this calles the
> > >> > SelectedValueCahnged event. I dont want it to call the event until I
> > >> > select a
> > >> > value in the combo box, whats the best way around the problem.
> > >> >
> > >> > I think I can get around it by adding the handler after I have
> > >> populated
> > >> > the
> > >> > data, but just wondering if there is a better solution.
> > >>
> > >>
> > >>
> > >> --
> > >> Happy Coding!
> > >> Morten Wennevik [C# MVP]
> > >>

> >
> >
> >
> > --
> > Happy Coding!
> > Morten Wennevik [C# MVP]
> >

 
Reply With Quote
 
=?Utf-8?B?UmFkIFtWaXN1YWwgQyMgTVZQXQ==?=
Guest
Posts: n/a
 
      15th Nov 2006
I suppose that even though you specify the datasource and the data members,
under the hood items are still being added to the combo box ergo the selected
value would change.

It is debatable if Microsoft should switch off this behavior. Personally I
don't think so -- perhaps there are other housekeeping routines that need to
be called when each item is added.

Though it would be nice if there was a property such as EnableEvents that
would control the raising of of events to our custom code ....
--


Bits. Bytes.
http://bytes.thinkersroom.com
------------------------------


"Neil Steventon" wrote:

> Yep I used the addhandler , just thought it was a bit strange that this event
> is raised in this manner.
>
> Thanks for all the replies
>
>
> "Rad [Visual C# MVP]" wrote:
>
> > Alternatively, you could remove the handler of the event before you assign
> > the datasource and then reattach it after you're done.
> >
> > So you can modify your code to do this:
> >
> > '
> > ' Remove the handler
> > '
> > RemoveHandler cboTest.SelectedValueChanged, AddressOf
> > cboTest_SelectedValueChanged
> > '
> > ' Bind the data
> > '
> > Me.cboTest.DataSource = myTable
> > Me.cboTest.DisplayMember = "FieldDisplayName"
> > Me.cboTest.ValueMember = "FieldID"
> >
> > '
> > ' Re-attach the handler
> > '
> > AddHandler cboTest.SelectedValueChanged, AddressOf
> > cboTest_SelectedValueChanged
> >
> > --
> >
> >
> > Bits. Bytes.
> > http://bytes.thinkersroom.com
> > ------------------------------
> >
> >
> > "Morten Wennevik" wrote:
> >
> > > Interesting, it seems that ValueChanged fires whenever you set the
> > > DataSource, DisplayMember or ValueMember. I'm not sure you can avoid
> > > firing the event, but you can use a flag to determine when to not process
> > > the event.
> > >
> > >
> > > ignoreFlag = True
> > > Me.cboTest.DataSource = myTable
> > > Me.cboTest.DisplayMember = "FieldDisplayName"
> > > Me.cboTest.ValueMember = "FieldID"
> > > ignoreFlag = False
> > >
> > >
> > > And in your event
> > >
> > > If(ignoreFlag)
> > > Return
> > > End If
> > >
> > >
> > > On Wed, 15 Nov 2006 11:46:03 +0100, Neil Steventon
> > > <(E-Mail Removed)> wrote:
> > >
> > > > I basically read the values with a reader from SQL and then create a
> > > > datatable. I then make the combo box reference the table.
> > > >
> > > > Example code.
> > > > Do while myReader.Read
> > > > Dim currentRow as DataRow = myTable.NewRow
> > > > currentRow("FieldID") = myReader("FieldID")
> > > > currentRow("FieldDisplayName") = myReader("FieldDisplayName")
> > > > myTable.Rows.Add(currentRow)
> > > > Loop
> > > >
> > > > ME.cboTest.DataSource = myTable
> > > > Me.cboTest.DisplayMember = "FieldDisplayName"
> > > > Me.cboTest.ValueMember = "FieldID"
> > > >
> > > >
> > > > Thanks
> > > >
> > > > Neil
> > > >
> > > > "Morten Wennevik" wrote:
> > > >
> > > >> Hi Neil,
> > > >>
> > > >> Just filling the ComboBox with values shouldn't fire any
> > > >> SelectedValueChanged event. How are you filling it?
> > > >>
> > > >>
> > > >>
> > > >> On Wed, 15 Nov 2006 10:53:01 +0100, Neil Steventon
> > > >> <(E-Mail Removed)> wrote:
> > > >>
> > > >> > Hi fill a combo box with items as the form loads but this calles the
> > > >> > SelectedValueCahnged event. I dont want it to call the event until I
> > > >> > select a
> > > >> > value in the combo box, whats the best way around the problem.
> > > >> >
> > > >> > I think I can get around it by adding the handler after I have
> > > >> populated
> > > >> > the
> > > >> > data, but just wondering if there is a better solution.
> > > >>
> > > >>
> > > >>
> > > >> --
> > > >> Happy Coding!
> > > >> Morten Wennevik [C# MVP]
> > > >>
> > >
> > >
> > >
> > > --
> > > Happy Coding!
> > > Morten Wennevik [C# MVP]
> > >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ComboBox SelectedValueChanged won't fire goscottie@gmail.com Microsoft C# .NET 3 29th Aug 2007 04:34 PM
ListBox SelectedValueChanged Event Anomaly!!!! giddy Microsoft Dot NET Framework Forms 2 14th Mar 2007 03:20 PM
ComboBox SelectedIndexChanged vs. SelectedValueChanged Stephen Boutros Microsoft Dot NET Framework Forms 1 28th Oct 2005 12:37 PM
SelectedValueChanged event fires too many times Keith-Earl Microsoft VB .NET 5 24th Feb 2005 09:54 PM
ComboBox.SelectedValueChanged event Dominic Paquette Microsoft Dot NET Framework Forms 2 23rd Sep 2003 04:57 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:49 PM.