This is probably easy as well. But I have this listview which contains exe files I've listed up. Now, I want to execute these exe files in turn from which items are checked or not.
So, I've tried this:
For each item in listView1.CheckedItems
Msgbox item.ToString
Next
Cause I noticed that the item in checkedItems doesn't contain much. And if I convert it to a string, it ends up in the msgbox looking like this: ListViewItem: {Filename.exe}
Now, I want the filename obviously. But is there any other way of extracting the name only? Or do I have to strip the string to remove the ListViewItem: { part away?
Your first step should have been to consult the documentation of ListViewItem to find out how to retrieve the required information from the object.
The Text property is what you’re after.
For Each item in listView1.CheckedItems
MsgBox(item.Text)
Next
For each item in listView1.CheckedItems
Msgbox item.Text
Next
This should work for winforms.
Have you tried Msgbox item.Text?
Your probably lookin for more of something like this. This will have the item and the subitems. You will need a loop with in a loop to accomplish this. Hopes this helps
Dim x As Integer = 0
Dim y As Integer = 0
For Each item In listView1.CheckedItems
For Each subitem In listView1.CheckedItems(y).SubItems
MsgBox(subitem.Text)
x = x + 1
Next
y = y + 1
Next
Related
I'm so close to getting this code working, I just need a little push please. I would like to
take the name of a combo box and then add a string to the end, But then get the value of a textbox with that string. This is to create a dynamic function instead of pasting the same code over and over.
Here's what I have so far, after you select something in the dropdown, the data is then pulled to populate the boxes next to it. I have about 8 drop downs so far so that's why I need this to work.
'Combobox after update
Call GrabData(Me, Me.ActiveControl)
Then
Private Sub GrabData(ctl As Control)
'name of ctl/combobox is "Kitchen"
data1 = (ctl.Name & "Size") '"KitchenSize"
'Here is where it all goes wrong
data1.Value = size.value
'size.value is just a textbox for example
End Sub
I can debug this with:
msgbox(data1)
'outputs "KitchenSize"
But I cannot get the value of kitchensize's textbox with data1.value
Error:
Object Required
I have also added Dim As String / Dim As Control.
I will be assigning the variable to some other stuff in this 50 line code I wrote so please don't take the above example as exactly what I intend to do, I just need help appending the ctl.name to a string, then use that to reference another control and so on.
EDIT
For anyone who wants to know, I figured it out.
Dim Ctrl As Control
Dim CtrlName As String
CtrlName = ctl.Name & "Size"
Set Ctrl = Me.Controls(CtrlName)
Ctrl.Value = 'Wherever you want to send the values to
See the edit.
You need to dim it as a string, then use Set Ctrl
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
Not sure if my title makes much sense, so I will try to explain my question here. So basically I am expanding my program by allowing things to be customized within it.
Say for example I do this: I click on File -> Options, and a new form is opened with tabs. I have different settings that you can toggle via dropdown box and checkboxes. Now once a user sets the settings they want, or don't want, they click on a button that says either "OK" or "Cancel".
What is the method to saving these settings, or reverting back to the original settings? Do you save via txt file, or is this a default function within a certain line of code?
UPDATE:
So I fixed my previous issue. Now I am having another with the saves. The saves are working good, but I want to use them in selecting my CheckListBox Collection range and also have that range load on start as well. so these are the 2 things that I have been using to do so, that results in adding to the previous, set, collection.
Working for RNG:
Dim rand As New Random()
Dim winners = Enumerable.Range(1, My.Settings.numberSetting).OrderBy(Function(r) rand.Next()).Take(5).ToArray()
Not working for Onload CheckListBox:
Me.LotteryNumbers.Items.Add(1, My.Settings.numberSetting)
If I remove the 1 from Me.LotteryNumbers.Items.Add, the result is this:
This ought not compile:
LotteryNumbers.Items.Add(1, My.Settings.numberSetting)
The overload which takes a second argument expect a Boolean to set the item added to Checked or not. One way is to add items in a loop:
Dim maxNums = My.Settings.numberSetting
' make sure it is empty
clb.Items.Clear()
For n As Int32 = 1 To maxNums
clb.Items.Add(n.ToString)
Next
I don't like using items in Settings as variables, so it grabs the current value to use. Another way uses AddRange:
clb.Items.AddRange(Enumerable.Range(1, maxNums).Select(Function(s) s.ToString()).ToArray())
Items is an collection of Object, so the Select converts to string to add them.
NEVER ORDER BY RANDOM.NEXT()
Mostly you get lucky, but it's not guaranteed. It's only a matter of time before that code blows up on you at run time. The longer the sequence to be sorted, the more likely you are to get an exception.
What you should do instead is implement a Fisher-Yates sort method:
Private rand As New Random()
Public Function Shuffle(Of T)(ByVal items As IList(Of T)) As IList(Of T)
For i As Integer = items.Count - 1 To 1 Step -1
Dim j As Integer = rand.Next(i + 1)
Dim temp As T= items(i)
items(i) = items(j)
items(j) = temp
Next
Return items
End Function
Solution for the working code to update and save checklistbox box count.
Private Sub OptionOkButton_Click(sender As Object, e As EventArgs) Handles OptionOkButton.Click
Main.LotteryNumbers.Items.Clear()
My.Settings.numberSetting = CInt(NumberCombo.Text)
Dim maxNum = My.Settings.numberSetting
Main.LotteryNumbers.Items.AddRange(Enumerable.Range(1, maxNum).Select(Function(s) s.ToString()).ToArray())
My.Settings.Save()
Me.Close()
End Sub
Self taught(in progress) Visual Basic guy here.
I've searched for a clear answer on this, but so far have come up empty handed.
The problem...
I have two comboboxes. The first combobox has 10 options, second combobox has 2 options
I have 10 textboxes, with a name that includes one of the 10 options.
ex 1st textbox name - "txb_Option1Type"
2nd textbox name - "txb_Option2Type" and so on.
I have 2 tabs, with the first 5 text boxes on the 1st tab and last 5 on the 2nd tab.
I thought the following bit of code, upon a button click, would transfer the text of the chosen option in the 2nd combobox to the corresponding textbox...
`
Public Sub TransferTruckToDoorText()
Dim str_ErrorButton As String = cbx_DoorNumber.Text
Dim str_ReplaceSpacesButton As String = str_ErrorButton.Replace(" ", "")
Dim str_Button As String = str_ReplaceSpacesButton
' Null reference error on below line of code
Me.Controls("txb_" & str_Button & "Type").Text = cbx_TruckType.Text
End Sub
`
As noted in the above code, I'm getting a null reference and for the life of me cannot figure out why. I've stepped through the code, and I'm not able to find a NULL or Nothing value that could be making this catch.
Any and all help would be appreciated.
edited for clarity
The Me.Controls collection does not automatically search the child panels.
Try using the Controls.Find method for that, which includes a parameter to search the child control's control collection, too. It returns an array:
Dim c As Control() = Me.Controls.Find("txb_" & str_Button & "Type", True)
If c.Length = 1 Then
c(0).Text = cbx_TruckType.Text
End If
Me.Controls.Item("txb_" & str_Button & "Type")
How do I get the SuspendLayout and ResumeLayout to work correctly in regards to a listview. I have added these lines of code before and after I have added the items, yet each item is still being displayed as I add each item.
thanks
My code:
Dim x As Integer
Dim startTick As Integer = Environment.TickCount
lstFiles.SuspendLayout()
For x = 0 To UBound(cacheListArray)
If cacheListArray(x) Like "*.txt" Then
lstFiles.Items.Add(cacheListArray(x))
End If
Next
lstFiles.ResumeLayout()
MsgBox(Environment.TickCount - startTick)
I am adding 2000 items. This takes 7 seconds. I was told that it should be a lot shorter than that. Is that correct?
New code:
Dim ListViewItemArray(-1) As ListViewItem
ReDim Preserve ListViewItemArray(UBound(ListViewItemArray) + 1)
ListViewItemArray(UBound(ListViewItemArray)).Text = file.Name
ListViewItemArray(UBound(ListViewItemArray)).SubItems.Add(file.FullName)
ListViewItemArray(UBound(ListViewItemArray)).SubItems.Add(file.Length)
ListViewItemArray(UBound(ListViewItemArray)).SubItems.Add(file.CreationTime)
ListViewItemArray(UBound(ListViewItemArray)).SubItems.Add(file.LastWriteTime)
ListViewItemArray(UBound(ListViewItemArray)).SubItems.Add(file.LastAccessTime)
lstFiles.Items.AddRange(ListViewItemArray)
Nothing is being added. What have I done incorrectly?
You are using the wrong method. The Suspend/ResumeLayout() methods are only appropriate for container controls whose child controls you want to move around without them getting repainted while you are doing so. You never give ListView any child controls.
Use the Begin/EndUpdate() methods instead.