Linking NumericUpDown control to a combobox

L

Laurie

Hi,

It there any elegant way of linking a ComboBox with a NumericUpDown so
that the combobox.selectedindex is set by the NumericUpDown.value
without needing to use code like:

Private Sub NumericUpDownCode_ValueChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
NumericUpDownCode.ValueChanged

If Me.NumericUpDownCode.Value < Me.ComboBoxCode.Items.Count Then
Me.ComboBoxCode.SelectedIndex = Me.NumericUpDownCode.Value
End If

End Sub

If there any value in:

NumericUpDownCode.Maximum = Me.ComboBoxCode.Items.Count - 1

which could avoid the need for the If statement?


Regards,


Laurie
 
J

James Hahn

Subclass the combobox control and add a new property AutoIndex as the
correct type. Write Get and Set routines that validate the values and
convert to the correct type for the combobox property you want to set frm
tehe NumericUpDown (eg, SelectedIndex)

Build the application and replace your existing ComboBox wth your new
control.

In Application Settings / Property bindings, for the NumericUpDown control,
bind the Value property to a settings variable that you define. For the
Combobox, bind the new property you created to the same settings variable.
They will now move in lock step.
 
L

Laurie

Hi James,

Thanks for the quick reply, but I'm afraid it's gone over my head.

I'm quite new to VB.NET and do not understand what you mean by
"Subclass" or how I would go about doing that.


Regards,


Laurie
 
J

James Hahn

Define a new class that inherits from the ComboBox class:
Public Class myCombo
Inherits ComboBox
...
End Class

Add a property
Public Property AutoValue
...
End Property

Add code to the Property Get routine to return the current property value
(MyBase.SelectedIndex). Add code to the property Set to validate the value
passed in (must be greater than -1 and less than MyBase.items.count - 1,
convert it from decimal to integer, and push it back to
MyBase.SelectedIndex.

Build your project. myCombo is now an item in the tooldbox. Replace your
existing combo box with your own version, and bind the properties as I
mentioned.
 
L

Laurie

Hi James,

Got it. Made it work and learnt quite a bit.

However, I believe I've reached a better solution. It is illustrated by
the code below on a form with a button, a combobox and a NumericUpdown
control.

The methods of using it are to ensure that when ever the Items.Count of
the combo is changed then the line:

"NumericUpDown1.Maximum = Me.ComboBox1.Items.Count - 1" is run

and any code which would have changed the ComboBox1.SelectedIndex,
changes the value of the NumericUpDown1 instead.


Public Class Form1

Private Sub ButtonExit_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ButtonExit.Click
End
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.ComboBox1.Items.Clear()
For i As Int16 = 1 To 5
Me.ComboBox1.Items.Add(Chr(64 + i))
Next
NumericUpDown1.Maximum = Me.ComboBox1.Items.Count - 1
NumericUpDown1.Value = 3
End Sub

Private Sub NumericUpDown1_ValueChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
NumericUpDown1.ValueChanged
ComboBox1.SelectedIndex = NumericUpDown1.Value
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ComboBox1.SelectedIndexChanged
NumericUpDown1.Value = ComboBox1.SelectedIndex
End Sub
End Class


Regards,


Laurie Comerford
 

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