Single selection CheckedListBox control - vb.net

I have a CheckedListBox control.I want to limit it's selection property to one means now a user can select more than one item in the control, need to limit this property to single selection only.
For example, Let's CHKListsolutions has following items
Google
Bing
Yahoo
Normally we can select 3 of them because of an obvious reason.
How to make this CHKListsolutions to select only one item in the list.
for example, I select Google and for some reason I want chnage the selection so I will select Yahoo then my last selection should unchecked and new one should be checked
I have checked in the resource for a property but in vain.
Any help would be very much appreciated

Private Sub CHKListsolutions_MouseClick(sender As Object, e As MouseEventArgs) Handles CHKListsolutions.MouseClick
Dim idx, sidx As Integer
sidx = CHKListsolutions.SelectedIndex
For idx = 0 To CHKListsolutions.Items.Count - 1
If idx <> sidx Then
CHKListsolutions.SetItemChecked(idx, False)
Else
CHKListsolutions.SetItemChecked(sidx, True)
End If
Next
End Sub
In MouseClick event you'll get the currently selected index of the item in the control(sidx) use this sidx to loop through number of items in the control and uncheck checked item that is not equal to the current index using SetItemChecked method

Use Radio Button instead of Checkedlistbox

Related

How to alphabetize fields in Unhide Columns dialog box in Access VBA?

I have a ton of columns in this table and I want them to be alphabetized so they are easier to find.
I remember seeing how to do this in a Youtube video but I can't find it for the life of me. Below is an example of the code I am using in multiple datasheet type forms. I'm not sure what needs to be added in to make these field lists alphabetize
Private Sub showHideColumns_Click()
frmInventoryListSubform.SetFocus
DoCmd.RunCommand acCmdUnhideColumns
End Sub
The "columns", the controls, have both a name and, optionally, a caption.
So, in your form, you can run this code to list these:
Private Sub ListColumns()
Dim Control As Control
Dim Index As Long
For Index = 0 To Me.Controls.Count - 1
Set Control = Me.Controls(Index)
If Control.ControlType <> acLabel Then
Debug.Print Index, Control.Name, Control.Properties("DatasheetCaption").Value
End If
Next
End Sub
May return a result like this:
0 StipendNo Student Number
2 PayNo Pay Number
4 PayDate
6 PayAmount

Visual Basic: How can I get all selected Values from a Listbox (Selectionmode Multisimple) using a Loop?

I'm using a Listbox in my program which is coupled to a dataset. Now I want to select some items in Runtime and get all the values behind those selected items. The values represent the ID's from the Database Entities. I need them for some SQL-queries.
So this is what I've already tried:
For x = 0 To ContracttypeListBox.Items.Count() - 1
If ContracttypeListBox.GetSelected(x) = True Then
MsgBox(ContracttypeListBox.SelectedItems(x)(ContracttypeListBox.ValueMember))
End If
Next
I found the function in the MsgBox using my friend Google. Actually it works quite well but only if I select all items (starting with the first one) in the Listbox. An unselected item among them will cause a System.IndexOutOfRangeException. The same Problem occurs when I don't select the first item in the list but all the others.
Thanks to Plutonix
This is the solution:
For x = 0 To ContracttypeListBox.SelectedItems.Count() - 1
If ContracttypeBox.SelectedItems.Count() > 0 Then
MsgBox(ContracttypeListBox.SelectedItems(x)(ContracttypeListBox.ValueMember))
End If
Next x
You can use ListBox.SelectedItems.
For Each item In ListBox1.SelectedItems
Dim id As Integer = Val(item.ToString)
Next

DevExpress XtraGrid GroupRow, CheckEdit Interactions Possibilities

Currently, I am displaying an XtraGrid that contains Group Rows. I have a "Select All" DevExpress.XtraEditors.CheckEdit control (which is different from this elusive "Select All" check box control I am reading about in the documentation). It is different for a reason: I want the check box to do something other than "Select All" (which comes in only three different varieties according to the DevExpress Docs.).
I want a user to be able to do one of two things with the CheckEdit control. [1] If no Group Rows are expanded, I want to select all Group Rows. [2] If one or more Group Rows are expanded, I only want to select the expanded rows.
Presently, I am able to manipulate the controls to do only one of the two things (see code). My question is twofold: is this possible; and, if so, how would I go about it?
Here is my code that does the second of the two 'things' described above:
'If the CheckEdit control is checked:
xtraGrid.SelectAll()
Dim rowHandles() As Int32 = xtraGrid.GetSelectedRows()
If rowHandles.Count > 0 Then
For Each RowHandle As Int32 In rowHandles
If xtraGrid.IsGroupRow(RowHandle) Then
xtraGrid.UnselectRow(RowHandle)
End If
Next
End If
As you can see, all this really is just a work around. There is also presumably more overhead than needed in my calling .GetSelectedRows(). I am just attempting to sort through the row types in order to keep the row selected or .UnselectRow()
You can use GridView.GetRowExpanded method to check whether a specific group row is expanded. For iterating through visible rows you can use GridView.RowCount property. To get row level just use GridView.GetRowLevel method. Also you need to check wether a row is new item row or is filter row by using GridView.IsNewItemRow method and GridView.IsFilterRow method. For all of this methods you need to get Row handle by using GridView.GetVisibleRowHandle method. When you are working with selection is better to use GridView.BeginSelection method and GridView.EndSelection method outside of your code.
UPDATE: If you want to select hidden rows within a collapsed group then you can use GridView.GetChildRowCount method and GridView.GetChildRowHandle method to get all hidden rows.
Here is some sample code that is performing your two things together:
Private Sub SomeSub
If xtraGrid.GroupCount = 0 Then
xtraGrid.SelectAll()
Exit Sub
End If
xtraGrid.BeginSelection()
xtraGrid.ClearSelection()
Dim isExpanded As Boolean = False
For rowVisibleIndex = 0 To xtraGrid.RowCount - 1
Dim rowHandle As Integer = xtraGrid.GetVisibleRowHandle(rowVisibleIndex)
If xtraGrid.IsNewItemRow(rowHandle) Then
Continue For
End If
Dim level As Integer = xtraGrid.GetRowLevel(rowHandle)
If level = 0 Then
If Not isExpanded Then
isExpanded = xtraGrid.GetRowExpanded(rowHandle)
If isExpanded Then
xtraGrid.ClearSelection()
Else
xtraGrid.SelectRow(rowHandle)
End If
End If
Else
xtraGrid.SelectRow(rowHandle)
'Update: select hidden rows
If xtraGrid.IsGroupRow(rowHandle) And Not xtraGrid.GetRowExpanded(rowHandle) Then
SelectRowHierarchy(rowHandle)
End If
End If
Next
xtraGrid.EndSelection()
End Sub
Private Sub SelectRowHierarchy(rowHandle As Integer)
Dim childCount As Integer = xtraGrid.GetChildRowCount(rowHandle)
For childIndex As Integer = 0 To childCount - 1
Dim childRowHandle As Integer = xtraGrid.GetChildRowHandle(rowHandle, childIndex)
xtraGrid.SelectRow(childRowHandle)
If xtraGrid.IsGroupRow(childRowHandle) Then
SelectRowHierarchy(childRowHandle)
End If
Next
End Sub

Search Listbox and return specific number of items defined in another field - VB.net

I'm running a search query that pulls the search string from one text box, then searches a listbox for the string and displays the results in a second listbox. I would like to define the number of items that it returns based on a second text box. So far I am able to get all list items with be given string to show in the second box. But I have yet to get it to limit the search to 1 item etc. The functional code I've used to show all results is:
Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
lstResults.Items.Clear()
If txtSearch.Text.Length > 0 Then
For index As Integer = 0 To lstCountries.Items.Count - 1
Dim txt = lstCountries.Items(index).ToString()
If txt.StartsWith(txtSearch.Text, StringComparison.CurrentCultureIgnoreCase) Then
lstResults.Items.Add(txt)
End If
Next
End If
I've tried using while txtnumber.text > 1 then ahead of this but seem to have created a loop. Any ideas as to what I'm missing?
The datasource is going to have the final say on how many results there are. If the user specifies 3 and there are only 2 in the source, thats all you will get. Something like this will move the results from one to the other until the max is found:
Dim Max as integer = Convert.Toint32(txtNumber.Text)
Dim Count as integer = 0
lstResults.Items.Clear()
For index As Integer = 0 To lstCountries.Items.Count - 1
if lstCountries.Items(index).StartsWith(txtSearch.Text, StringComparison.CurrentCultureIgnoreCase) Then
lstResults.Items.Add(lstCountries.Items(index))
count += 1
end if
' exit the loop if we found enough
If count>= Max Then
Exit For
End If
Next
If the listbox just contains text (strings) then you do not need the ToString: lstCountries.Items(index).StartsWith(strSearch). Basically, you need to exit the loop once the user desired number have been found OR if you run out of data...if I understood right. while txtnumber.text > 1 would not work because textboxes contain strings not numerics (23 is not the same as "23").
The LINQ extensions would work well here as well:
lstResults.Items.AddRange((From item In lstCountries.Items
Let strItem As String = item.ToString
Where strItem.StartsWith(txtSearch.Text, StringComparison.CurrentCultureIgnoreCase)
Select strItem).Take(Integer.Parse(txtnumber.Text)).ToArray)
This assumes that all your data has already been validated.

Find a part of text in a listbox

I've been given some code to find text that is contained in a listbox but, its not what I need. When the user types in the textbox (which is the search field), they have to type the exact text, not the part of the text. Is there any way to find a part of a value/text in a listbox?
For example, I have a listbox that contains these items:
data1
data2
When I type (2) in the search field(textbox/richtextbox), I would like the second item, which contain the '2' value, to be selected.
How can I code this?
You could use IndexOf
Dim hits = From item In listBox1.Items.Cast(Of String)()
Where item.IndexOf(txtSearch.Text, StringComparison.OrdinalIgnoreCase) >= 0
If hits.Any Then
listBox1.SelectedItem = hits.First()
End If
If you don't want to ignore the case, just use String.Contains instead of String.IndexOf.
Note that above is a linq query, so it won't work with .NET 2 this way.
You have to know the terms you want for your search. For example, Text containing any of the letters in the search text, complete words, not searching symbols, etc.
You have to do it "manually", there isnt a function to search similar texts.
For example:
Private Sub SearchBox_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles SearchBox.TextChanged
Dim iFound As Integer = -1
For i As Integer = 0 To SearchBox.TextLength - 1
If TextBox1.Text.Contains(SearchBox.Text(i)) Then
iFound = TextBox1.Text.IndexOf(SearchBox.Text(i))
Exit For
End If
Next
If iFound >= 0 Then
TextBox1.Select(iFound, 1)
End If
End Sub
You didn't mention that you want to keep other items (which do not match with the search query) in your listbox or not.
If you just want to keep the items in listbox which matches or contains your search query then you can check this answer on stackoverflow.
Match starting of a string in listbox OR Match some part of a string in listbox items