1.One which displays the items in the combo box called the DISPLAY MEMBER
2. Another one which stores the underlying ID for the Item which is being Displayed.
eg your table may have EmployeeId and EmployeeName.
You would like to display the EmployeeName in the ComboBox and when a user selects an employee name you would like to save the EmployeeID.
Function Fillcombo(ByVal SQL As String, ByVal CBO As ComboBox, ByVal DisplayMember As String, ByVal ValueMember As String)
Dim dt As New DataTable 'Create a New DataTable
Dim dc As New SqlCommand 'Create a New SQLCommand
Dim da As New SqlDataAdapter ' Create a New SQL Data Adapter
dc.CommandTimeout = 0
dc.CommandText = sql
da.SelectCommand = dc
dc.Connection = cn
da.Fill(dt) ‘ Use the DataAdapter to fill the DataTable with the records
CBO.DataSource = dt
CBO.DisplayMember = DisplayMember ‘ This is the item which will be displayed on the combo box eg Employee Name
CBO.ValueMember = ValueMember ‘ This is the value which is stored eg. employeeID
MsgBox(“Unable to Fill the Combobox:” & CBO.Name & ” with values” & “:” & Err.Description)
How to use the above function
Fillcombo(“Select EmployeeID,EmployeeName from Employees”, Me.EmployeeCombo, “EmployeeName”, “EmployeeID”)
You can easily use the following code to fill up your listview control from the data in a datatable using VB.NET
Supply the name of your listview and the datatable to this procedure and it will fill it up your list view.
If you wish to display the ongoing process then you can do that as well. Just create a label and then uncomment the following text in the procedure. Create a label and call it myStatusLabel.
‘ myStatusLabel.text = “Displaying Record ” & i + 1 & “/” & iRowCount
Sub FillListViewByTable(ByVal listView As ListView, ByVal dt As DataTable) Dim ii As Integer Dim iColCount As Integer = dt.Columns.Count Dim iRowCount As Integer = dt.Rows.Count listView.Clear() For ii = 0 To iColCount – 1 listView.Columns.Add(dt.Columns(ii).ColumnName, 250, HorizontalAlignment.Left) Next Dim i As Integer Dim x As Integer For i = 0 To iRowCount – 1 ‘ for each row in the table Dim lst As New ListViewItem lst.Text = dt.Rows(i).Item(0) lst.UseItemStyleForSubItems = True For x = 1 To iColCount – 1 ‘ for each column in the row of the table Application.DoEvents() lst.SubItems.Add(SNZ(dt.Rows(i).Item(x))) Next
Application.DoEvents() ‘ If you want to display the progress then you can do it here ‘ myStatusLabel.text = “Displaying Record ” & i + 1 & “/” & iRowCount listView.Items.Add(lst) lst = Nothing Next End Sub
You start with a new project and you have created a winform with graphics and the functionality which you want to use in all your other forms in the same project.
One would think that it is easily done as we can simply copy and paste the existing form and rename it and then suddenly all will be well.
That is not the case when you Control C and Control V to copy the form. You will try to rename it but you might gets lots of errors. I have struggled with this may times.
I have discovered a fool proof way which works well.
There is an Export Functionality in Visual Studio which is handy. This functionality allows you to create a template and resuse the template form in your project .
Go to File> Export Template in Visual Studio
Select ITEM Template, You can also choose Project Template ( which will appear only in your existing project).
where as ITEM template would be universally available for all new or existing projects.
Select the form which you want to clone
You might need to select the references which you want to be included, select the ones which you think will need. You can always add them later using the code in your project using the USING command in C# or IMPORTS command in VB.NET after you have cloned a form.
Provide a new name and some useful description to your new template.
when you click on Finish in Step 5 the new template will be created in the Windows Folder as you can see below.
When you click on ADD NEW ITEM in your Visual Studio project you should be able to see you newly created Template. You can use this template whenever there is a need . I found this approach simpler and safer than merely copying and pasting an existing form.
When you create a Combo Box in Visual Studio , the default behaviour is that it will show the list and it will also allow the data entry. So a user can type something in instead of selecting the options listed in the Combo Box. This can cause issues as you might want the user to only select the valid options which are listed in your combo box.
Here is a simple way to achieve this behavior. Highlight the Combo Box on your form. The properties window will be show.
Change the Drop Down Style to DropDownList as shown below.
Your combo box will not allow the users to type in any free text. they will have to select the options listed in your combo box.
If you have a need to validate a user input or string to ensure that only a valid email is entered , then you can use Regular Expressions or REGEX to handle that easily.
REGEX expressions appear to be quite complicated but once you have a valid syntax then it is simply a matter of using the proper syntax of REGEX in the following function and you should also be able to use the same function block to handle other types of inputs as well. eg. Numeric
The following function will validate your input against a valid email address.
At the top of your module or form add the following line of code
Add the following function in your form
Function Validate_Email(ByVal value As String) As Boolean Dim bok As Boolean = False Dim s As String s = LCase(value)
Dim myMatch As Match = System.Text.RegularExpressions.Regex.Match(s, “^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$”)
If myMatch.Success Then bok = True
Return bok Else bok = False Return bok End If End Function
How to use the function
Dim bok as boolean
bok = Validate_Email( YourInputTextHere)
If the user input is a valid email then bok will be true else false.