Pulling Select Case data from one sub to another - vb.net

Hello all My question is: Is it possible to pull case select data from another sub? I have this in a button sub:
'Public Sub btnSearch_Click(sender As Object, e As EventArgs) Handles
btnSearch.Click
Dim strInvalid As String = txtUsername.Text
Dim frmIdenitityCheckerResults As New frmIdenitityCheckerResults()
Select Case True
Case txtUsername.Text = (ReadALine(filename, GetNumberOfLines(filename),
0))
Me.Close()
My.Forms.frmIdenitityCheckerResults.TopLevel = False
My.Forms.FrmWorkTool.Panel2.Controls.Add
(My.Forms.frmIdenitityCheckerResults)
my.Forms.frmIdenitityCheckerResults.Show()
My.Forms.frmIdenitityCheckerResults.Label2.Text = "John"
My.Forms.frmIdenitityCheckerResults.lblSSN.Text = "1211"
My.Forms.frmIdenitityCheckerResults.lblCompany.Text = strPhoenix
My.Forms.frmIdenitityCheckerResults.lblEMP.Text = "1"
My.Forms.frmIdenitityCheckerResults.lblStartDate.Text = "10/10/2017"
Case txtUsername.Text = (ReadALine(filename, GetNumberOfLines(filename), 2))
Me.Close()
My.Forms.frmIdenitityCheckerResults.TopLevel = False
My.Forms.FrmWorkTool.Panel2.Controls.Add
(My.Forms.frmIdenitityCheckerResults)
My.Forms.frmIdenitityCheckerResults.Show()
My.Forms.frmIdenitityCheckerResults.Label2.Text = "Terri"
My.Forms.frmIdenitityCheckerResults.lblSSN.Text = "4218"
My.Forms.frmIdenitityCheckerResults.lblCompany.Text = strPhoenix
My.Forms.frmIdenitityCheckerResults.lblEMP.Text = "2"
My.Forms.frmIdenitityCheckerResults.lblStartDate.Text = "10/10/2017"'
Case txtUsername.Text = (ReadALine(filename, GetNumberOfLines(filename), 1))
Link_lbl1.Text = "John"
Case txtUsername.Text = (ReadALine(filename, GetNumberOfLines(filename), 3)) OrElse
txtUsername.Text = (ReadALine(filename, GetNumberOfLines(filename), 3)) OrElse
txtUsername.Text = (ReadALine(filename, GetNumberOfLines(filename), 3)) OrElse
txtUsername.Text = (ReadALine(filename, GetNumberOfLines(filename), 3))
Link_lbl1.Text = "Terri"
Link_lbl2.Text = "Jordan"
Link_lbl3.Text = "Lisa"
Link_lbl4.Text = "David"
I'm wanting to pull data from this Select Case to input in the code below
Private Sub link_lbl1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles Link_lbl1.LinkClicked
Select Case
Case Link_lbl1.Text = "John"
btnSearch 1
Case Link_lbl1.Text = "Terri"
End Select
End Sub'
But I'm unsure if this is a possibility or not if anyone can point me in the right direction It'll be much appreciated.
I attempted:
Select Case link_lbl1.text
Case "John"
But that threw an error and wasn't pulling data from my previous case select.
I can make it work using the below but didn't know if there was an easier way to go about it.
Private Sub link_lbl1_LinkClicked(sender As Object, e As
LinkLabelLinkClickedEventArgs) Handles Link_lbl1.LinkClicked
Select Case True
Case Link_lbl1.Text = "John"
Me.Close()
My.Forms.frmIdenitityCheckerResults.TopLevel = False
My.Forms.FrmWorkTool.Panel2.Controls.Add(My.Forms.frmIdenitityCheckerResults)
My.Forms.frmIdenitityCheckerResults.Show()
My.Forms.frmIdenitityCheckerResults.Label2.Text = "John"
My.Forms.frmIdenitityCheckerResults.lblSSN.Text = "1211"
My.Forms.frmIdenitityCheckerResults.lblCompany.Text = strPhoenix
My.Forms.frmIdenitityCheckerResults.lblEMP.Text = "1"
My.Forms.frmIdenitityCheckerResults.lblStartDate.Text = "10/10/2017"
Case Link_lbl1.Text = "Terri"
Me.Close()
My.Forms.frmIdenitityCheckerResults.TopLevel = False
My.Forms.FrmWorkTool.Panel2.Controls.Add(My.Forms.frmIdenitityCheckerResults)
My.Forms.frmIdenitityCheckerResults.Show()
My.Forms.frmIdenitityCheckerResults.Label2.Text = "Terri"
My.Forms.frmIdenitityCheckerResults.lblSSN.Text = "4218"
My.Forms.frmIdenitityCheckerResults.lblCompany.Text = strPhoenix
My.Forms.frmIdenitityCheckerResults.lblEMP.Text = "2"
My.Forms.frmIdenitityCheckerResults.lblStartDate.Text = "10/10/2017"
End Select
End Sub'

As suggested, your Select Case is wrong to begin with. Rather than this:
Select Case True
Case Link_lbl1.Text = "John"
'...
Case Link_lbl1.Text = "Terri"
'...
End Select
You should be doing this:
Select Case Link_lbl1.Text
Case "John"
'...
Case "Terri"
'...
End Select
As for the question, I assume that what you mean is that you want to avoid repeating yourself in each Case block. If so then the first thing to do is to identify what is common and what is specific. You can copy the common part into a method and then add a parameter for each specific part, e.g.
Private Sub DoSomething(label2Text As String, ssn As String, emp As String)
With My.Forms.frmIdenitityCheckerResults
.TopLevel = False
.Show()
.Label2.Text = label2Text
.lblSSN.Text = ssn
.lblCompany.Text = strPhoenix
.lblEMP.Text = emp
.lblStartDate.Text = "10/10/2017"
End With
My.Forms.FrmWorkTool.Panel2.Controls.Add(My.Forms.frmIdenitityCheckerResults)
Me.Close()
End Sub
You can then just call that method in each Case block and pass in the appropriate values for each argument.
Note that I have also used a With block to neaten up that code and reordered things more appropriately too.

Related

How to verify the string value from a richtextbox

Original Code:
The code below always runs Case Else because the text values have an exclamation in them. It I were to remove them, then they would work again.
Private Sub CommandLineFunctions()
Select Case True
Case DeveloperCommandLine.Text = "!exit"
ToolStripButtonClose.PerformClick()
Case DeveloperCommandLine.Text = "!clear"
MessageDisplayBounds.Text = String.Empty
Case DeveloperCommandLine.Text = "!disable"
DeveloperMode.Enabled = False
CloseForm(ToolStripButtonClose, EventArgs.Empty)
Case Else
MessageDisplayBounds.Text += DeveloperCommandLine.Text & vbCr
ScrollableContentCollection.VerticalScroll.Value = ScrollableContentCollection.VerticalScroll.Maximum
End Select
DeveloperCommandLine.Text = String.Empty
End Sub
Edit
Trying the suggested method (seen below) also only runs Case Else.
Private Sub CommandLineFunctions()
Select Case True
Case DeveloperCommandLine.Text.Equals("!exit", StringComparison.CurrentCultureIgnoreCase)
ToolStripButtonClose.PerformClick()
Case DeveloperCommandLine.Text.Equals("!clear", StringComparison.CurrentCultureIgnoreCase)
MessageDisplayBounds.Text = String.Empty
Case DeveloperCommandLine.Text.Equals("!disable", StringComparison.CurrentCultureIgnoreCase)
DeveloperMode.Enabled = False
CloseForm(ToolStripButtonClose, EventArgs.Empty)
Case Else
MessageDisplayBounds.Text += DeveloperCommandLine.Text & vbCr
ScrollableContentCollection.VerticalScroll.Value = ScrollableContentCollection.VerticalScroll.Maximum
End Select
DeveloperCommandLine.Text = String.Empty
End Sub
Try this instead of the Select Case:
If DeveloperCommandLine.Text.ToLower().Contains("exit") Then
ToolStripButtonClose.PerformClick()
ElseIf DeveloperCommandLine.Text.ToLower().Contains("clear") Then
MessageDisplayBounds.Text = String.Empty
ElseIf DeveloperCommandLine.Text.ToLower().Contains("disable") Then
DeveloperMode.Enabled = False
CloseForm(ToolStripButtonClose, EventArgs.Empty)
Else
MessageDisplayBounds.Text += DeveloperCommandLine.Text & vbCr
ScrollableContentCollection.VerticalScroll.Value = ScrollableContentCollection.VerticalScroll.Maximum
End If
So I figured it out by looping through all Char's in the string. Apparently Environment.NewLine, vbcr, or something similar gets added to the end of the string. To resolve this I used the following code:
Private Sub CommandLineFunctions()
Dim GetChars As New List(Of String)
For Each i As Char In DeveloperCommandLine.Text
GetChars.Add(i)
Next
GetChars.Remove(GetChars.Last)
Dim CleanInput As String = String.Join("", GetChars)
Select Case True
Case CleanInput = "!exit"
ToolStripButtonClose.PerformClick()
Case CleanInput = "!clear"
MessageDisplayBounds.Text = String.Empty
Case CleanInput = "!disable"
DeveloperMode.Enabled = False
CloseForm(ToolStripButtonClose, EventArgs.Empty)
Case Else
MessageDisplayBounds.Text += DeveloperCommandLine.Text & vbCr
ScrollableContentCollection.VerticalScroll.Value = ScrollableContentCollection.VerticalScroll.Maximum
End Select
DeveloperCommandLine.Text = String.Empty
End Sub

>= Message Box sequence not working properly

I'm trying to make a messagebox pop up when "Trade-in Value" >= "Price." If I enter Trade-in Value:2000 and Price:12,000. The messagebox comes up, when clearly that should not be happening. After testing a bunch of numbers, it seems like the form doesn't like the number "2" in either text box. The second block is where I have my code that does not work.
I am fairly new to coding and this is my first post, taker easy boy/girls.
Entered numbers/Form
Message Box error
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
GBTradeIn.Enabled = False
TxtTradeIn.Enabled = False
RBStandard.Checked = True
RBExcellent.Checked = True
End Sub
Private Sub CBTradeIn_CheckedChanged(sender As Object, e As EventArgs) Handles CBTradeIn.CheckedChanged
GBTradeIn.Enabled = True
TxtTradeIn.Enabled = True
If CBTradeIn.Checked = (False) Then
GBTradeIn.Enabled = False
TxtTradeIn.Enabled = False
RBExcellent.Checked = True
TxtTradeIn.Text = ""
End If
End Sub
Private Sub BtnCalculate_Click(sender As Object, e As EventArgs) Handles BtnCalculate.Click
Try
If CBTradeIn.Checked = (False) Then
TxtTradeIn.Text = 0
End If
If RBExcellent.Checked Then
TxtTradeInAllowance.Text = FormatCurrency(TxtTradeIn.Text * 1)
ElseIf RBGood.Checked Then
TxtTradeInAllowance.Text = FormatCurrency(TxtTradeIn.Text * 0.9)
ElseIf RBFair.Checked Then
TxtTradeInAllowance.Text = FormatCurrency(TxtTradeIn.Text * 0.8)
ElseIf RBPoor.Checked Then
TxtTradeInAllowance.Text = FormatCurrency(TxtTradeIn.Text * 0.7)
End If
Catch
MessageBox.Show("Enter valid Trade-In value")
End Try
Dim Exterior As Decimal
Dim Accessories As Decimal
If CBPremiumStereo.Checked And CBLeatherInterior.Checked And CBGPS.Checked Then
Accessories = 3154.4
ElseIf CBPremiumStereo.Checked And CBLeatherInterior.Checked Then
Accessories = 1413.17
ElseIf CBPremiumStereo.Checked And CBGPS.Checked Then
Accessories = 2166.99
ElseIf CBGPS.Checked And CBLeatherInterior.Checked Then
Accessories = 2728.64
ElseIf CBPremiumStereo.Checked Then
Accessories = 425.76
ElseIf CBLeatherInterior.Checked Then
Accessories = 987.41
ElseIf CBGPS.Checked Then
Accessories = 1741.23
End If
If RBStandard.Checked = True Then
Exterior = 0
ElseIf RBPearlized.Checked = True Then
Exterior = 345.72
ElseIf RBCustom.Checked = True Then
Exterior = 599.99
End If
TxtAccessoriesAndFinish.Text = FormatCurrency(Accessories + Exterior)
Try
Dim total As Decimal
Dim TradeIn As Decimal
Dim AccessoriesandExterior As Decimal
Dim Subtotal As Decimal
total = TxtPrice.Text
TradeIn = TxtTradeInAllowance.Text
AccessoriesandExterior = TxtAccessoriesAndFinish.Text
TxtSubtotal.Text = FormatCurrency(total + AccessoriesandExterior - TradeIn)
Subtotal = TxtSubtotal.Text
TxtSalesTax.Text = FormatCurrency((Subtotal + TradeIn) * 0.08)
TxtAmountDue.Text = FormatCurrency(Subtotal + TxtSalesTax.Text)
Catch
TxtAccessoriesAndFinish.Text = ""
TxtAmountDue.Text = ""
TxtTradeIn.Text = ""
TxtSubtotal.Text = ""
TxtSalesTax.Text = ""
TxtPrice.Text = ""
TxtTradeInAllowance.Text = ""
MessageBox.Show("Enter a valid price")
End Try
>
If TxtTradeIn.Text >= TxtPrice.Text Then
TxtAccessoriesAndFinish.Text = ""
TxtAmountDue.Text = ""
TxtTradeIn.Text = ""
TxtSubtotal.Text = ""
TxtSalesTax.Text = ""
TxtPrice.Text = ""
TxtTradeInAllowance.Text = ""
MessageBox.Show("Trade in value can't be higher than or equal to price")
End If
If CBTradeIn.Checked = (False) Then
TxtTradeIn.Text = ""
End If
End Sub
>
Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
TxtAccessoriesAndFinish.Text = ""
TxtAmountDue.Text = ""
TxtTradeIn.Text = ""
TxtSubtotal.Text = ""
TxtSalesTax.Text = ""
TxtPrice.Text = ""
RBStandard.Checked = True
RBExcellent.Checked = True
CBPremiumStereo.Checked = False
CBLeatherInterior.Checked = False
CBGPS.Checked = False
CBTradeIn.Checked = False
TxtTradeInAllowance.Text = ""
End Sub
End Class
Instead of
If TxtTradeIn.Text >= TxtPrice.Text Then
use
If CDec(TxtTradeIn.Text) >= CDec(TxtPrice.Text) Then
CDec() converts the text inside the brackets to a decimal, allowing you to do numerical comparison rather than string comparison.
However, you must be careful: CDec() will return an error if you try to convert a non decimal to a decimal, such as a string, so you will have to check that the user has entered a valid decimal in the textboxes

Index is outside the bounds of the array VB 2015

I'm getting this error message, and it points to this line
Index was outside the bounds of the array
bookinfo(i).Author = parts(1)
Could someone help me understand what I'm doing wrong?
Thank you!
Private Sub JamesProject1_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
Dim parts() As String
lines = IO.File.ReadAllLines("LibraryDatabase.txt")
ReDim Preserve bookinfo(lines.Count)
If Not OpenFileDialog1.ShowDialog() = DialogResult.OK Then
MessageBox.Show("You must choose a file")
Me.Close()
End If
lines = IO.File.ReadAllLines(OpenFileDialog1.FileName)
For i = 0 To lines.Count - 1
parts = lines(i).Split(","c)
bookinfo(i).Title = parts(0)
bookinfo(i).Author = parts(1)
bookinfo(i).ISBN = parts(2)
bookinfo(i).YearPublished = parts(3)
Next
Dim query = From book In bookinfo
Order By book.Title, book.Author, book.ISBN, book.YearPublished
Select book.Title, book.Author, book.ISBN, book.YearPublished
DGVBookInfo.DataSource = query.ToList
DGVBookInfo.Columns("Title").HeaderText = "Title"
DGVBookInfo.Columns("Author").HeaderText = "Author"
DGVBookInfo.Columns("ISBN").HeaderText = "ISBN"
DGVBookInfo.Columns("Year Published").HeaderText = "Year Published"
DGVBookInfo.AutoResizeColumns()
DGVBookInfo.RowHeadersVisible = False
End Sub

Issue with combobox autocomplete in vb.net

I have a combobox populated by a datatable, the code searches for a text string located at any position of the field while the user is writing, so far no problem.
So the problem is: When I write the third character the combobox autocompletes with the first result, and there is no way to type anything else.
I have tried already using all AutocompleteMode & AutocompleteSourse properties settings and combinations.
That’s why I’m asking for help.
The code is below:
Private Sub ComboListadoRemitente_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboListadoRemitente.KeyUp
Dim strText As String
strText = ComboListadoRemitente.Text
If Len(strText) > 2 Then
ComboListadoRemitente.DataSource = dtListado.Select("listado LIKE '%" & strText & "%'")
ComboListadoRemitente.DroppedDown = True
Cursor.Current = Cursors.Default
End If
End Sub
Thanks
Finally I got something that works well, it is not the final version, surely can be further improved, here is the code:
Public Sub ComboListadoRemitente_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboListadoRemitente.KeyUp
Dim strText As String
strText = ComboListadoRemitente.Text
If ComboListadoRemitente.Text = "" Then
ComboListadoRemitente.DataSource = Me.dtListado
ComboListadoRemitente.ValueMember = "Id"
ComboListadoRemitente.DisplayMember = "listado"
ComboListadoRemitente.SelectedIndex = -1
ComboListadoRemitente.DroppedDown = False
End If
If Len(strText) > 2 Then
ComboListadoRemitente.DataSource = dtListado.Select("listado LIKE '%" & strText & "%'")
ComboListadoRemitente.ValueMember = "Id"
ComboListadoRemitente.DisplayMember = "listado"
If ComboListadoRemitente.Items.Count <> 0 Then
ComboListadoRemitente.DroppedDown = True
ComboListadoRemitente.SelectedIndex = -1
ComboListadoRemitente.Text = ""
ComboListadoRemitente.SelectedText = strText
strText = ""
Cursor.Current = Cursors.Default
Else
ComboListadoRemitente.DataSource = Me.dtListado
ComboListadoRemitente.ValueMember = "Id"
ComboListadoRemitente.DisplayMember = "listado"
ComboListadoRemitente.SelectedIndex = -1
ComboListadoRemitente.Text = ""
ComboListadoRemitente.SelectedText = strText
strText = ""
ComboListadoRemitente.DroppedDown = False
End If
End If
End Sub

Select from table Syntax with multiple paramaters

I need help with getting my syntax correct on the myDataAdapter.SelectCommand.CommandText line. I cannot seem to get the date to work as my second parameter, before it would show if a person was in a seat of my theatre, now I made more dates for shows so I need to check the seat and the date now and I cannot seem to get the date check to work:
Private Sub lblSeat1_MouseEnter(sender As Object, e As EventArgs) Handles lblSeat1.MouseEnter
'NEED HELP ON THIS LINE BELOW
myDataAdapter.SelectCommand.CommandText = ("select * from seating where seat_no = " & seatNumber(0) And "select * from seating where perf_date = " & lstPerfDates.SelectedIndex)
myDataSet.Clear()
myDataAdapter.Fill(myDataSet)
If myDataSet.Tables(0).Rows.Count = 0 Then
lblSeat1.BackColor = Color.Green
ToolTipSeats.SetToolTip(lblSeat1, "Available")
ElseIf myDataSet.Tables(0).Rows.Count = 1 Then
lblSeat1.BackColor = Color.Red
ToolTipSeats.SetToolTip(lblSeat1, myDataSet.Tables(0).Rows(0)("patron"))
End If
End Sub
Changed to use parameters, and missing .item before ("patron")
Private Sub lblSeat1_MouseEnter(sender As Object, e As EventArgs) Handles lblSeat1.MouseEnter
'NEED HELP ON THIS LINE BELOW
myDataAdapter.SelectCommand.CommandText = ("select * from seating where seat_no = #seatNumber and perf_date #perfDate")
myDataAdapter.SelectCommand.CommandType = CommandType.Text
myDataAdapter.SelectCommand.Parameters.AddWithValue("#seatNumber", seatNumber(0))
myDataAdapter.SelectCommand.Parameters.AddWithValue("#perfDate", lstPerfDates.SelectedValue)
myDataSet.Clear()
myDataAdapter.Fill(myDataSet)
If myDataSet.Tables(0).Rows.Count = 0 Then
lblSeat1.BackColor = Color.Green
ToolTipSeats.SetToolTip(lblSeat1, "Available")
ElseIf myDataSet.Tables(0).Rows.Count = 1 Then
lblSeat1.BackColor = Color.Red
ToolTipSeats.SetToolTip(lblSeat1, myDataSet.Tables(0).Rows(0).item("patron"))
End If
End Sub