VB.NET: Fields from a class - vb.net

I have a class being populated from comma separated rows in a text file.
I am trying to have the name property of each object appear in a listbox, then have the rest of the properties of a selected object show up in text boxes. How do I load the properties of the selected object to the correct textbox?

I'm assuming that the class you fill has a list of the objects and that you have already figured out how to fill the listbox...
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
For Each obj As YourObjectType In YourClass.CollectionOfYourObjects
If obj.Name = ListBox1.SelectedItem.ToString Then
Textbox1.Text = obj.Property1
Textbox2.Text = obj.Property2
End If
Next
End Sub

You have one TextBox for every Property and the number of properties is static, isn't it?
You have to set the DisplayMember to the Property's name that you want to see in the Listbox.
This simplified sample should work:
Public Class ListBox
Private Sub ListBox_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim allFoos As New List(Of FooClass)
For i As Int32 = 1 To 10
Dim foo As New FooClass
foo.Name = "Foo_" & i
foo.Prop1 = "Prop1_" & i
foo.Prop2 = "Prop2_" & i
foo.Prop3 = "Prop3_" & i
allFoos.Add(foo)
Next
Me.ListBox1.DataSource = allFoos
Me.ListBox1.DisplayMember = "Name"
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim foo As FooClass = DirectCast(ListBox1.SelectedItem, FooClass)
Me.TxtName.Text = foo.Name
Me.TxtProp1.Text = foo.Prop1.ToString
Me.TxtProp2.Text = foo.Prop2.ToString
Me.TxtProp3.Text = foo.Prop3.ToString
End Sub
End Class
Class FooClass
Private _name As String
Private _prop1 As Object
Private _prop2 As Object
Private _prop3 As Object
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Prop1() As Object
Get
Return _prop1
End Get
Set(ByVal value As Object)
_prop1 = value
End Set
End Property
Public Property Prop2() As Object
Get
Return _prop2
End Get
Set(ByVal value As Object)
_prop2 = value
End Set
End Property
Public Property Prop3() As Object
Get
Return _prop3
End Get
Set(ByVal value As Object)
_prop3 = value
End Set
End Property
End Class

Related

What should I add to a custom class so it works with System.Windows.Forms.Listbox.Items.Contains()..?

What should I add to a custom class so it works with System.Windows.Forms.Listbox.Items.Contains()..?
I've created a class called "FileItem" for handling filenames in ListBoxes. It works as expected, but if I try to use ListBox.Items.Contains() to prevent duplicate items from being added, nothing happens. The Contains() function never returns True.
Here's the code. Add it to a VB.Net form named frmTest with a ListBox1, Button1, and Button2. Run it and press Button1 multiple times. The duplicate warning message is never displayed.
Public Class frmTest
Private Sub frmTest_Load(sender As Object, e As EventArgs) Handles Me.Load
With ListBox1
.DisplayMember = "ShortName"
.ValueMember = "LongName"
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As FileItem = New FileItem("C:\Windows\Notepad.exe")
If ListBox1.Items.Contains(f) Then
MsgBox("The file is already added and will be skipped.")
Else
ListBox1.Items.Add(f)
End If
f = Nothing
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim msg As String = ""
For Each item As FileItem In ListBox1.Items
msg &= item.FullName & vbCr
Next
MsgBox(msg)
End Sub
Private Class FileItem
Private ReadOnly _FullName As String
Private ReadOnly _ShortName As String
Public Sub New(ByVal FullName As String)
_FullName = FullName
If FullName.Contains("\") Then
Dim pos As Integer = FullName.LastIndexOf("\")
_ShortName = FullName.Substring(pos + 1)
End If
End Sub
Public ReadOnly Property FullName() As String
Get
Return _FullName
End Get
End Property
Public ReadOnly Property ShortName() As String
Get
Return _ShortName
End Get
End Property
Public Overrides Function ToString() As String
Return _FullName
End Function
End Class
End Class

How to associate an object with ToolStripMenuItem VB.net?

Public Class Apple
Private d_isplayName As String
Public Property DisplayName() As String
Get
Return d_isplayName
End Get
Set(ByVal value As String)
d_isplayName = value
End Set
End Property
Private u_niqueVal As String
Public Property uniqueVal() As String
Get
Return u_niqueVal
End Get
Set(ByVal value As String)
u_niqueVal = value
End Set
End Property
End Class
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim RedApple As New Apple With {.DisplayName = "redapple", .uniqueVal = "Uval"}
tsiAddFruit.DropDownItems.Add("Some Text", Nothing, AddressOf fruit_Click)' Work fine
tsiAddFruit.DropDownItems.Add(RedApple, Nothing, AddressOf fruit_Click)' does not work
End Sub
Private Sub fruit_Click(sender As System.Object, e As System.EventArgs)
Dim MenuItem As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
MessageBox.Show(MenuItem.Text)
End Sub
How can I pass an object in ToolStripMenuItem without using the .Tag property(no third-party tool too)? I want to use Apple.DisplayName and the display value and Apple.UniqueVal as the value type.

Login Screen: Stack overflow exception

I am studying VB.NET so I want to make a simple login screen. For now, I only want that if I click on the button it writes something to the console ( I still dont know where that output is going ) but I get a stack overflow exception as soon as I click in Run.
Can someone advice me in why this code does not work?
Public Class Form1
Private Class Users
Public Property Name() As String
Get
' Gets the property value.
Return Name
End Get
Set(ByVal Value As String)
' Sets the property value.
Name = Value
End Set
End Property
Public Property Password() As String
Get
' Gets the property value.
Return Password
End Get
Set(ByVal Value As String)
' Sets the property value.
Password = Value
End Set
End Property
Public Sub New(ByVal name As String, ByVal password As String)
Me.Name = name
Me.Password = password
End Sub
End Class
Private user As New Users("Matias", "Barrios")
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Public Sub Validar(nombre As String, password As String)
Me.TextBox1.Text = user.Name
If nombre = user.Name And password = user.Password Then
System.Console.Write(user.Name)
Me.TextBox1.Text = "No"
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Validar("Matias", "Barrios")
System.Console.Write("Click!")
End Sub
End Class
You have this:
Public Property Name() As String
Get
' Gets the property value.
Return Name
End Get
Set(ByVal Value As String)
' Sets the property value.
Name = Value
End Set
End Property
The Get for that property refers to itself. So Get calls Get, which calls Get again, and so on, forever, until you run out of stack space for the function calls. Set does the same thing.
To fix the problem, the property is simple enough to use the auto-implement shorthand:
Public Property Name As String
But if you want to do it the long way, you need a backing field with a different name:
Private _Name As String
Public Property Name() As String
Get
' Gets the property value.
Return _Name
End Get
Set(ByVal Value As String)
' Sets the property value.
_Name = Value
End Set
End Property
Whichever you choose, you'll need to make the same change for the Password property.

Entering data into a datagrid

I am using Visual Basic 2008. I am trying to enter 2 columns of text into a datagrid using a button.
When i press the button a variable and a data associated to the variable should be entered into 2 columns of datagrid.
The code i am using is:
Public Class Entering_the_rates
Public Class Datagrid
Private category As String
Public Property getcategory() As String
Get
Return category
End Get
Set(ByVal value As String)
category = value
End Set
End Property
Private price As Decimal
Public Property getprice() As Decimal
Get
Return price
End Get
Set(ByVal value As Decimal)
price = value
End Set
End Property
End Class
Private Sub Entering_the_rates_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
datagridadult() as new Datagrid (category, price)
End Sub
End Class
is this syntax right or wrong.
I also need a sample code on how to call a datagrid i have declared . Please help me
You need a constructor in your class:
Public Class Datagrid
Private _category As String
Public Property getcategory() As String
Get
Return category
End Get
Set(ByVal value As String)
category = value
End Set
End Property
Private _price As Decimal
Public Property getprice() As Decimal
Get
Return price
End Get
Set(ByVal value As Decimal)
price = value
End Set
End Property
Public Sub New(byval category as string, byval price as decimal)
_category = category
_price = price
End Sub
End Class
Your private variables behind your properties should start with an underscore too, see here: http://10rem.net/articles/net-naming-conventions-and-programming-standards---best-practices this makes assigning them in the constructor easier. You could also use autoimplemented properties, these auto create the _ behind variables for you (though they do not show in intellisense).

VB .Net listboxes and collections of objects

I would like to move Item objects between the 2 following collections.
Private ItemsInRoom As New List(Of CItem)
Private Inv As New List(Of CItem)
I would like this to be done through 2 ListBoxes. 1 is the Inventory and the other is the Item list. How can I do this.
The CItem class has several members, only the Name of the item needs to be shown in the ListBox. I have been at this for hours, but I can't get anything to work. Does this explanation make sense in what I'm trying to do? If not, what else can I explain so someone might help me?
In your CItem class you need to override the ToString() function. That will get the name displayed in the listbox.
Public Class CItem
Public Overrides Function ToString() As String
Return Me.Name
End Function
'etc...
End Class
I think what you want is this:
Which is accomplished with the following code:
Option Explicit On
Public Class Form1
Private ItemsInRoom As New List(Of CItem)
Private ItemsInInv As New List(Of CItem)
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
ItemsInInv.Add(New CItem(1001, "Egret"))
ItemsInInv.Add(New CItem(1002, "Dove"))
ItemsInInv.Add(New CItem(1003, "Hawk"))
UpdateBindings()
End Sub
Public Function CheckOut(ByVal item As CItem) As Boolean
If item IsNot Nothing Then
ItemsInInv.Remove(item)
ItemsInRoom.Add(item)
Return True
End If
Return False
End Function
Public Function CheckIn(ByVal item As CItem) As Boolean
If item IsNot Nothing Then
ItemsInRoom.Remove(item)
ItemsInInv.Add(item)
Return True
End If
Return False
End Function
Public Sub UpdateBindings()
itemsInInvListBox.BeginUpdate()
itemsInInvListBox.DataSource = Nothing
itemsInInvListBox.DataSource = ItemsInInv
itemsInInvListBox.DisplayMember = "Name"
itemsInInvListBox.EndUpdate()
itemsInInvListBox.Refresh()
itemsInRoomListBox.BeginUpdate()
itemsInRoomListBox.DataSource = Nothing
itemsInRoomListBox.DataSource = ItemsInRoom
itemsInRoomListBox.DisplayMember = "Name"
itemsInRoomListBox.EndUpdate()
itemsInRoomListBox.Refresh()
End Sub
Private Sub itemsInInvListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles itemsInInvListBox.SelectedIndexChanged
checkOutButton.Enabled = itemsInInvListBox.SelectedIndex <> -1
End Sub
Private Sub itemsInRoomListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles itemsInRoomListBox.SelectedIndexChanged
checkInButton.Enabled = itemsInRoomListBox.SelectedIndex <> -1
End Sub
Private Sub checkOutButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkOutButton.Click
Dim item As CItem = CType(itemsInInvListBox.SelectedItem, CItem)
If CheckOut(item) Then
UpdateBindings()
End If
End Sub
Private Sub checkInButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkInButton.Click
Dim item As CItem = CType(itemsInRoomListBox.SelectedItem, CItem)
If CheckIn(item) Then
UpdateBindings()
End If
End Sub
End Class
Public Class CItem
Public Sub New(ByVal item_id As UInteger, ByVal item_name As String)
Me.m_id = item_id
Me.m_name = item_name
End Sub
Private m_name As String
Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property
Private ReadOnly m_id As UInteger
Public ReadOnly Property ID() As UInteger
Get
Return m_id
End Get
End Property
End Class