I've created a Student Record, I have a Button Delete ,I want to display a MessageBox with warning When Delete Button is clicked and no Item is selected in my Listview Control.You need to choose a Student.
Here is my code
If LvSV.SelectedItems.Count < 0 Then
MessageBox.Show("You need to choose Student!")
Else
Dim items As ListViewItem
items = LvSV.SelectedItems(0)
items.Remove()
End If
Dear you have a small mistake in your code :-)
you should check for Count equal to 0 instead of < 0.
If LvSV.SelectedItems.Count = 0 Then
MessageBox.Show("You need to choose Student!")
Else
Dim items As ListViewItem
items = LvSV.SelectedItems(0)
items.Remove()
End If
Also your code assumes exactly 1 selected item. Maybe you want to generalize it to all selected items.
For each item As ListViewItem in LvSV.SelectedItems
LvSV.Items.Remove(item)
Next
Cheers
Related
Sorry im new :-D
i wanne do the following in a List box:
add item
add item
....
im counting the added items and display them to a label.text. and also sending that to a .txt that updates.
when all items are added i want to press a bttn "end adding Items" and then doubleclick the items to edit them(i want to count that aswell.)
So want it look like this "changed Items / total Items"
how can i count the changes done by the doubleclick ? and how to display it ?
this is the doubleclick i did ;D
Dim intIndex As Integer = lstBla.Items.IndexOf(lstBla.SelectedItem)
Dim objInputBox As Object = InputBox("Edit", lstBla.SelectedItem)
If Not objInputBox = Nothing Then
lstBla.Items.Remove(lstBla.SelectedItem)
lstBla.Items.Insert(intIndex,objInputBox)
End If
End Sub
and this is the save to the lable.txt
Total.Text = lstBla.Items.Count
My.Computer.FileSystem.WriteAllText("C:\Counter\Total.txt", Total.Text, False)
Once I select a value from a Combobox and save the form, ComboBox.SelectedText contains the value selected but ComboBox.SelectedIndex is returning 0 always for each item in the list. Below is just a sample code for reference.
If (combobox1.SelectedIndex = 0 Or combobox1.SelectedText = "")
MessageBox.Show("No value selected")
else
MessageBox.Show("Some value selected")
End If
Some code to illustrate usage
Private Sub ComboBox1_SelectedIndexChanged(sender As Object,
e As EventArgs) Handles ComboBox1.SelectedIndexChanged
'check for no item selected
If ComboBox1.SelectedIndex < 0 Then
Stop 'no item
Else
Dim idx As Integer = ComboBox1.SelectedIndex
Dim val As String = CStr(ComboBox1.SelectedItem) '<-- use SelectedItem
Stop
End If
End Sub
Small Answer
SelectedText is not the same as SelectedItem. Take a look at ComboBox.SelectedText:
Gets or sets the text that is selected in the editable portion of a ComboBox.
I think that you are confusing this with ComboBox.SelectedItem
SelectedIndex: -1 if nothing selected, otherwise the index of the selected item
SelectedValue: null if nothing selected, otherwise the selected item
SelectedText: the text that the operator marked in the editable part of the combo box.
** Room for improvement **
You use VB. My VB is a bit rusty, so I'll give my answer in C#. I guess you'll get the gist.
In Winforms, whenever you want to fill a that shows a sequence of items, like ComboBoxes, ListBoxes, DataGridViews, Charts, etc, there are usually two methods:
Fill the ComboBox one by one with the texts that you want to display
Use a DataSource: fill it with the Items that you want to be selectable, and tell the ComboBox which property of the selectable items you want to display.
Use the first method if you only want to display a constant array of strings. Fill ComboBox.Items. When an item is selected, use SelectedIndex to get the index of the selected string. Use ComboBox.Items[selectedIndex] to get the selected string.
If the string represents something more than just a string, for instance the text represents a Customer, or a Product. It is usually easier to use the DataSource method.
To do that, you use property ComboBox.DataSource to tell the ComboBox which Customersit should display. In ComboBox.ValueMember you tell the ComboBox which Customer poperty should be used to represent the Customer, for instance the name of the Customer.
Once the operator selected the name of the Customer, you get the complete Selected Customer using property ComboBox.SelectedItem:
List<Customer> availableCustomers = ...
ComboBox combo1 = new ComboBox(...);
combo1.ValueMember = nameof(Customer.Name); // the Customer property that you want to display
combo1.DataSource = availableCustomers;
After the operator selected an item, you can process the event, and fetch the selected customer immediately:
Customer selectedCustomer = (Customer)cmbo1.SelectedValue;
ProcessSelectedCustomer(selectedCustomer);
Of course you should only select a property that is unique. If you have two Customers named "Hans Brinker", operators wouldn't know which name represents which Customer.
Apart from the nice thing that you don't have to do a lookup from SelectedIndex to what this selected item represents (a Customer), you are independent of the order in which the Customers are displayed.
Another nice thing: if in future versions you want to change from ComboBox to ListBox, or maybe even to DataGridView, you won't have to change your model drastically: the control still shows a sequence of Customers, and once an operator selects something that represents this Customer (Name? Id?, DataGridView Row?), you get the complete Customer.
(1)ComboBox1.SelectedIndex starts from -1, when you are not selected, ComboBox1.SelectedIndex=-1
(2)When you click combobox1.items, SelectedText is always "".
This is because, at the time of these events(SelectedIndexChanged, SelectedValueChanged.. ), the previous SelectedText value has been cleared and the new value has not yet been set. You can use the SelectedItem property instead.
E.g:combobox1.SelectedItem
Please try the following code.
If ComboBox1.SelectedIndex = -1 Then
MessageBox.Show("No value selected")
Else
MessageBox.Show("Some value selected")
End If
I have a checklistbox and the items of it came from my database (tbl_Section) so it loads all the Section Number (Primary Key). I have 5 Section Numbers, and 3 of it will be assigned to only one teacher. I'm thinking of using While-statement but i dont know how.
To make it simpler to you, this is what i need to do:
While //index(number) is checked
//do something
Else (i know it should not be ELSE, but i dont know what keyword is to be used)
//do something
End While
Thanks a lot!
What you want to do is iterate through every item in your checkbox. For each item, you check if it is checked, then you act accordingly :
'We will run through each indice
For i = 0 To CheckedListBox1.Items.Count - 1
'You can replace As Object by your object type
'ex : Dim Item As String = CType(CheckedListBox1.Items(i), String)
Dim Item As Object = CheckedListBox1.Items(i)
'We ask if this item is checked or not
If CheckedListBox1.GetItemChecked(i) Then
'Do something if Item is checked
Else
'Do something else if Item is not checked
End If
Next
am building a task management system using vb.net. How to set a textbox field beside each checkboxlist so when the user tick the checkbox he can comment too for this item and submit the whole finished tasks ? below is my code
Sub GetGroups()
cblGroups.DataSource = Task.Components.Tasks.GetAllTasks
cblGroups.DataTextField = "TaskName"
cblGroups.DataValueField = "ID"
cblGroups.DataBind()
End Sub
For Each item As ListItem In cblGroups.Items
If item.Selected Then
'reading each item value
End if
Next
I think you should be using a DataGridView instead. In there you can have a checkbox column and a textbox column, also a new row placeholder to put new tasks. If you don't like DataGridView, you can use alternatives.
Your other option would be to maintain a list of CheckBox controls and their TextBox pairs, essentially doing data grid's job. You are okay at first, but then you may need scrolling etc., so why not use a built-in control, where such issues are already solved out of the box.
'Check through each of the items
For Each item As ListItem In cblGroups.Items
'If this particular item is checked
If item.Selected = True Then
'Dynamically create a HTML Textbox
item.Text = [String].Format("{0}<input id=""TextBox{0}"" name=""TextBox{0}"" / >", item.Text)
Else
'Otherwise simply store the normal value
item.Text = [String].Format("{0}", item.Text)
End If
Next
How to retrieve the items from the selected item of a listview ?
[multiselect is set to to false]
I want to get data from the selected Listview Item and put it in a textbox.
I need subitems too
I tried this Textbox1.Text = List.SelectedItems().ToString but its not working
try
If List.SelectedItems.Count > 0 Then
Textbox1.Text = List.SelectedItems(0).Text
End If
For Each item As ListViewItem In listView1.SelectedItems()
MsgBox(item.SubItems(4).Text)
Next