Used VB 6
Trying out VB.Net 2008
How is the following code made easier in .Net?
VB 6 sample.
'10 textboxes on a form.
Private Sub txtTest_GotFocus(Index as Integer)
txtTest(Index).SelStart=0
txtTest(Index).SelLength=Len(txtTest(Index).Text)
Exit Sub
End Sub
With VB.Net I am required to enter this in 10 seperate text box fields.
How is this easier???
On the face of it it may look that way, but it's really not that way. I would
do something like this in my code (warning: air code!):
Public Class Form1
Inherits System.Windows.Forms.Form
Private txtBoxes() As TextBox
Public Sub New()
InitializeComponent()
txtBoxes = new TextBox() { _
TextBox1, _
TextBox2, _
TextBox3, _
TextBox4}
For Each txt As TextBox In txtBoxes
AddHandler txt.GotFocus, CommonFocusHandler
Next
End Sub
Private Sub CommonFocusHandler (ByVal sender As Object, ByVal e As EventArgs)
Dim txt As TextBox = TryCast (sender, TextBox)
If txt IsNot Nothing Then
' do stuff to text box
End If
End Sub
End Class
I know... Not exactly easier
Not everything in .NET is. But, you can
still accomplish what you want in this case - it just takes a bit more setup
Next Question.
How do you retrireve the ItemData from a combobox in VB.Net when it is not
there??
You don't need it in .NET. Combobox and ListBox will store any arbitrary
object. You aren't limited to strings... There are couple of behaviors that
you should be aware of though when using this feature - the first, is that by
default the combobox will display the results of .ToString of the object.
Usually, the default .ToString simply prints out the type name - not very
usefull
So, the way to overcome this is to:
1) Override the objects .ToString method to return something more appropriate
2) Change the ComboBox/ListBox DisplayProperty value to the name of the
property you want to use...
So, for a simple example (again, air code):
Public Class Person
Private _firstName As String
Private _lastName As String
Public Sub New()
End Sub
Public Sub New (ByVal firstName As String, ByVal lastName As String)
_firstName = firstName
_lastName = lastName
End Sub
Public Property FirstName() As String
Get
Return _firstName
End Get
Set (ByVal value As String)
_firstName = value
End Set
End Property
Public Property LastName() As String
Get
Return _lastName
End Get
Set (ByVal value As String)
_lastName = value
End Set
End Sub
Public ReadOnly Property FullName As String
Get
Retrun _lastName & ", " & _firstName
End Get
End Property
Public Override Function ToString() As String
Return FullName
End Function
End Class
Now you can create an array of these thingies like this:
Dim people() As Person = new Person() { _
New Person("John", "Smith"), _
New Person("Jerry", "Baker"), _
New Person("Sammy", "Simpleton")}
And then add them to a combobox:
ComboBox1.Items.AddRange (people)
Then, when an item is selected:
Private Sub combobox1_SelectedIndexChanged (ByVal sender As Object, ByVal e As EventArgs)
Person selectedPerson = TryCast (combobox1.SelectedItem, Person)
If selectedPerson IsNot Nothing Then
MessageBox.Show selectedPerson.FullName
End If
End Sub
What all of this amounts to - is that the combobox/listbox etc in .NET are far
more powerfull - and ItemData is a thing of the past. You can store a lot of additional data beyond a simple Long
value
Oh, the days when I used to use those ItemData fields as a pointers
to additional data
What is the limit of controls on a VB.Net form, since there is not longer
the Index of a control??
Only memory and the number of windowhandles - which I believe is something
like 10,000 per process on an NT based system...
Any tips might help with the evaluation of .Net.
Yeah, forget VB6