how to add combobox items in listbox or listview - vb.net

I want to know what control to be use on my project is it Listview or listbox???. I have a comboBox control on my project what I want to do is when I selected 1 item on my combobox it will automatically add on listbox or listview and when I selected more than 1 item I want to add it on listbox or listview on newline...
Is it simple, please help me to do that in listbox or listview..thanks!

Listbox > Is for viewing too but user can select it
Listview > Is for viewing only, user cannot select also it viewing by five view directly cause it's for viewing only
If your project wanted the list to be viewing from what have been select by Combobox, then you just pick List View, but if you want for viewing also user can select it, better use listbox, so it's up to you.
Also you can know how the Tools work by focus your mouse cursor to the tool, then it will pop up an tooltip that write what the tool for.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
ListView1.Items.Add(ComboBox1.SelectedIndex)
End Sub
That is the code to viewing in listview for what you select in combobox
For clearing all the item in the Listview or listbox, just write to your form_load
Listview.items.clear
Why i said in form load, cause the list just for viewing, of course every time the form begin to run, it will need blank list, so the best is put in form load
UPDATE
To remove the selected index in listbox
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub

ListView Items can be selected a couple of ways In the Properties window for the ListBox the activation property allows an item to be activated by one click or two clicks. Here is an example of how the selected items can be used
If Me.ListView1.SelectedItems.Count = (1) Then
'Declare the selected item
Dim lviSelectedItem As ListViewItem = Me.listView1.SelectedItems(0)
'Now you can use it
lblLabel1.Text = lviSelecetedItem.Text
Else
lblLabel2.Text = "You can make Items selectable by switching CheckBoxes property to True in the Properties windows and using CheckBox event handlers"
End If

Related

Retrieving the item right-clicked in a listbox for a context menu action - vb.net

In my vb.net app, I've got a list box that contains a bunch of email addresses.
There's a context menu on the list box with view contact, modify and remove options.
I'm currently stuck on how to determine which item in listbox1.items the user has right-clicked for use in the context menu actions.... So say for example a user right clicks 'johnsmith#stackoverflow.com' and clicks remove I then need to say
listbox1.items.remove(THEITEMTHATWASRIGHTCLICKED)
But how would I determine THEITEMTHATWASRIGHTCLICKED?
I tried...
itemthatwasrightclicked = listbox1.SelectedIndex
But if I right click on an item before left-clicking, I get a returned index of -1. If I left click the item to select it first and then right click I get the correct index returned, so it seems that if a user right clicks without first left clicking, the item isn't selected as such.
I'm at a loss and any help is appreciated!
I feel like this should be something simple.
Thanks in advance! :)
The listbox class provides a method for this in the MSDN. You will want to use the IndexFromPoint(Point) method. When this method is called it returns the index for the item in the list box found at the coordinates of the Point specified. You will want to capture the coordinates of the right click event by implementing this within the MouseDown event of the ListBox.
In its most basic form, the code for this would be as follows.
Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
If e.Button = MouseButtons.Right Then
ListBox1.SelectedIndex = ListBox1.IndexFromPoint(e.X, e.Y)
End If
End Sub

Filter a listview in vb.net

Currently I have a program that is able to write to a ListView with column named : number, time, description . This listview is not bound to anything data, I'm just basically writing into it using the code.
What I want to do is to have a TextBox, whereby if the user wants to look at particular number i.e. 2, when they type into the textbox, then I want the listview to only show data with number = 2. When there's nothing in the textbox, I want the listview to show all the data.
I have being looking around on the internet and I didn't seem to find a filter method. Does it even exist and if so how would I go about implementing this.
All help is appreciated.
While I recommend using a DataGridView with DataSource but in cases that you need to use ListView, you can use this solution.
You can filter your list view this way:
Define a member field as backup storage of items:
In form Load after adding items to list view, store each item in that member field
Put a TextBox and a Button on form and handle Click event of the Button and in the handler, first clear all items of ListView then each item from that backup storage that matches with criteria.
Member Field for Backup of Items
Private ItemsBackup As New List(Of ListViewItem)
Fill backup after loading items in ListView in the form Load event
For Each item As ListViewItem In ListView1.Items
ItemsBackup.Add(item)
Next
Code of Filter
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.ListView1.BeginUpdate()
Me.ListView1.Items.Clear()
For Each item As ListViewItem In ItemsBackup
If (item.Text = Me.TextBox1.Text Or String.IsNullOrEmpty(Me.TextBox1.Text)) Then
Me.ListView1.Items.Add(item)
End If
Next
Me.ListView1.EndUpdate()
End Sub
You can also use above code for TextChanged event of the TextBox.
Here is a test data:
For i = 1 To 30
Dim item As New ListViewItem(i.ToString())
item.SubItems.Add(DateTime.Now.AddHours(i))
Me.ListView1.Items.Add(item)
Next
A normal .NET ListView can't do this without a considerable amount of work. So, you could use ObjectListView -- an open source wrapper around a standard .NET ListView -- which has already done all the work for you.
It has built-in filtering (and highlighting)

winforms vb 2008 textbox validation issue

I have vb 2008 Winforms application.
In my Branch form I have 11 textbox fields and I want to validate 8 of them, either mandatory, or required format such as UK postcode, UK tel nos etc.
My issue now is that when the validation starts, it is validating the last text field first ( or seems to be)
here is my code
For Each oCtrl As Control In Me.Controls
If TypeOf oCtrl Is TextBox Then
oCtrl.Focus()
If Validate() = False Then
Exit Sub
End If
End If
Next
what's wrong please?
what's wrong please?
The controls collection isn't sorted or grouped. Your loop will access them in whatever order the collection has them.
Without more code it's hard to say how to fix it. However a tip may be in order. Use the same handler to handle the validate event for each textbox. This way you can keep the user at that textbox until the input is valid.
is it possible to add the items to the collection in the order of their tab indexes on form Shown event, how would I do that please?
A List(Of TextBox) and a custom sorter would probably be the way to go
Dim AllTB As New List(Of TextBox)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
AllTB.AddRange(Me.Controls.OfType(Of TextBox))
AllTB.Sort(Function(x, y) x.TabIndex.CompareTo(y.TabIndex))
End Sub
To loop through the textboxes use:
For Each tb As TextBox in AllTB
Because the TextBoxes are in the list by reference you can get or set any of the properties in the textboxes and any changes, will be reflected on your form. You could also use sequential names for the textboxes, tag property, etc. and sort by that.

Showing List of combobox while getting focus (vb.net)

When we click on drop down combobox control in our windows form, it shows the list automatically.
But when we press tab and navigate to that control from keyboard it doesn't shows the list automatically. So in other to show the list automatically on receiving focus what should be done?
Set the DroppedDown property of the combobox equal to true.
Private Sub myComboBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles myComboBox.GotFocus
myComboBox.DroppedDown= true
End Sub
I would like to mention on thing here.
I used Enter event to show Drop down list with DroppedDown=true,
But When I type something in text area of combobox and if i leave the area to next control, entered text is lost.
My combobox is databound.

Clean way to display/hide a bunch of buttons based on a ComboBox selection

I'm writing a standalone application in VB.NET using Visual Studio 2005.
I want to display/hide a bunch of Buttons based on the selected value of a ComboBox. Each selection would have a different set of Buttons to display, and I'd like to have them arranged in a nice grid.
Driving a TabControl with the ComboBox value would be the kind of behavior I want, but I don't want it to look like a TabControl to the user because it might be confusing.
Is there a way to do this?
Basically, I'd like Selection1 of the ComboBox to show Buttons 1-4, Selection2 to show Buttons 5-11, Selection3 to show (maybe) Buttons 1, 3, 5, 6, and 8, etc., have them arranged nicely, and have the GUI show only the ComboBox and the buttons.
Thanks in advance as always!
Use a Panel control (or multiple if the items aren't grouped right next to each other) and set the visibility accordingly.
(Added)
You CAN stack panels on top of each other, so that the buttons all look like they're in the same location. but it becomes a maintenance nightmare and I don't recommend it.,
Hack warning - the following is a hack, but it works.
Another option is to use a tab control, but hide the tab buttons. (You can do this by positioning a panel over the buttons, but you have to be careful of letting the user resize the form.) Then you set the TabIndex based on the drop-down changing.
Edit again - added per comment
If you use the hack, you can add this into the ComboBox's selected index changed event....
(code may be wrong, as I'm not at my dev pc and can't check, but you get the idea)
TabControl1.SelectedIndex = ComboBox1.SelectedIndex
You could put all of your buttons on a panel on your form. Then when the SelectedIndex event of the combobox fires, you can loop through the buttons on the panel and turn them on and off based on their Tag property.
For this example you would set the Tag property of each button equal to the combobox index or indexes that you want it to turn on for. If you want it visible for more than one combo selection just comma seperate the index values in the tag property.
You don't have to key off of the combobox index. You could use the selected text for example. If you did that, just put the texts to show the button for in the tag property and change the code from ComboBox1.SelectedIndex.ToString to ComboBox1.SelectedText.
The buttons will turn on and off where they are placed at design time, but you could add some code here to arrange them dynamically as well so that all the visible buttons are neatly arranged.
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
For Each ctrl As Control In Me.Panel1.Controls
If TypeOf ctrl Is Button Then
If Array.IndexOf(Split(ctrl.Tag, ","), ComboBox1.SelectedIndex.ToString) > -1 Then
ctrl.Visible = True
Else
ctrl.Visible = False
End If
End If
Next
End Sub
Maybe using a FlowLayoutPanel will help you display the buttons.
You can use a jagged array to define which buttons belong to which combo-box item.