How to get all the values selected in a multiselect ListBox in vb6? - vb6

I have a ListBox on my vb6 project and I've set its property to multiselect.
Now, I want to get all of the selected items on my multiselect ListBox and put it in a variable for example, or maybe on a multiline textbox. I just wanted to get the multiselected values.

Unfortunately you must loop through all elements. Here is some sample code:
Dim k As Long
Dim s As String
For k = 0 To List1.ListCount - 1
If List1.Selected(k) Then
s = List1.List(k)
' ... do something
End If
Next

Related

Unable to allow row selection on click over mshflexgrid control in vb6

The form that has mshflexgrid control is not allowing to select the row on which the mouse click has done. Sometimes it allows selection of previous row and not the row which is been clicked
I have tried adding one to the property .RowSel of mshflexgrid to allow the row selection, it is not working for second row selection.
Private Sub MSHFlexGrid1_Click()
last_row_selected = MSHFlexGrid1.RowSel
If last_row_selected <> 1 Then last_row_selected = last_row_selected + 1
With MSHFlexGrid1
If (boolShift And vbShiftMask) = vbShiftMask Then
SelectionOneAfterTheOther
ElseIf (boolShift And vbCtrlMask) = vbCtrlMask Then
SelectUnSelectGridRow
Else
UnSelectAllGridRows
.Row = last_row_selected
.ColSel = .Cols - 1
.CellBackColor = vbHighlight
.CellForeColor = vbHighlightText
End If
End With
MSHFlexGrid1.Refresh
End Sub
It seems that the .RowSel property isn't taking correct value for selection of row
The result needs to be proper selection of row on which it is clicked.
In your grid Click event, try something like this:
With MSHFlexGrid1
If .Row = .RowSel Then
.Col = .Cols - 1
.ColSel = 0
End If
End With
You may need to set SelectionMode on the grid control also. The above code should work if set to flexSelectionFree
Have you set the SelectionMode property? If not, you get to it by clicking "Custom" in the properties window. This opens a dialog where SelectionMode is one of the available properties.

VB6: Combo box style 2. Text Property behaviour

One of our VB6 project form is having a combo box. It's style is set to 2(Drop-down List Box).
When style is set to 2, its Text property becomes read-only. We cannot assign value to it.
But in our project some one has written code to assign string to Text property, and is working fine.
combobox1.Text = "Something" 'working
If i create a same kind of combo box with same properties, i am not able to assign string to text property.
I am getting error runtime error 383 'text' property is read-only
combobox2.Text = "Something" 'Not working
Can anyone help me understanding what i am missing.
The reason why someone else code worked is because they had the dropdown combo property selected and not the dropdown list. You need to add items to the combo list and then set the listindex to 0 as per below -
Combo1.AddItem "MyComboCaptionHere"
Combo1.AddItem "Hi"
Combo1.AddItem "There"
Combo1.ListIndex = 0
This will show a "caption" of MyComboCaptionHere.

Event Handlers for Dynamic Table Layout Panel in Visual Basic

I am making a risk-type game for school that dynamically creates a 4x4 grid of buttons inside a table layout panel in visual basic. I have successfully created the panel and buttons with names that correspond to the row and column of the button. There are also two parallel arrays - one for button owner and the other for button number - that correspond to the owner of the button and the number of "armies" in the button. My issue is that when the user clicks a certain button, I need to reference the button name/value to know how many "armies" the button has to control the "attack" portion of the player's turn.
The following code creates the table layout panel and the buttons with names.
'Create table Dynamically
Dim ColCount As Integer = 4
Dim RowCount As Integer = 4
Dim f As New System.Drawing.Font("Arial", 15)
riskTable.AutoScroll = True
riskTable.Dock = DockStyle.Fill
riskTable.ColumnCount = ColCount
riskTable.RowCount = RowCount
For rowNo As Integer = 0 To riskTable.RowCount - 1
For columnNo As Integer = 0 To riskTable.ColumnCount - 1
Dim buttonname As String
buttonname = "B" & rowNo & columnNo
Dim button As Control = New Button
button.Size = New Size(179, 100)
button.Name = buttonname
button.Text = "1"
button.ForeColor = Color.White
button.Font = f
AddHandler button.Click, AddressOf buttonname_Click
riskTable.Controls.Add(button, columnNo, rowNo)
Next
Next
Me.Controls.Add(riskTable)
This is the dynamic event handler that I created. I tried using 'Me.Click' to get the name of the button, but it only returns the name of the form. I need to have code in here that references the name of the currently clicked button to then in turn reference the box owner and box number arrays.
Private Sub buttonname_Click(sender As Object, e As EventArgs) Handles Me.Click
MessageBox.Show(Me.Name)
End Sub
Any help would be greatly appreciated! I think that once I get this working, the rest of the game will be pretty simple to figure out.
Thanks!
Put the name in 'button.Tag' instead/also:
button.Tag = buttonname
Then it is easy to get the name with:
Private Sub buttonname_Click(sender As Object, e As EventArgs) Handles Me.Click
Dim result As String = CType(CType(sender, System.Windows.Forms.Button).Tag, String)
End Sub
(Check the System.Windows.Forms.Button though, might need some tweak to match your buttons inside the table. riskTable.Controls.button ?)

How to export the contents of a combo box to the datatable?

I want to export the contents of a combo box to the local data table so that I can parametrize the test using that value.
You need to get each item from the ComboBox. For example where Window Name and ComboBox Name are the names of your window and ComboBox respectively
comboBox = Window("Window Name").WinComboBox("ComboBox Name");
count = comboBox.GetItemsCount
For i = 0 to count-1
item = comboBox.GetItem(i)
' put the item in the DataTable...
Next
How you output each item, the ' put the item in the DataTable.. line, will depend on your set up.

List view Double click Event

Using VB6
Listview
ID Name
001 Raja
002 Ramu
003 Sajee
..
…
Code
Private Sub listview1_DblClick()
If Not (listview1.SelectedItem Is Nothing) Then
Textbox1.text = listview1.selectedItem(0)
Textbox2.text = listview1.SelectedItem(1)
End If
End Sub
Above code is not showing the values in the text box
How to show the list view row values in the text box.
Need VB6 Code Help
The ListView SelectedItem property does not return a collection of items selected on your ListView, so therefore you can't explicitly get the first selected item, the second selected item, etc. You will need to loop through all ListItems in your ListView and check if each is selected. If it is, do what you want to do.
One problem I see with your sample code is you're using the ListView DblClick event. I might be wrong, but it looks like whenever it fires only one ListView item can be selected (the one that fired the event). A solution for this is to put your code into a new procedure. Here's one that should work:
Private Sub GetSelectedItems()
' Make sure exactly two items are selected on our ListView.
If (CheckListViewSelectedItemCount(listview1, 2)) Then
Dim blnFoundFirstItem As Boolean
blnFoundFirstItem = False
Dim i As Integer
' Find out which items are selected.
For i = 1 To listview1.ListItems.Count
If (listview1.ListItems(i).Selected) Then
' Assign the Text of the 'first' selected item to Textbox1.Text.
If (Not blnFoundFirstItem) Then
Textbox1.Text = listview1.ListItems(i).Text
blnFoundFirstItem = True
' Assign the Text of the 'second' selected item to Textbox2.Text.
Else
Textbox2.Text = listview1.ListItems(i).Text
End If
End If
Next i
Else
MsgBox "You need to select two items."
End If
End Sub
I'm not sure in which order ListItems are iterated through in my For loop. It's possible that what would be assigned to Textbox1.Text in my code you might want to assign to Textbox2.Text.
Your code required at that at least two items are selected on the ListView. I don't know if VB6 has a way to return the number of selected items so I wrote a small function that does this:
' Return True if the passed ListView control has a number of selected items that's equal to the intExpectedItemCount parameter.
Private Function CheckListViewSelectedItemCount(listView As listView, intExpectedItemCount As Integer) As Boolean
Dim intSelectedItemCount As Integer
intSelectedItemCount = 0
Dim i As Integer
For i = 1 To listView.ListItems.Count
If (listView.ListItems(i).Selected) Then
intSelectedItemCount = intSelectedItemCount + 1
End If
Next i
CheckListViewSelectedItemCount = (intSelectedItemCount = intExpectedItemCount)
End Function
I dont have vb6 at hand and its been a while since I used it, but if memory serves me right:
ListView1.SelectedItem will return you a ListViewItem which gives you a Text property along with SubItems property that gives you access to associated columns as an array.

Resources