I'm using vb.net form.
I have 8 listboxes (listbox1 , listbox2,listbox3..) each one contains many items and another empty listbox (listbox10). I'm trying to add first listboxes items into the listbox10
ex: ) listbox1 first items is 'A' , listbox2 first items is 'b' , listbox3 first items is 'c'...etc . Now listbox10 first item must be "Abc..."
The problem is i want to add text ( string or number) between each listbox
ex:)listbox1 first items is 'A' , listbox2 first items is 'b' , listbox3 first items is 'c'...etc . Now I want listbox10 first item to be "The letters ABC is now done"
The main idea is to combine all listboxes items and adding text between them to create a meaningful sentence
here is my code
Dim controls = New List(Of ListBox)() From _
{ListBox1, ListBox2, ListBox3, ListBox4, ListBox5, ListBox6, ListBox7, ListBox8}
Dim minCount = Controls.Min(Function(x) x.Items.Count)
For x = 1 To minCount - 1
ListBox10.Items.Add(String.Join(" ", controls.Select
(Function(lb) lb.Items(x).ToString)))
Next
End Sub
Where i must to add my (string)?
Since you want to put different text between the listbox items, you would have to drop the String.Join function and loop through the ListBoxes yourself:
Dim sb As New StringBuilder
For i As Integer = 0 To controls.Count - 1
For j As Integer = 1 To minCount - 1
sb.Append(controls(i).Items(j).ToString)
Next
If i < controls.Count - 1 Then
sb.Append(" " & i.ToString & " ")
End If
Next
ListBox10.Items.Add(sb.ToString)
Related
I have a ListView with multiple columns. More precisely, the ListView contains 8 columns. 2 of them should be filled with checkboxes.
Currently only the first column contains checkboxes. It is defined as follows:
While Not rs.EOF
//first column with checkboxes
ListViewCustomer.ListItems.Add , , rs("Id")
ListViewCustomer.ListItems(ListViewCustomer.ListItems.Count).tag = rs("Status")
//second column etc.
ListViewCustomer.ListItems(ListViewCustomer.ListItems.Count).ListSubItems.Add , , rs("name")
....
//Here is the second column, which doesn't display the checkboxes
ListViewCustomer.ListItems(ListViewCustomer.ListItems.Count).ListSubItems.Add , , IIf(IsNull(rs("date_from")), "", rs("date_from"))
ListViewCustomer.ListItems(ListViewCustomer.ListItems.Count).tag = rs("Status2")
Wend
Do anyone have an idea how to add the checkboxes in the last column?
EDIT:
Is it possible to realize this column with adding via .Controls?
A ListView is a more expanded version of the ListBox control.
See ListBox control on msdn as well.
They both display records of rows (the ListView has more advanced formatting options). This however means that a record is a row. Therefore you select a row when you select one of the items.
The function of the checkbox is to allow the user to mark the row(s) that is the records(s) he selects.
Thus there is only one checkbox per row, at the front of the row.
Consider this code (this is Excel 2003 VBA, but gives you the idea):
Private Sub UserForm_Initialize()
Dim MyArray(6, 8)
'Array containing column values for ListBox.
ListBox1.ColumnCount = 8
ListBox1.MultiSelect = fmMultiSelectExtended
'Load integer values MyArray
For i = 0 To 5
MyArray(i, 0) = i
For j = 1 To 7
MyArray(i, j) = Rnd
Next j
Next i
'Load ListBox1
ListBox1.List() = MyArray
End Sub
You could do a custom ListBox or ListView if you really want. You could create a frame and put Labels and CheckBoxes on it. This is the only way to do this in Excel2003 where I tested. The ListBox object has no Controls child.
But this is more like a datagrid and not really a ListBox or ListView which by definition are a listing of records (rows).
Update:
I saw your update and that you really want to place the CheckBox at the end of the row.
If you only want one checkbox at the last row, you could do this custom checkbox. Again this is written for the ListBox, so need to convert it to your ListView if you want to.
Still requires a custom handling, but I had some time, so I did this code. See if you like it:
Private Sub ListBox1_Change()
For i = 0 To ListBox1.ListCount - 1
ListBox1.List(i, 3) = ChrW(&H2610)
Next i
ListBox1.List(ListBox1.ListIndex, 3) = ChrW(&H2611)
End Sub
Private Sub UserForm_Initialize()
Dim MyArray(5, 3)
'Array containing column values for ListBox.
ListBox1.ColumnCount = 4
ListBox1.MultiSelect = 0
ListBox1.ListStyle = 0
'Load integer values MyArray
For i = 0 To 5
MyArray(i, 0) = i
For j = 1 To 2
MyArray(i, j) = Rnd
Next j
MyArray(i, 3) = ChrW(&H2610)
Next i
'Load ListBox1
ListBox1.List() = MyArray
End Sub
I'm using vb.net form.
I have 8 listboxes (listbox1 , listbox2,listbox3..) each one contains many items and another empty listbox (listbox10). I'm trying to add first listboxes items into the listbox10
ex: ) listbox1 first items is 'A' , listbox2 first items is 'b' , listbox3 first items is 'c'...etc . Now listbox10 first item must be "Abc..."
The problem is i'm only able to add 3 listboxes into the listbox10
But i want to all 8 listboxes to include in the loop.
here is my code
Dim n As Integer = _
Math.Min(ListBox1.Items.Count, Math.Min(ListBox2.Items.Count, ListBox3.Items.Count))
For i As Integer = 0 To n - 1
ListBox10.Items.Add( _
ListBox1.Items(i).ToString + _
ListBox2.Items(i).ToString + _
ListBox3.Items(i).ToString)
Next
Create a List<Listbox> of all the listboxes that you want to work on.
Then, using Linq, is pretty easy to reach your objective
Dim controls = new List(Of Listbox)() From _
{ listbox1, listbox2, listbox3, ....etc.... }
Dim minCount = controls.Min(Function(x) x.Items.Count)
for x = 0 to minCount-1
listbox10.Items.Add(string.Join(" ", controls.Select(Function(lb) lb.Items(x).ToString)))
Next
Of course, if all the Listbox have the same amount of items then the calculation to find the listbox with the lower amount of items is useless, you could replace it using the Items.Count of any Listbox (However, in defensive programming, I would leave it)
I'm making a 'Deal or No Deal' type game for a project in Visual Basic 2008.
I'm having a problem assigning the 5 values randomly to 5 boxes.
For example, in one game, the boxes could hold these values:
Box 1 = 1000
Box 2 = 35000
Box 3 = 25000
Box 4 = 75000
Box 5 = 5000
and in another game, they could hold these values
Box 1 = 75000
Box 2 = 25000
Box 3 = 1000
Box 4 = 5000
Box 5 = 35000
The main aim is to randomly assign these values to each box, and once a value has been assigned, it cannot be assigned to another box at the same time.
Here is the code that I have at the moment:
Dim values As New List(Of Integer)
Dim box(4) As Integer
Dim randNum(4) As Integer
'adding values to the Value list
values.Add(1000)
values.Add(5000)
values.Add(25000)
values.Add(35000)
values.Add(75000)
Dim i As Integer
For i = 0 To 4
Dim RandomClass As New Random()
Dim RandomNumber As Integer
RandomNumber = RandomClass.Next(0, 4)
'assigning a box a random value form the list
box(i) = values(RandomNumber)
'removing that value from the list
values.RemoveAt(i)
Next
Console.WriteLine("Box 1 = " & box(0))
Console.WriteLine("Box 2 = " & box(1))
Console.WriteLine("Box 3 = " & box(2))
Console.WriteLine("Box 4 = " & box(3))
Console.WriteLine("Box 5 = " & box(4))
Console.Read()
VB keeps returning this error message when I try t run the application:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Thanks in advance for any answers.
When you remove an item, the length of the list gets smaller. So on the second loop, index of 4 no longer exists. On the third loop, 3 no longer exists and so on. Try this instead:
Dim RandomClass As New Random()
Dim RandomNumber As Integer
For i = 0 To 4
RandomNumber = RandomClass.Next(0, values.count - 1)
'assigning a box a random value form the list
box(i) = values(RandomNumber)
'removing that value from the list
values.RemoveAt(i)
Next
The answer from Steve prevents the error from occurring, but I think it's still not doing quite what you want. values.RemoveAt(i) will always remove the first item from the list the first time, the second, next time, etc... it is not removing the value that you have just put in the box. To do that you should use values.RemoveAt(RandomNumber) instead.
For i = 0 To 4
RandomNumber = RandomClass.Next(0, values.count - 1)
'assigning a box a random value form the list
box(i) = values(RandomNumber)
'removing that value from the list
values.RemoveAt(RandomNumber)
Next
I have 100 order number in listbox and that number is part or file name. what i need to do is search file name using order number from list box. Example value is list box is
456789-789464
879746-123456
and file name is 456789-789464-2013-11-23456-456.pdf.
Logic for finding file is working but i cannot pass all order number from list box in to variable. I m using below code
MOOOR as string = ""
For Each item As String In ListBox1.Items(1)
MOOOR &= item & vbCrLf
Next
With this for loop i m getting msg ("Unable to cast object of type 'System.Data.DataRowView' to type 'System.Collections.IEnumerable'")
Your ListBox1 is probably bound to a DataTable( or DataView) and so the Items are DataRowView so the ListBox1.Items(1).Row is the Row(1) of your table.
To find selected items you need something like (WD: How to Determine Which Items Are Selected in a ListBox):
For x = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(x) = True Then
msg = msg & ListBox1.List(x) & vbCrLf
End If
Next x
My program takes a checkboxed item and depending on the serial number that is present on the item on the invoice, subtracts one from the amount list
I have the following in a listview on a details page which I added with the following code:
Item - Low Socks(pink)
Serial # - 34-75-860
Price - 5.89
Amount - 12
Except they are in columns instead of rows like above
Dim items As New ListViewItem
items = ListView1.Items.Add("Low Socks(Pink)")
items.SubItems.Add("34-75-860")
items.SubItems.Add("$5.89")
items.SubItems.Add("12")
items = ListView1.Items.Add("Low Socks(Black)")
items.SubItems.Add("34-75-900")
items.SubItems.Add("$5.89")
items.SubItems.Add("25")
items = ListView1.Items.Add("Low Socks(Red)")
items.SubItems.Add("34-75-756")
items.SubItems.Add("$5.89")
items.SubItems.Add("10")
items = ListView1.Items.Add("Low Socks(Orange)")
items.SubItems.Add("34-75-234")
items.SubItems.Add("$5.89")
items.SubItems.Add("34")
items = ListView1.Items.Add("Low Socks(Blue)")
items.SubItems.Add("34-75-598")
items.SubItems.Add("$5.89")
items.SubItems.Add("23")
End Sub
Under my invoice page I have checkboxes next to the items on the invoice. When the checkbox is clicked I want the amount to decrease by one. I will go in later and change it to the actual amount it needs to be depending on how many of that item they ordered... my coding for my checkbox is this:
Dim item As ListViewItem
Dim i As Integer
Dim count As Integer
'count the number of items in itemdetails2 listview
count = ItemDetails2.ListView1.Items.Count - 1
'loop to read each item in the list
For i = 1 To count
If i > count Then Exit For
item = ItemDetails2.ListView1.Items(i)
'compare the item to the serial number
If item.Checked = True Then
If (item.SubItems(0).Text = "34-75-860") Then
item.SubItems(2).Text -= 1
End If
i = i + 1
count = count - 1
End If
Next
ItemDetails2.Show()
End Sub
Right now it doesn't look like it does anything. I have tried changing my index's on my subitems to 1 and 3 instead of 0 and 2 but i figured because they are subitems that they need to be subitem index 0 and subitem index 2 since there is one item and three subitems to that one item. if that makes sense....
please help.
Without digging into your indexing stuff i can notice that your are treating strings as like as numeric
item.SubItems(2).Text -= 1
When you should try :
item.SubItems(2).Text -= CStr(CDec(item.SubItems(2).Text) - 1D)
I hope that helps...