List Boxes in VB.NET - vb.net

I want to manually add two items as "Active" and "Inactive" in a ListBox. When the user selects "Active", i want to get the value "A" and when "Inactive" is selected, i want to get "I".
How do i do this in VB.NET.

Are you using .NET 4? If so, the simplest solution is probably to use Tuple(Of String, String). Create a tuple of ("Active", "A") and another of ("Inactive", "I") and add those to the listbox. Then set the listbox's DisplayMember property to "Item1" and ValueMember to "Item2".
Or you could do the same sort of thing with an anonymous type.

The ListboxItemCollection is of type Object. You can create a custom ListItem like this
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
BindListBox()
End Sub
Private Sub BindListBox()
With ListBox1
.Items.Add(New CustomListItem("Acitve", "A"))
.Items.Add(New CustomListItem("Inactive", "I"))
.DisplayMember = "Text"
End With
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
MsgBox(CType(ListBox1.SelectedItem, CustomListItem).Value)
End Sub
End Class
''Custom ListItem Class
Public Class CustomListItem
Dim _text As String
Dim _value As String
Sub New(ByVal text As String, ByVal value As String)
Me._text = text
Me._value = value
End Sub
Public ReadOnly Property Text() As String
Get
Return _text
End Get
End Property
Public ReadOnly Property Value() As String
Get
Return _value
End Get
End Property
End Class

A simple option
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Debug.Print(ListBox1.Items(ListBox1.SelectedIndex).ToString.Substring(0, 1))
End Sub

Related

Validating a checkListBox?

I have two forms (1 and 2). I have been battling with some code that would prevent the user from selecting an item in the checkedListBox that was not added into ListBox2 from the previous form (form1).
The code I have is kind of weird because even if the item was added to listbox2 from form1, it continues to display the msgBox. I need the msgBox to display only to those items that were not added to listbox2, form1.
Here is what I have:
Public Class Form1
Dim ActSubject As Boolean
Public Function ActivateSubject() As String
Return ActSubject
End Function
Private Sub ListBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.TextChanged
Dim x As New Items
x.AvailableItems = ListBox2.Items.ToString
For Each x In ListBox2.Items
If ListBox2.Items.Contains(x) Then
ActSubject = True
Else
ActSubject = False
End If
Next
End Sub
End Class
Public Class Form2
Dim HaveActSubject As Boolean = Form1.ActivateSubject
Private Sub CheckedListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedValueChanged
If HaveActSubject = False Then
MsgBox("Sorry! Subject should be activated six month before registration.")
End If
Return
End Sub
End Class
Public Class Form1
Public Function ActivateSubject(itm as string) As String
Return ListBox2.Items.Contains(itm)
End Function
End Class
Public Class Form2
Dim HaveActSubject As Boolean = Form1.ActivateSubject
Private Sub CheckedListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedValueChanged
If Form1.ActivateSubject(CheckedListBox1.selectedValue) = False Then
MsgBox("Sorry! Subject should be activated six month before registration.")
End If
Return
End Sub
End Class

how to populate combobox in a usercontrol from an array in a class

I have a program in vb.net with a class and an usercontrol with a combobox. I want to populate the combobox with an array in the class. If user selects a value in combobox in usercontrol and return that value into class. I tried lot but there is no use.
Is it possible? If yes, then please guide me how to do it.
I have written the code for usercontrol as container1 as follows:
Private Sub container1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each i As Double In yl
Me.ComboBox1.Items.Add(i)
Next
End Sub
ERROR: Name yl is not declared.
yl is an array in my main class in the program.
thanks in advance
gvg
Ok you can do this in four steps:
Create a custom event in the usercontrol that fires when the selectedindex in the combobox is changed
Create a function that populates the combobox
Create a readonly property in your class that returns the items for the combobox
Mix this all together in the base form
First add some functionality to your userform
Public Class CtrlBox
'This is a usercontrol, created in VS
'The designer is not shown in this example
'This event is used to relay changes to the combobox to the outside world
Public Event ComboboxSelectionChanged(ByVal NewIndex As Integer, ByVal NewText As String)
'This Sub sets the combobox items
Public Sub SetComboboxItems(Newitems() As String)
Me.ComboBox1.Items.Clear()
Me.ComboBox1.Items.AddRange(Newitems)
End Sub
'Here the custom event is raised
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
RaiseEvent ComboboxSelectionChanged(Me.ComboBox1.SelectedIndex, Me.ComboBox1.Items(Me.ComboBox1.SelectedIndex).ToString)
End Sub
End Class
The class that will contain the selected index is called clsStuff. Notice the readonly property I use to return the desired items in the combobox
Public Class clsStuff
'This is like a constant that returns the items you
'want to add to the combobox
Public ReadOnly Property CustomComboItems As String()
Get
Return {"Test 1", "Test 2", "Test 3", "Test 4"}
End Get
End Property
'This variable shall store the selected string
Public SomeString As String = ""
End Class
Then bring it all together in your main form
Public Class Form1
'Drag the usercontrol containing the combobox onto your form
'This object is the one used to populate the combobox
'and store the selected item
Dim StuffObj As clsStuff
'Here the object is initialized and the CB-Items are set
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
StuffObj = New clsStuff
Me.CtrlBox1.SetComboboxItems(StuffObj.CustomComboItems)
End Sub
'Here the custom event is handled and used to change the
'string in the object
Private Sub CtrlBox1_ComboboxSelectionChanged(NewIndex As Integer, NewText As String) Handles CtrlBox1.ComboboxSelectionChanged
If StuffObj IsNot Nothing Then
StuffObj.SomeString = NewText
MsgBox("Updated property in object: " & StuffObj.SomeString)
End If
End Sub
End Class
This scheme should work well for your needs.
It is not a good way to access main program variable from the UserControl, although there is Parent property.
(1), you should create a AddItems() method to add the items as follow in your UserControl.
Public Sub AddItems(ByVal yl As String())
ComboBox1.Items.Clear()
For Each i As String In yl
ComboBox1.Items.Add(i)
Next
End Sub
(2), create a SelectedText() read-only property as follow
Public ReadOnly Property SelectedText()
Get
Return ComboBox1.Text
End Get
End Property
(3), create an event SelectedIndexChanged
Public Event SelectedIndexChanged()
(4), fire the event
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
RaiseEvent SelectedIndexChanged()
End Sub
(5), you can add items in your main form like this
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim yl() As String = {"Aa", "Bb", "Cc"}
Container1.AddItems(yl)
End Sub
(6), you can catch SelectedIndexChanged() event in your main form
Private Sub Container1_SelectedIndexChanged() Handles Container1.SelectedIndexChanged
Me.Text = Container1.SelectedText
End Sub

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).

Adding items from textbox to a list(of string)

I`m new to vb.net. Looking to find out how to add items into the list. At the moment,its only adding one item. I need it to save many items and must be able to display all items in another textbox. Please help!
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim patients As List(Of String) = New List(Of String)
patients.Add(TextBox1.Text)
TextBox2.Text = patients.Count
End Sub
End Class
Every time you click the button a new copy of the list variable is created and, of course, it is initially empty. You add one item but that's the end of the game.
If you want to preserve the contents of the list, you need to move the List variable at the global class scope
Public Class Form1
Dim patients As List(Of String) = New List(Of String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
patients.Add(TextBox1.Text)
TextBox2.Text = patients.Count
End Sub
.....
End Class
You need to declare and instantiate your list outside of Button Click:
Public Class Form1
Dim patients As New List(Of String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
patients.Add(TextBox1.Text)
TextBox2.Text = patients.Count
End Sub
End Class

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