vb.net listview equivalent to VB6 listindex - vb.net

I am trying to get the following VB6 listindex to work within my vb.net code:
setTheR CStr(payReq.ItemData(payReq.ListIndex))
But if i copy and paste that into VB.net it wont accept it.
This is what VB.net did with the converting of the VB6 to .net code:
strContract = payReq.Items.Item(payReq.FocusedItem.Index).Text
However, checking that value it returns the name instead of the index. While the VB6 code returns the value of 2311 (which is what it needs to return)
When i add items to the listview i do this:
Item = payReq.Items.Add(rsPayRequests.Fields("userid").Value)
Item.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, VB6.Format(rsPayRequests.Fields("reqdatetime").Value, "mm/dd/yyyy")))
But i noticed it does this as well:
payReq.Items.Add(New VB6.ListBoxItem(Item, rsPayRequests.Fields("requestNum").Value))
But that does not work with my listview in .net since that above is a listbox and not a listview. Is there an equivalent in .net for the listbox to have a custom index?
Any help would be great!
David

Try strContract = lstPayRequest.FocusedItem.Index
The way you are using it is returning the item at that index
Edit:
To answer your question you can add subitems to a listviewitem or you could use the tag property of the ListViewItem for your custom index.
Dim lv As New ListViewItem
lv.Text = "Item1"
lv.Tag = 1001
lv.SubItems.Add("SubItem1")
lv.SubItems.Add("SubItem2")
lstPayRequests.Items.Add(lv)

Assuming I've read this right, you're doing what many a VB6 programmer did. You're storing a relevant value in the ItemData field that isn't the index but is related to the item. Very common practice in VB6.
Unfortunately, that practice is not directly supported in VB.NET. The VB.NET list box does not have the concept of ItemData thus a direct conversion of the VB6 code is not possible. The only solution I've encountered for this is to create a class based on the ListViewItem class. It could have a display name and an item data property. Then when you add items to the list, you create your custom item class, populate the properties and add that instance to the list. Then you can retrieve the item data value by casting the selected item into your custom item class.
It's a lot of work to replicate built-in VB6 functionality but it's the only option I've seen. Hopefully someone has a better answer to this question and I'll learn too.

Related

Fill a boolean array from checkbox control array

My program creates an array of checkboxes at runtime as shown below:
For Looper = 0 To 36
Dim Ex1ConfigCheck As New CheckBox
frmSetup.Controls.Add(Ex1ConfigCheck) ' Add Control to from
Ex1ConfigCheck.Top = (Looper + 45) + (Looper * 18) ' Set Location
Ex1ConfigCheck.Left = 210
Ex1ConfigCheck.Text = Setup.ExCheckName(Looper) ' Set Text property from strArray
Next
This is where I don't know how to proceed.
I would like to fill a boolean array (ex. MyBoolean(37)) with the value of Ex1configCheck().Checked. The reason I would like to fill another array is because I need to be able to reference the value of the checkboxes in other parts of the code but can't access them until they are created. Also, I plan on saving the array out to a binary file.
Could someone point me in the right direction please?
If there are no other CheckBoxes in the same container as those ones then you can do this:
Dim flags = Me.Controls.OfType(Of CheckBox)().
Select(Function(cb) cb.Checked).
ToArray()
If the controls are in a different container than the form itself, replace Me with that container.
As suggested by #Jimi, you could also create a List(Of CheckBox) and assign that to a field, populating it when you create the controls. You can then use that list instead of creating one on demand:
Dim flags = myCheckBoxList.Select(Function(cb) cb.Checked).
ToArray()
Of course, if you know exactly how many CheckBoxes you are going to be adding, why do you need to wait until run time to create them? Why can't you create them at design time and then modify them at run time? You usually only create controls at run time if you don't know how many there will be until run time, but that seems not to be the case here.
Thanks all for your answers and comments. I always have a fear of being roasted when I ask what some may consider a simple question online.
I have found an alternative way of accomplishing my task. Instead of creating 8 "Arrays" of checkboxes, I have learned of a very simple control available called "CheckedListBox".
I really didn't need to create the checkboxes at runtime but was trying to find an easier way to create 8 groups of 37 checkboxes without having to manually name and set the properties of each one during design. I also wanted to be able to index them in my code to be able to update and read the value using simple loops. I could have done this by creating arrays of CheckBox but again, I would have had to manually initialize the arrays.
Once I found the CheckedListBox, I was able to accomplish what I want very quickly. I only had to set the properties of the 8 "groups" (CheckedListBox's) and fill them using the items property. The ListBox essentially created a List like Jimi suggested automatically and I can index thru each list with a loop as desired. Jimi's suggestion actually lead me to finding the CheckedListBox while I was searching for more information on using "List(of CheckBox)".
Sometimes talking to others helps me find the right questions to ask. Google was able to figure out what I wanted when I searched for "List(of CheckBox)". (:

How to filter a GridView in Visual Basic WITHOUT any Database

I made a lot of research on internet, and have found a lot of tutorials about "how to filter/sort a GridView" in VB.NET, BUT, all of theme are using a Database.
In my case I only have these:
MP3Song : a custom class with some property like "Title","Artist","Duration", etc.
A List(Of MP3Song) : which is linked to my GridView like this:
myMP3Collection = New List(Of MP3Song.MP3Song)
mp3SongBinndingSource.DataSource = myMP3Collection
I'm using a List(Of ...) because when I populate this list, I do some verification and I'm likely to modify some "already added" data. And it is very easy for me :
myMP3Collection.ElementAt(i).Extd = True 'Extd is a Boolean property, others are string...
During my research I have seen that I should use a DataSet (easier to filter/sort a GridView).
Here is my question :
Should I make a DataSet based on my collection (populated by a For Each which scan my List, and add all item in row in my table
Or should I try to directly populate a DataSet instead of a ListOf(). If 2nd choice is better, is that easy as with collection to access and modify an element in a row ?
Thanks for your time
Tim Van Wassenhove's FilterList is just what you need. He extends the List(Of T) into SortableBindingLists and FilterLists.
While his code is C#, you can easily use an on-line converter. Or, create a C# .DLL, use the code, and reference that .DLL from your VB.NET app.

Load comboboxes with one line of code?

I have around 15 comboboxes on my form, all being loaded with the same information pulled from a table(~150 entries). Currently I am taking the information from the table, then looping through the entries and adding them to each textbox. I'm wondering if there's a more efficient way to load these comboboxes then having to individually add the table entry into each combobox, having to list 15 lines of code within the For loop.
I'm not seeing any performance issues with this, but figured I might as well work with the most efficient way possible rather than stick with what works. :)
You can create a list of the combo boxes, and then just loop through them. For instance:
Dim cbos() As ComboBox = {ComboBox1, ComboBox2, ComboBox3}
For Each cbo As ComboBox In cbos
' Load cbo from table
Next
Alternatively, if they are named consistently, you could find the combo box by name:
For i As Integer = 1 to 15
Dim cbo As ComboBox = DirectCast(Controls("ComboBox" & i.ToString())), ComboBox)
' Load cbo from table
Next
Since Combobox items are a collection, if their elements are the same, you can build and array with the objects you want to insert, and then just insert this array to each ComboBox with the method AddRange() (it's a method which exists inside the Combobox.items).
Getting an example from MSDN:
Dim installs() As String = New String() {"Typical", "Compact", "Custom"}
ComboBox1.Items.AddRange(installs)
Then you would only have to do a loop to add the array to each ComboBox. Of course, you will need to build your array first on your own, instead of this easy string array from the example.
Reference:
MSDN - AddRange
You could also do it this way since you mentioned that you already have a table.
Use a datatable
Change your table object into a datatable, which will assist in binding to the comboboxes. It might help if you add the datatable to a dataset too. That way you can attach all ComboBoxes (which are UI elements that let users see information) to the same DataSource, which is the datatable, in the dataset.
Binding
Now all you need to do is loop through all the comboboxes and set the datasource to the same table, that is if you decide to do it programmatically like so:
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.ValueMember = "au_id"
ComboBox1.DisplayMember = "au_lname"
A further tutorial on this with the example above is found here
You can then also get the user selected value with ComboBox1.selectedValue.
On the other hand, if you did this with C# WPF, you can bind each comboBox in the XAML directly, I am unsure if this can be done in VB.net as I tried to look for the option but did not manage to do so, something you might want to try though.
Some very useful tutorials and guides on Data binding, which you might be interested:
~ denotes recommended reading for your question
MSDN: Connect data to objects
DotNetPerls on DataGridView (note this isn't a combobox, just displaying values)
~ VBNet DataTable Usage from DotNetPerls (this is in relation to 1.)
~ SO Q&A on Binding a comboBox to a datasource
Concepts of Databinding

How to run a string as a command in Visual Basic (VB.NET)

i am running a Sub which takes as arguments a Datatable and a String.
Private Sub Populate(ByVal dttemp As DataTable, ByVal strWhichPart As String)
At one point there is a combobox which is populated with some of the data in the datatable. The name of the combobox is cmd plus the name of the string for example when the string is Person_FirstName the name of the combobox is cmbPerson_FirstName. Then i add items to the combobox like this:
cmbPerson_FirstName.Items.Add(strHold(rr))
My question is this can i make a String into a command? Because i have many comboboxes which have the same name as the string argument of the sub how can i do something like this to work
strWhichPart.Items.Add(strHold(rr))
in which strWhichPart is a string. Is there a command that i can execute a string as a command?
Thank you.
If I understand correctly, just grab the correct control from the Form's Controls collection using the string as the key:
CType(Controls(strWhichPart), ComboBox).Items.Add(strHold(rr))
You can use reflection to achieve this by creating an assembly with the code in a method, but its really not recommended. As in, it's insane. I suspect there are no problems in .NET development that need this approach.
Rather than using this approach, actually pass the relevant combo box as an argument - not an arbitrary string. Combo boxes are objects like anything else. You could create a dictionary which allows you to lookup combo boxes by a string. Something like:
Dictionary(Of String, ComboBox) MyDictionary = new Dictionary(Of String, ComboBox)()
MyDictionary.Add("ComboBoxA", objComboBoxA)
ComboBox objTheComboBox = MyDictionary("ComboBoxA")
The name you give your objects should not be semantically relevant in your code. If I name an object "lstMyObjectNamedThisWayForAReason1" I should NOT be using that to reference it. Instead, there should be a separation between what constitutes a GUI element and how it is referenced.
For example, if I create a WinForms GUI and reference all the items directly, then later have to write another front-end using a different framework I have to rewrite every reference. This isn't a problem if you don't tie your logic directly into your controls.
The only reason to tie them together is laziness and lack of respect for co-workers who might have to improve on your code in future. Why should they care what the GUI looks like? They might not even be using Visual Studio! They certainly can't take your code and use it elsewhere without ripping out your GUI dependency.
With a minor modification of ho1 it worked. Thanks a lot
CType(Controls("cmb" + strWhichPart), ComboBox).Items.Add(strHold(rr))

DataList Control in vb.net windows application forms

I am upgrading my application from vb6 to vb.net.
I used to populate my data in datalist control in vb6 forms. I am not finding it in vb.net windows form application. Which one control is the equivalent to it?
How can i list my data in that control?
just like:
I had a table with data like id, name
I want the name should be the displayed column and id the bound column? how can i do that
I'm not exactly sure but I think you're looking for a ListBox. You create a class with 2 members and a property for each (suggested property names ID and Name), create a list of them and put all your data in there and then just set the 'ListBox.DisplayMember' to "Name" and ListBox.ValueMember to "ID".
The MSDN page for ListControl.DisplayMember has a full sample showing how to do it as can be seen here.
Though I'd suggest that you use a List(Of YourClass) rather than the ArrayList in the sample.