Textbox replace and split not working vb.net - vb.net

I've been struggling way too much with simple things, like the one i'm posting.
I'm developing a UI in vb.net that gathers some information from a machine. The information is collected to a TextBox:
Private Sub ReceivedText(ByVal [text] As String)
If Me.TextBox2.InvokeRequired Then
Dim x As New SetTextCallBlack(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.TextBox2.Text &= [text]
End If
End Sub
Then i gather that information either to a datagridview or to some labels to display simple information.
Sub dgv()
Dim sup2 = TextBox2.Text.Replace("#", "").Replace(">", " "c)
Dim sup() = sup2.Split(" "c, "#", vbCrLf, vbTab)
With DataGridView1
.Rows(0).Cells(0).Value = sup(1).ToString
.Rows(0).Cells(1).Value = sup(7).ToString
.Rows(0).Cells(3).Value = sup(4).ToString
End With
Button5.Enabled = True
Button6.Enabled = True
End Sub
This works just fine !!!
But when i try to populate the labels, with the code below, it just won't work!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Thread.Sleep(250)
Dim final = TextBox2.Text.Replace("#", "").Replace("SN", " "c)
Dim final2() = final.Split(" "c, "#", vbCrLf, vbTab)
Label1.Text = final2(0).ToString
Textbox2.Text= final2(0).ToString
End Sub
Can someone help me? The label gets no text.. and the textbox gets all of it.
Btw, the textbox is multiline and if i paste the text in microsoft word it comes with tabs and extra spaces.
Edit: printscreen from microsoft word below [ related to Multiline Textbox to Datagridview ]
Edit2: This is so strange ..
If i do this
Label1.Text = "Testing" & TextBox2.Text
it only shows "Testing" on the label..

If you set the label to AutoSize, it will automatically grow with whatever text you put in it. (This includes vertical growth.)
Before assign value to the label1,
Use the below code
Label1.MaximumSize = new Size(100, 0)
Label1.AutoSize = true
your code would be like
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Thread.Sleep(250)
Dim final = TextBox2.Text.Replace("#", "").Replace("SN", " "c)
Dim final2() = final.Split(" "c, "#", vbCrLf, vbTab)
Label1.MaximumSize = new Size(100, 0)
Label1.AutoSize = true
Label1.Text = final2(0).ToString
Textbox2.Text= final2(0).ToString
End Sub

Related

Get data from cmd, split the values and insert them into textboxes

I'm trying to read battery information from cmd. How can I extract each value and put them in their respective textbox, as shown in the image below?
Here's my code:
Private Results As String
Private Delegate Sub delUpdate()
Private Finished As New delUpdate(AddressOf UpdateText)
Private Sub UpdateText()
TextBox11.Text = Results
End Sub
Private Sub batt_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
CMDThread.Start()
End Sub
Private Sub CMDAutomate()
Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
StartInfo.FileName = "cmd" 'starts cmd window
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
StartInfo.UseShellExecute = False 'required to redirect
StartInfo.CreateNoWindow = False 'creates no cmd window
myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput
SW.WriteLine("adb shell dumpsys battery") 'the command you wish to run.....
SW.WriteLine("exit") 'exits command prompt window
Results = SR.ReadToEnd 'returns results of the command window
SW.Close()
SR.Close()
'invokes Finished delegate, which updates textbox with the results text
Invoke(Finished)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim value As String = TextBox11.Text
Dim topic_start As String = topic_start = value.LastIndexOf("AC Powered:") + 1
Dim topic As String = value.Substring(topic_start, value.Length - topic_start)
TextBox1.Text = topic.ToString
End Sub
Button1 is labeled Get Battery Information in the image.
Private Sub UpdateText()
Dim xList As New List(Of KeyValuePair(Of String, String))
Results = Results.Replace(vbLf, "")
Dim LineSplit() As String = Results.Split(vbCr)
For Each xLine As String In LineSplit
If xLine <> "" Then
xList.Add(New KeyValuePair(Of String, String)(xLine.Split(":")(0), xLine.Split(":")(1).Trim.Replace(" ", "")))
End If
Next
'do some work here to put the values in the right textboxes
End Sub
You can make yourself a custom control made of a label and a textbox that you use in your app. Then you just pass each entry of the list to your custom controls. Makes it easier to add or remove fields without having tons of IF then or a big Select Case
Here one way to extract values from text that is separated with new lines (Enviroment.NewLine) using IndexOf(), Substring() methods and Length property:
Private Sub UpdateGUI()
Dim lines As String() = TextBox1.Text.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
For Each itm In lines
MsgBox(itm.Substring(itm.IndexOf(":") + 1, itm.Length - itm.IndexOf(":") - 1))
Next
End Sub
Unless your TextBoxes are named exactly like the value in your return string (you'd have to convert the space to some other character anyways), there really isn't any way to do this except to manually code each case. Something like this should do it, just change the name of the TextBoxes accordingly:
Private Sub UpdateText()
Dim lines() As String = Results.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
For Each line In lines
Dim values() As String = line.Split(":")
Select Case values(0).ToUpper.Trim
Case "AC POWERED"
TextBox1.Text = values(1).ToUpper.Trim
Case "USB POWERED"
TextBox2.Text = values(1).ToUpper.Trim
Case "WIRELESS POWERED"
TextBox3.Text = values(1).ToUpper.Trim
Case "STATUS"
TextBox4.Text = values(1).ToUpper.Trim
Case "HEALTH"
TextBox5.Text = values(1).ToUpper.Trim
Case "PRESENT"
TextBox6.Text = values(1).ToUpper.Trim
Case "LEVEL"
TextBox7.Text = values(1).ToUpper.Trim
Case "SCALE"
TextBox8.Text = values(1).ToUpper.Trim
Case "VOLTAGE"
TextBox9.Text = values(1).ToUpper.Trim
Case "TEMPERATURE"
TextBox10.Text = values(1).ToUpper.Trim
Case "TECHNOLOGY"
TextBox11.Text = values(1).ToUpper.Trim
Case Else
MessageBox.Show(line, "Unknown Value")
End Select
Next
End Sub
If you named your TextBoxes in a predictable manner, then you can use Controls.Find() and shorten the code to the below. For instance, change spaces to underscore, and prepend "txt" in front: "txtAC_Powered", "txtUSB_Powered", "txtStatus", "txtHealth", etc. Case does NOT matter as a match will be found regardless. This means you're doing the manual work in naming your controls, as opposed to writing a long select case statement (or something else):
Private Sub UpdateText()
Dim lines() As String = Results.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
For Each line In lines
Dim values() As String = line.Split(":")
Dim ctlName As String = "txt" & values(0).Replace(" ", "_").Trim
Dim ctl As Control = Me.Controls.Find(ctlName, True).FirstOrDefault
If Not IsNothing(ctl) Then
ctl.Text = values(1).ToUpper.Trim
End If
Next
End Sub

How to increase Input box font size in VB.Net 2012

I am using VB2012, How can I increase the input box font size?
Dim inputBoxMessage As String = "You are picking part : "
For count = 0 To dgv_partDetails.Rows.Count - 1
If dgv_partDetails.Rows.Count = 0 Then
The following assumes you want to ask for user input outside the DataGridView. If this is not the case stop here.
Create a new form, add two button, set DialogResult for one to OK and the other to Cancel, do this in the property window for both buttons. Add a Label (set autosize to false) and TextBox, set the font to how you want them. Set FormBorderStyle = FixedToolWindow, set StartPosition to CenterParent.
Call the dialog as shown below
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As New frmUserInput
Dim Value As Integer = 0
Try
f.lblQuestion.Text = "your question"
f.Text = "Question"
' optional
f.txtInput.Text = "100"
If f.ShowDialog = Windows.Forms.DialogResult.OK Then
If Not String.IsNullOrWhiteSpace(f.txtInput.Text) Then
' Work with data, lets say we want a numeric
If Integer.TryParse(f.txtInput.Text, Value) Then
' use value varible
Else
MessageBox.Show("Not a valid integr [" & f.txtInput.Text & "]")
End If
End If
End If
Finally
f.Dispose()
End Try
End Sub
End Class
Or we can set properties on creation of the form
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As New frmUserInput
Dim Value As Integer = 0
Try
Dim specialFont As Font = New Font(
"Microsoft Sans Serif", 12.0!,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
f.lblQuestion.Text = "your question"
f.lblQuestion.Font = specialFont
f.lblQuestion.ForeColor = System.Drawing.Color.Red
f.Text = "Question"
' optional
f.txtInput.Text = "100"
If f.ShowDialog = Windows.Forms.DialogResult.OK Then
If Not String.IsNullOrWhiteSpace(f.txtInput.Text) Then
' Work with data, lets say we want a numeric
If Integer.TryParse(f.txtInput.Text, Value) Then
' use value varible
Else
MessageBox.Show("Not a valid integr [" & f.txtInput.Text & "]")
End If
End If
End If
Finally
f.Dispose()
End Try
End Sub
End Class
Personally I would wrap this code into a class or code module rather than in a form so it can be used in more than one spot in your code.

vb.net change combobox options depending on selected item in 2 previous comboboxes

I am creating a program which someone can input a search into a textbox and then narrow down the results using a series of comboboxes (or just use the comboboxes to search through everything).
The program looks like this: form 1
I have made the options in the 2nd combobox change using the following code:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim type As String = ComboBox1.SelectedItem
Dim make As String = ComboBox2.SelectedItem
Dim model As String = ComboBox3.SelectedItem
Dim version As String = TextBox2.Text
Dim memory As String = TextBox3.Text
Dim problem As String = TextBox4.Text
Dim casenumber As Integer = Int(Rnd() * 9999) + 1000
If type = "Phone" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E: \phone.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox2.Items.Add(q(i))
Next
ElseIf type = "Tablet" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\tablet.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox2.Items.Add(q(i))
Next
ElseIf type = "Desktop computer" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\pc.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox2.Items.Add(q(i))
Next
ElseIf type = "Laptop" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\laptop.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox2.Items.Add(q(i))
Next
'Else
'Dim objwriter As System.IO.StreamWriter
'objwriter = My.Computer.FileSystem.OpenTextFileWriter("E:\unknown.txt", True)
'File.AppendText("type:" And ComboBox1.Text And "make" & ComboBox2.Text And "model: " & ComboBox3.Text And "version: " And TextBox2.Text & "memory" And TextBox3.Text)
End If
End Sub
However,the code won't work to change what is in the 3rd box. I have repeated the following code for each option:
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
Dim type As String = ComboBox1.SelectedItem
Dim make As String = ComboBox2.SelectedItem
Dim model As String = ComboBox3.SelectedItem
Dim version As String = TextBox2.Text
Dim memory As String = TextBox3.Text
Dim problem As String = TextBox4.Text
If type = "Phone" Then
If make = "apple" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\apple.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox3.Items.Add(q(i))
Next
ElseIf make = "samsung" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\samsung.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox3.Items.Add(q(i))
Next
ElseIf make = "htc" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\htc.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox3.Items.Add(q(i))
Next
ElseIf make = "Nokia" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\Nokia.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox3.Items.Add(q(i))
Next
ElseIf make = "Blackberry" Then
ComboBox2.Items.Clear()
Dim file As New System.IO.StreamReader("E:\blackberry.txt")
For i = 1 To 20
q(i) = file.ReadLine() & vbNewLine
ComboBox3.Items.Add(q(i))
Next
I have checked the obvious problems like putting the wrong text file names and capital letters etc, but can't get this to work whatever I do.
Does anyone know why the third combobox remains blank even when both conditions are met (both combobox1 and 2 have the right thing selected)? Any suggestion would be much appreciated.
Firstly I suspect that this code :-
Dim type As String = ComboBox1.SelectedItem.ToString
Dim make As String = ComboBox2.SelectedItem.ToString
Dim model As String = ComboBox3.SelectedItem.ToString
Dim version As String = TextBox2.Text
Dim memory As String = TextBox3.Text
Dim problem As String = TextBox4.Text
shouldn't be in each if your events. They should really be placed somewhere that executes after all the information has been chosen yes?
OK for a start paste this into a text file called "device type.txt"
Phone,E:\phone.txt
Tablet,E:\tablet.txt
Desktop Computer,E:\pc.txt
Laptop,E:\laptop.txt
Then edit your phones.txt file and the rest of the above files to match that format e.g this phone.txt file
Apple,E:\apple.txt
Samsung,E:\samsung.txt
Htc,E:\htc.txt
Nokia,E\nokia.txt
Blackberry,E:\blackberry.txt
and so on for each item that links to another file. For the last files in the tree - which I presume are the files containing a list of the models for each brand of phone, just leave them as a simple list. No commas or anything after each model.
Use this code to do the populating of each ComboBox
Private Sub PopulateComboBox(ByRef cboBox As ComboBox, ByVal itemSource As String)
RemoveHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
RemoveHandler ComboBox2.SelectedIndexChanged, AddressOf ComboBox2_SelectedIndexChanged
RemoveHandler ComboBox3.SelectedIndexChanged, AddressOf ComboBox3_SelectedIndexChanged
Dim devices As New List(Of item)
Dim csvFlag As Boolean = False
cboBox.Items.Clear()
Using MyReader As New Microsoft.VisualBasic.
FileIO.TextFieldParser(itemSource)
If MyReader.ReadLine.Contains(",") Then csvFlag = True
End Using
Using MyReader As New Microsoft.VisualBasic.
FileIO.TextFieldParser(itemSource)
If csvFlag Then
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
End If
Dim currentRow As String() = {"", ""}
While Not MyReader.EndOfData
Try
If csvFlag Then
currentRow = MyReader.ReadFields()
Dim tempItem As New item
tempItem.item = currentRow(0)
tempItem.fileName = currentRow(1)
devices.Add(tempItem)
Else
currentRow(0) = MyReader.ReadLine
Dim tempItem As New item
tempItem.item = currentRow(0)
tempItem.fileName = ""
devices.Add(tempItem)
End If
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
End Try
End While
End Using
If csvFlag Then
cboBox.DataSource = devices
cboBox.DisplayMember = "item"
cboBox.ValueMember = "fileName"
Else
cboBox.DataSource = devices
cboBox.DisplayMember = "item"
cboBox.ValueMember = "item"
End If
AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
AddHandler ComboBox2.SelectedIndexChanged, AddressOf ComboBox2_SelectedIndexChanged
AddHandler ComboBox3.SelectedIndexChanged, AddressOf ComboBox3_SelectedIndexChanged
End Sub
What it does is firstly check to see if the file being read is a comma-delimited file.
If it is delimited, it will read the file referred to in itemSource and expect a pair of values. The first value is what you see in the box(the DisplayMember), and the second value is what clicking on the box actually returns(the ValueMember)
If the file isn't comma-delimited, it will just read each line and add it to the combobox so that it acts normally (this will be important for the last files in the tree)
Next you need these methods for the ComboBoxes
Private Sub PopulateComboBox1()
PopulateComboBox(ComboBox1, "E:\device type.txt")
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
PopulateComboBox(ComboBox2, ComboBox1.SelectedValue.ToString)
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
PopulateComboBox(ComboBox3, ComboBox2.SelectedValue.ToString)
End Sub
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
PopulateComboBox(ComboBox3, ComboBox2.SelectedValue.ToString)
End Sub
Call the method to populate Combobox1 possibly in the form's load event.

Read .txt file and display into different button

I'm trying to display some text on my button but I can only display those text on 1 button. My buttons are separated like Button1.Text, Button2.Text, and Button3.Text and my .txt file can only be displayed in Button1.Text. Here is the code that I did so far.
Private Sub FormMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim R As New IO.StreamReader("TestFile.txt")
Button1.Text = R.ReadToEnd()
R.Close()
End Sub
Inside my .txt file got something like
First Button
Second Button
Third Button
and I want my system be able to read them and display into each of the button. How to do that? Thank you.
Use something like...
Private Sub FormMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim lines() As String = System.IO.File.ReadAllLines("TestFile.txt")
For i As Integer = 1 To lines.Length
Dim ctl As Control = Me.Controls.Find("Button" & i, True).FirstOrDefault
If Not IsNothing(ctl) Then
ctl.Text = lines(i - 1)
End If
Next
End Sub
Private Sub FormMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim R As New IO.StreamReader("TestFile.txt")
Dim words As String() = R.ReadToEnd().Split(New String() {Environment.NewLine})
Button1.Text = words(0)
Button2.Text = words(1)
Button3.Text = words(2)
R.Close()
End Sub

How do I read contents of a DataTable and assign them to other controls?

Good morning,
I've been up all night trying to figure this out on my own without bugging anybody else, but I can't.
I've been successful in querying my MySQL database and gotten a set of records into a DataTable (dbTable). During debugging, I can see its contents so I know the data is there. Initially, the DataTable is used to populate a ListView control I have on my form.
When I select a record, I want the contents of the DataTable (or the query I just ran) to be assigned to some TextBox controls. I can't seem to figure out how to do this. Any help would be greatly appreciated.
UPDATE TO ADD IMAGES:
I'm hoping these screenshots will give an idea of what I'm looking to do. The first image shows what happens after an account number has been entered. The second box shows a Groupbox expanded to reveal the form fields after a record has been selected in the ListView.
The control names are: TextBoxCustomer, TextBoxLastName, TextBoxFirstName, ComboBoxSalutation, ComboBoxCardType, TextBoxCard.Text, TextBoxExpireMonth, TextBoxExpireYear, TextBoxCVV2.
The field names in the DataTable (dbTable) are: nameCOMPANY, nameLAST, nameFIRST, nameSALUTATION, ccType, ccNumber, ccExpireMonth, ccExpireYear, ccCode.
IMAGE 1:
IMAGE 2:
Have you tried this?
TextBox1.Text = dbTable.Rows(0)("ColumnName").ToString()
TextBox2.Text = dbTable.Rows(1)("OtherColumnName").ToString()
You can also do this:
Dim row as DataRow = dbTable.Rows(0)
TextBox1.Text = row("ColumnName").ToString()
row = dbTable.Rows(1)
TextBox2.Text = row("OtherColumnName").ToString()
You could also DataBind to a DataGrid (or similar control) using dbTable as the DataSource and then set the DataGrid.EditMode to True. This would create the textbox controls for you.
UPDATE:
Try something like this to bind your textboxes to the selected values of your ListView:
Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
Dim item As ListViewItem = Nothing
Dim tb As TextBox = Nothing
Dim i As Integer = 0
For Each item In ListView1.SelectedItems
tb = Me.Controls.Find("TextBox" & i.ToString, True)(0)
If tb IsNot Nothing Then
tb.Text = item.Text
End If
i += 1
Next
End Sub
UPDATE:
This is a little more error-proof, but this routine will only work if your textboxes are named TextBox1, TextBox2, TextBox3, etc.:
Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
Dim item As ListViewItem = Nothing
Dim found() As Control = Nothing
Dim tb As TextBox = Nothing
Dim i As Integer = 0
For Each item In ListView1.SelectedItems
found = Me.Controls.Find("TextBox" & i.ToString, True)
If found.Length > 0 Then
tb = TryCast(found(0), TextBox)
Else
tb = Nothing
End If
If tb IsNot Nothing Then
tb.Text = item.Text
End If
i += 1
Next
End Sub
UPDATE:
Okay, thanks to the screenshots, I am assuming that your ListView.MultiSelect = False, so only one item can be selected at a time. Given that, the following should work as long as the textboxes and ListView columns are named correctly:
Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
Dim item As ListViewItem = Nothing
If ListView1.SelectedItems.Count = 1 Then
item = ListView1.SelectedItems(0)
txtCardNumber.Text = item.SubItems("CARD NUMBER")
txtCardExpirationMonth.Text = item.SubItems("EXP MO")
txtCardExpirationYear.Text = item.SubItems("EXP YEAR")
End If
End Sub
Hello guys/gals,
With tremendous assistance from Pete, I was able to modify the suggested answer and I achieved the desired solution. To prevent the horizontal scrollbars from displaying, I didn't add columns to the Listview (from the Designer). Instead, I added the fields to the Listview programatically - this way they were available for selection.
The main trouble I ran into was figuring out the Index numbers of the fields. I had to debug several times to figure out what the numbers were - so if anybody knows of a better way please do share.
Here're the two codes I used (thanks Pete):
Private Sub loadCard()
Try
'FOR MySQL DATABASE USE
Dim dbQuery As String = ""
Dim dbCmd As New MySqlCommand
Dim dbAdapter As New MySqlDataAdapter
Dim dbTable As New DataTable
Dim i As Integer
If dbConn.State = ConnectionState.Closed Then
dbConn.ConnectionString = String.Format("Server={0};Port={1};Uid={2};Password={3};Database=accounting", FormLogin.ComboBoxServerIP.SelectedItem, My.Settings.DB_Port, My.Settings.DB_UserID, My.Settings.DB_Password)
dbConn.Open()
End If
dbQuery = "SELECT *" & _
"FROM cc_master INNER JOIN customer ON customer.accountNumber = cc_master.customer_accountNumber " & _
"WHERE customer.accountNumber = '" & TextBoxAccount.Text & "'"
With dbCmd
.CommandText = dbQuery
.Connection = dbConn
End With
With dbAdapter
.SelectCommand = dbCmd
.Fill(dbTable)
End With
ListViewCard.Items.Clear()
For i = 0 To dbTable.Rows.Count - 1
With ListViewCard
.Items.Add(dbTable.Rows(i)("ccID"))
With .Items(.Items.Count - 1).SubItems
.Add(dbTable.Rows(i)("ccNumber"))
.Add(dbTable.Rows(i)("ccExpireMonth"))
.Add(dbTable.Rows(i)("ccExpireYear"))
.Add(dbTable.Rows(i)("ccCode"))
.Add(dbTable.Rows(i)("ccType"))
.Add(dbTable.Rows(i)("ccAuthorizedUseStart"))
.Add(dbTable.Rows(i)("ccAuthorizedUseEnd"))
.Add(dbTable.Rows(i)("nameCOMPANY"))
.Add(dbTable.Rows(i)("nameSALUTATION"))
.Add(dbTable.Rows(i)("nameLAST"))
.Add(dbTable.Rows(i)("nameFIRST"))
End With
End With
Next
If dbTable.Rows.Count = 0 Then
LabelNoCard.Visible = True
LabelNoCard.Focus()
TextBoxAccount.Focus()
Me.Refresh()
Else
If dbTable.Rows.Count > 1 Then
LabelNoCard.Visible = False
LabelMultipleCards.Visible = True
ListViewCard.Visible = True
Me.Refresh()
End If
End If
Catch ex As MySqlException
MessageBox.Show("A DATABASE ERROR HAS OCCURED" & vbCrLf & vbCrLf & ex.Message & vbCrLf & _
vbCrLf + "Please report this to the IT/Systems Helpdesk at Ext 131.")
End Try
dbConn.Close()
End Sub
Here's the second one:
Private Sub ListViewCard_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListViewCard.SelectedIndexChanged
GroupBox2.Visible = True
Dim item As ListViewItem = Nothing
If ListViewCard.SelectedItems.Count = 1 Then
item = ListViewCard.SelectedItems(0)
TextBoxCustomer.Text = item.SubItems(8).Text
TextBoxLastName.Text = item.SubItems(10).Text
TextBoxFirstName.Text = item.SubItems(11).Text
ComboBoxSalutation.Text = item.SubItems(9).Text
ComboBoxCardType.Text = item.SubItems(5).Text
TextBoxCard.Text = item.SubItems(1).Text
TextBoxExpireMonth.Text = item.SubItems(2).Text
TextBoxExpireYear.Text = item.SubItems(3).Text
TextBoxCVV2.Text = item.SubItems(4).Text
DateTimePickerStartDate.Text = item.SubItems(6).Text
DateTimePickerEndDate.Text = item.SubItems(7).Text
End If
End Sub