Write individual listbox items into different text boxes and repeat until all text boxes are full - vb.net

I'm programming in Visual Basic.
I have one form.
Form 1 contains:
nameTextBox
addNameButton
namesListBox
generateButton
week1TextBox
week2TextBox
week3TextBox
week4TextBox
The user needs to go to Form 1 and type a name in the text box, then add the name to the List Box. The user will add 4 names to the List Box. So, the ListBox will contain the names: Adam, Brenda and Carol.
When the generateButton is clicked, the 3 names have to be written to the text boxes in that order. So week1TextBox should contain "Adam", week2TextBox should contain "Brenda", etc... but once the last name (in this case "Carol") is written into the text box, the loop should start over. Ultimately, there may be up to 50 week text boxes (so week50TextBox). So the loop needs to repeat over and over.

As there is a lack of source code in your question, I'm really not sure exactly how the layout should look, I can only offer some advice/suggestions.
I would recommend creating your listbox control, input textbox, and button to add names to the listbox. In addition to these, though, also add a scrollable panel. (Not sure what the exact term for that control is in VB.net; it's been a long time since I've worked with that language.) Because it sounds like there might be a variable number of items on the panel, when the user goes to generate the list of names, I would use the following rough pseudocode:
Dim OutputTexts As New ArrayList ' This is only here if you want to work with these textboxes later
Private Sub CreateOutput() Handles btnGenerate.Click
pOutputPanel.Controls.Clear()
OutputTexts.Clear()
Dim NextX As Integer = 0 ' Pretty much unnecessary value, but included in case you want to mess with this
Dim NextY As Integer = 0
For i As Integer = 0 To Convert.ToInt32(txtWeekCount.Text)
Dim txtName As New TextBox
txtName.Text = lbNameList.Item(i Mod lbNameList.Items.Count)
txtName.Location = new Point(NextX, NextY) ' Play with this as necessary
NextY += 50 ' Play with this as necessary
OutputTexts.Add(txtName)
pOutputPanel.Controls.Add(txtName)
Next
End Sub
Again, this is very much pseudocode, so I would not encourage copying and pasting, but give it a read, make sure you understand all of it, and then try implementing something similar. There might be an easier way to do it, but I have not programmed in VB.NET in probably over 2 years (at least). Nonetheless, the most important thing in here is the following line: lbNameList.Item(i Mod lbNameList.Items.Count). By Mod-ing your indexing variable, you will be accessing items sequentially, and then repeating from the start of the ListBox items collection once i is out of range.
I would also encourage you to dynamically generate your TextBox controls as needed rather than manually adding in 50 or more TextBox controls.

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)". (:

Multi-Criteria Search via three comboxes in vb.net with sql back end

I am currently working in VB.net with an sql back end server. I have a windows form application with a datagridview and 3 combo boxes that have data from three different columns in the datagridview. So far I have used IF statements for multi-criteria searches. This method has proven to be a long and inefficient way to write this code. It is literally a long run of IF statements saying if combobox1.selectedindex does not equal 1 and combobox 2 and 3 are still on the -1 index then search with just the first combo box. Then I write the code for every single possibility of the comboboxes being filled out. Whether it is 2 or all of them, or just one of them. I am wondering if there is a solid tutorial or a way to loop through these comboboxes to make this code quick and simple.
Something like this should get you started. It defines a list of combo boxes which you want to check and then determines which ones have a SelectedIndex <> -1. If it doesn't then the value is recorded.
' Build a list of values based on combo boxes with a selected index.
Dim values As New List(Of String)
' Build an array of combo boxes we want to process.
For Each cb As ComboBox In New ComboBox() {comboBox1, comboBox2, comboBox3}
' Check if the current combo box has an index selected.
If cb.SelectedIndex <> -1 Then
' It does - record its value.
values.Add(cb.Text)
End If
Next
' Do something with the values.
MessageBox.Show(String.Join(", ", values.ToArray))
' For example, build a where clause.
' If you do this, be sure to sanitize the values.
MessageBox.Show("WHERE 0=1 " & String.Join(" OR Field=", values.ToArray))

Any way for a combo box with 2 values per line?

I'm looking for an option with which I can use the functionality of a combobox together with a
listview.
The reason for this is because I have SQL queries which can't have more than 1 output, which I can get in some cases.
E.g. my SQL table looks somewhat like this
Unique_ID - Name
123456789 - Simon
987654321 - Simon
Basically the same name can be in the database multiple times, each entry with it's own ID.
For the user's sake, I can't have them choose what records to edit based on the ID, instead I have them base the chosen record on the name. When there's more than 1 record resulting from my query though, I get a MySQL exception.
Here's the MySQL query in question:
"SELECT worldspace from survivor where is_dead = '0' _ and survivor.unique_id = (select unique_id from profile where name = '" & target & "')"
The output from that query is then used in another UPDATE query.
So, is it possible for my combobox to have both the ID & the name as values, with a clear seperation between them so the entire value stays well readable, as it would do in a listview element?
I see you've already gotten the HighCore treatment about how easy everything is in WPF and how much WinForms sucks. But you might be interested to know that you can do this in WinForms, too. You just do it a little bit differently. It should come as no surprise that the standard design idioms differ in WinForms and WPF; that doesn't justify one being "better" than the other, it just means you need to learn how to use the one you're using. (Although, admittedly, some of the fancier stuff is a bit more difficult to achieve using a UI framework that was invented 20 years ago with Windows itself. The power it does have is rather remarkable.)
There are two basic ways of formatting the information: everything on a single line (which I believe is what you asked for in the question) or the pieces of information on two lines where each item is basically a two-line unit (which is what HighCore's WPF solution demonstrates).
Single-Line Format
The Simplistic Approach
We'll look at putting everything on a single line first, which really is simple. You don't need columns for separation, you can just use some kind of distinctive separator character when you add the items to the combobox, such as a vertical pipe (|) or a dash (-) like you used in the question.
This works so well because the ComboBox.Items.Add method accepts a parameter of type Object, on which it just calls ToString to get the value displayed in the control. If you pass it a string, it displays that string.
myComboBox.BeginUpdate()
For Each record In myRecordSet
myComboBox.Items.Add(String.Format("{0} | {1}", record.UniqueID, record.Name))
' or even...
myComboBox.Items.Add(String.Format("{0} ({1})", record.UniqueID, record.Name))
Next record
myComboBox.EndUpdate()
OR
An Incremental Improvement Through OOP
You can even pass a custom class to the Add method that keeps track of the unique ID and name properties (and anything else you want) and overrides the ToString method for display purposes.
Public Class Record
Public Property UniqueID As Long ' maybe this should be a string too
Public Property Name As String
Public Overrides Function ToString() As String
' Generate the string that will be displayed in the combobox for this
' record, just like we did above when adding it directly to the combobox,
' except that in this case, it will be dynamically generated on the fly,
' allowing you to also track state information along with each item.
Return String.Format("{0} | {1}", Me.UniqueID, Me.Name)
End Function
End Class
' ...
' (somewhere else, when you add the items to the combobox:)
myComboBox.BeginUpdate()
For Each r In myRecordSet
' Create a Record object representing this item, and set its properties.
Dim newRecord As New Record
newRecord.UniqueID = r.UniqueID
newRecord.Name = r.Name
' ...etc.
' Then, add that object to the combobox.
myComboBox.Items.Add(newRecord)
Next r
myComboBox.EndUpdate()
Fixing the Jaggies
Granted, if the first item in each set can be of variable length and you're using a variable-width font (i.e., one that is not monospaced like every UI on the planet does except code editors), the separators won't line up and you won't get two nicely-formatted columns. Instead, it looks all jumbled and ugly.
It would be nice of the ComboBox control supported tab characters that would handle lining everything up for us automatically, but unfortunately it does not. This is, regrettably, a hard limitation of the underlying Win32 control.
Fixing this ragged-edge problem is possible, but it does get a bit complicated. It requires taking over the drawing of the items in the combobox, referred to as "owner-draw".
To do this, you set its DrawMode property to OwnerDrawFixed and handle the DrawItem event to manually draw the text. You'll use the TextRenderer.DrawText method to draw the caption string (because that matches what WinForms uses internally; avoid using Graphics.DrawString), and TextRenderer.MeasureText if necessary to get the spacing right. The drawing code can (and should) use all of the default properties provided by the DrawItemEventArgs passed as e. You don't need OwnerDrawVariable mode or to handle the MeasureItem event because the width and height of each item cannot vary in this case.
Just to give you an idea, here's a quick-and-dirty implementation that simply divides the drop-down in half vertically:
Private Sub myComboBox_DrawItem(sender As Object, e As DrawItemEventArgs) Handles myComboBox.DrawItem
' Fill the background.
e.DrawBackground()
' Extract the Record object corresponding to the combobox item to be drawn.
Dim record As Record = DirectCast(myComboBox.Items(e.Index), Record)
Dim id As String = record.UniqueID.ToString()
Dim name As String = record.Name
' Calculate important positions based on the area of the drop-down box.
Dim xLeft As Integer = e.Bounds.Location.X
Dim xRight As Integer = xLeft + e.Bounds.Width
Dim xMid As Integer = (xRight - xLeft) / 2
Dim yTop As Integer = e.Bounds.Location.Y
Dim yBottom As Integer = yTop + e.Bounds.Height
' Draw the first (Unique ID) string in the first half.
TextRenderer.DrawText(e.Graphics, id, e.Font, New Point(xLeft, yTop), e.ForeColor)
' Draw the column separator line right down the middle.
e.Graphics.DrawLine(SystemPens.ButtonFace, xMid, yTop, xMid, yBottom)
' Draw the second (Name) string in the second half, adding a bit of padding.
TextRenderer.DrawText(e.Graphics, name, e.Font, New Point(xMid + 5, yTop), e.ForeColor, TextFormatFlags.Left)
' Finally, draw the focus rectangle.
e.DrawFocusRectangle()
End Sub
Now, this is looking pretty good. You can certainly improve on the technique used by the DrawItem event handler method, but it works out pretty well as is, so long as the combobox is made the right size for the values it will be displaying.
Multiple-Line Format
Defining a Custom ComboBox Class
The second method, where each item is a two-line group like HighCore's WPF example, is best done by subclassing the built-in ComboBox control and taking complete control its drawing routines. But that's nothing to be afraid of, subclassing a control is a standard WinForms idiom to gain extra control over the UI. (You could, of course, implement all of this by handling events like I did above, but I think subclassing is a much cleaner approach and also promotes reuse if you want to have multiple comboboxes that all behave in a similar fashion.)
Again, you don't need OwnerDrawVariable because the height of the items is not going to change. You'll always have two lines, so a fixed height works fine. You just need to make sure that you set the ItemHeight property to double of its normal value because you're going to have two lines. You could do this the complicated way using TextRenderer.MeasureText, or you could do it the easy way by just multiplying the default value by 2. I chose the latter for this demo.
Add this class into your project, and then use the MultiLineComboBoxcontrol instead of the built-in System.Windows.Forms.ComboBox. All of the properties and methods work the same.
Public Class MultiLineComboBox : Inherits ComboBox
Public Sub New()
' Call the base class.
MyBase.New()
' Typing a value into this combobox won't make sense, so make it impossible.
Me.DropDownStyle = ComboBoxStyle.DropDownList
' Set the height of each item to be twice its normal value
' (because we have two lines instead of one).
Me.ItemHeight *= 2
End Sub
Protected Overrides Sub OnDrawItem(e As DrawItemEventArgs)
' Call the base class.
MyBase.OnDrawItem(e)
' Fill the background.
e.DrawBackground()
' Extract the Record object corresponding to the combobox item to be drawn.
If (e.Index >= 0) Then
Dim record As Record = DirectCast(Me.Items(e.Index), Record)
' Format the item's caption string.
Dim caption As String = String.Format("ID: {0}{1}Name: {2}", record.UniqueID.ToString(), Environment.NewLine, record.Name)
' And then draw that string, left-aligned and vertically centered.
TextRenderer.DrawText(e.Graphics, caption, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.Left Or TextFormatFlags.VerticalCenter)
End If
' Finally, draw the focus rectangle.
e.DrawFocusRectangle()
End Sub
End Class
Adding Fancies and Flourishes
What we've got now isn't bad, but by lavishing a bit more effort on the drawing code in OnDrawItem, we can add some extra visual fancies and flourishes.
For example, without the selection rectangle, it would be pretty hard to tell that these are actually two-line units. That's unusual for a combobox control, so for usability reasons your application should go out of its way to make this abundantly clear. One way we might do that is by indenting the second line. You'll recall that I said that the built-in combobox control doesn't support tabs? Well that doesn't apply anymore, since we're doing the drawing ourselves now. We can emulate tabs by adding some extra padding to the beginning of the second line.
If you wanted the labels ("ID:" and "Name:") to be set apart from the actual values, you could do that, too. Perhaps you'd make the labels bold and lighten the text color.
So you see that just by playing with the drawing code, you can create almost any effect you want. We have complete control, and by wrapping it all up in a MultiLineComboBox class that can be reused all over the place, the rest of your code doesn't even have to know that anything special is happening. Cool, right?
Avoiding All the Work
And finally, I would be remiss if I didn't point out that you could skip doing all of this work and take your pick of the variety of custom multi-line combobox controls that have already been written.
This one is pretty nifty. It actually just displays a ListView control when you click the drop-down arrow on the combobox. In doing so, you get all of the formatting niceties of the ListView control for free, but it behaves just like a regular ComboBox.
This would be a WPF ComboBox with multiple Lines:
<ComboBox ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding ID, StringFormat='{}ID: {0}'}"/>
<TextBlock Text="{Binding Name, StringFormat='{}Name: {0}'}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Data Item:
public class ComboItem
{
public int ID { get; set; }
public string Name { get; set; }
}
Result:
To all the hackforms zombies: technical superiority speaks by itself.
The solution is actually way, way easier. Try this:
For i As Integer = 0 To dataSet.Tables(0).Rows.Count - 1
ComboBox1.Items.Add(dataSet.Tables(0).Rows(i)(0) + " | " + dataSet.Tables(0).Rows(i)(1)
+ " | " + dataSet.Tables(0).Rows(i)(2))
Next
But you have to implement it to your Code and DB

Searching a bound Combo Box for User Input

Alright Gents,
Im trying to search a vb.net combo box for an item. The combo box is already bound to dataset. The display member is set to display just a single column of the selected record. I had it set initially so the objects displayed in the combo were a customized class. In this class I specified all the properties I wanted to keep track of and that seemed to work well. However now that I am using the combo box in its bound state it is much more difficult to manipulate the data.
Mission:
To have the user type a number, if the number is contained in the ComboBox, the combo box should then move to that record so that all the other items bound to that control will update as well.
Research:
I have looked into the System.Windows.Forms.BingingManagerBase class and that seems to have the information I need. I just can't figure out the bridge between that and what I'm trying to do. I want to throw something together so I tried to simply do a SQL search against the dataset for the text in the combobox. Unfortunately that requires late binding and my targeted version of the .Net compact framework does not support that.
Here is an example of the late binding I was attempting. (Im working with VB.net 2005, Compact Framework 3.5 I believe:
For i as integer = 0 to combobox.items.count - 1
dim Dsr as Dataset.Row
dim dv as dataview
Dsr = DirectCase(Dv.row, Dataset.Row)
If Dsr(i).DesiredColumn = DesiredRow.Desiredcolumn then
'Do such and such code
End If
Next
I want to be able to search the dataset for a specific record matching a query. After I find that row matching the query I want to be able to move my combo box to the row found in the SQL query. The main problem seems to be that the Combo works in Datarowviews and my datasets are mostly cast to rows pertaining to the DS.
Anyone have some insight on this it would be much appreciated.
Thanks again!
If you know the item that should be set as selected in the combobox you can just set the ComboBox.SelectedItem Property
If you actually do really need to loop through all the items bound to the combobox then once you reach the correct one you can set the ComboBox.SelectedIndex Property.
For i As Integer = 0 To ComboBox.Items.Count - 1
Dim drv As System.Data.DataRowView = Nothing
Dim desiredColumn As String = String.Empty
drv = DirectCast(ComboBox.Items.Item(i), DataRowView)
desiredColumn = Convert.ToString(drv("Tag"))
Debug.WriteLine(desiredColumn)
Next
This seems to find the column value for every record in the combo box allowing me to find the correct index of the text I am searching for. Like I said though, if I could find a way to search through the list of items in the combobox without having to address each one, I would be grateful.

Typing filename in ListBox

when I wish to type the name of a listbox item (the listbox is populated by files in a directory), for example, if I type "apples" pressing A would take me to the first object with A in it's name, but typing "p" after will take me to the first item with p as the first letter. Is there any way I can make it so I can type a few characters and it would take me to that specific item? For example, the list might have;
ability
idea
boring
typing "abi" would select ability, rather than "ability", then "boring", then "idea"
Any help is appreciated. Thanks.
I think it's possible to make System.Windows.Forms.ListBox behave like that, but it would take some non-trivial code to make it work. System.Windows.Forms.ListView has this behavior built-in, so I would suggest using a ListView instead of a ListBox.
' Hide the headers to make the ListView look like a ListBox.
Me.ListView1.View = View.Details
Me.ListView1.HeaderStyle = ColumnHeaderStyle.None
Me.ListView1.BeginUpdate()
Try
' System.Windows.Forms.ListView doesn't have data binding capability.
' The listview's items have to be added using its
' Items.Add, Items.AddRange or Items.Insert methods.
For Each filename As String In Directory.GetFiles("C:\Windows").Select(Function(s) Path.GetFileName(s))
Me.ListView1.Items.Add(filename)
Next
Finally
Me.ListView1.EndUpdate()
End Try
' Add the column after adding the items.
' Setting column width to -1 will make
' the column autosize itself to the longest item.
Dim columnHeader As New ColumnHeader
columnHeader.Width = -1
Me.ListView1.Columns.Add(columnHeader)