vb.net If Else statement - vb.net

I have this if else statement that I need help with.
What I would like to happen is the user enters a number in data grid view columnIndex 0. If the first validation (If CheckSatus(chkValue)) fails turn the back color red and stop. If its valid I want to continue to CheckRelease(chkValue). Right now if its invalid it goes to CheckRelease(chkValue) and change the back color yellow.
Current Code:
Private Sub gridUserEntries_CellLeave(sender As Object, e As DataGridViewCellEventArgs) Handles gridUserEntries.CellLeave
'Validate Release Number is validate and that Release is in F4 screen
If (e.ColumnIndex = 0) Then
Dim currCell As DataGridViewCell = gridUserEntries.CurrentCell
Dim chkValue As String = currCell.GetEditedFormattedValue(currCell.RowIndex, DataGridViewDataErrorContexts.Display)
If Not (chkValue.Trim = "") Then
'Validate if release is in jobscopedb.IPJOBM table
If CheckSatus(chkValue) Then
currCell.Style.BackColor = Color.White
Else
currCell.Style.BackColor = Color.Red
btnUpdatePPUSRFS.Enabled = False
btnClear.Enabled = True
End If
'Validate if the Release Number is in the PPUSRFS TABLE
If CheckRelease(chkValue) Then
currCell.Style.BackColor = Color.White
btnValidate.Enabled = True
btnRetrieve.Enabled = True
btnClear.Enabled = True
Else
currCell.Style.BackColor = Color.Yellow
btnInsertPPUSRFS.Enabled = True
btnValidate.Enabled = False
btnRetrieve.Enabled = False
End If
Else
currCell.Style.BackColor = Color.White
End If
End If

Simplify each test by only looking for the failure condition (no Else block), and then use a Return statement to stop processing. Then put the success code just once, after all the tests:
If (e.ColumnIndex = 0) Then
Dim currCell As DataGridViewCell = gridUserEntries.CurrentCell
Dim chkValue As String = currCell.GetEditedFormattedValue(currCell.RowIndex, DataGridViewDataErrorContexts.Display)
'Validate if release is in jobscopedb.IPJOBM table
If String.IsNullOrWhitespace(chkValue) Then
currCell.Style.BackColor = Color.Red
btnUpdatePPUSRFS.Enabled = False
btnClear.Enabled = True
Return ' or Exit Sub
End If
'Validate if the Release Number is in the PPUSRFS TABLE
If Not CheckRelease(chkValue) Then
currCell.Style.BackColor = Color.Yellow
btnInsertPPUSRFS.Enabled = True
btnValidate.Enabled = False
btnRetrieve.Enabled = False
Return
End If
' Add as many other tests as you need
'Made it this far means success
currCell.Style.BackColor = Color.White
btnValidate.Enabled = True
btnRetrieve.Enabled = True
btnClear.Enabled = True
End If
Even better, use a boolean so you also only need the failure code once:
If (e.ColumnIndex = 0) Then
Dim currCell As DataGridViewCell = gridUserEntries.CurrentCell
Dim chkValue As String = currCell.GetEditedFormattedValue(currCell.RowIndex, DataGridViewDataErrorContexts.Display)
'Validate if release is in jobscopedb.IPJOBM table
Dim Success As Boolean = Not String.IsNullOrWhitespace(chkValue)
'Validate if the Release Number is in the PPUSRFS TABLE
Success = Success AndAlso CheckRelease(chkValue)
' Add as many other tests as you need
If Success Then
currCell.Style.BackColor = Color.White
btnValidate.Enabled = True
btnRetrieve.Enabled = True
btnClear.Enabled = True
Else
currCell.Style.BackColor = Color.Yellow
btnInsertPPUSRFS.Enabled = True
btnValidate.Enabled = False
btnRetrieve.Enabled = False
End If
End If
Note the use of AndAlso, which short-circuits, meaning it will stop evaluating the expression as soon as it knows that logical result (when the first part is False). The CheckRelease() only runs if Success is True. We can use this to further reduce the code:
Dim Success As Boolean =
Not String.IsNullOrWhitespace(chkValue) AndAlso
CheckRelease(chkValue) AndAlso
SomeOtherTest() AndAlso
AsManyAsYouNeed()

Related

What is lacking in my VBA code? Looking to have multiple checkboxes that when one is selected, it hides all other rows

Brand new to coding junk in VBA for Microsoft Word. I have a table with 12 rows and I want to place a standard content control checkbox next to each row, and when any given checkbox is checked, the other rows disappear.
Currently I've had good luck at this with purely text, but trying to bookmark to hide an entire row of a table only seems to work for the very first checkbox. (Sorry if my code is more complicated than it needs to be. I also skipped pasting all of the code since the other 10 lines are the same, so the final 12 End Ifs are necessary):
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim cc As ContentControl
For Each cc In ActiveDocument.ContentControls
If cc.Title = "impact" Then
If cc.Checked = True Then
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = True
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = True
ActiveDocument.Bookmarks("license").Range.Font.Hidden = True
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("blank").Range.Font.Hidden = True
ActiveDocument.Bookmarks("impact").Range.Font.Hidden = False
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = False
Else: ActiveDocument.Bookmarks("impact").Range.Font.Hidden = False
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = False
ActiveDocument.Bookmarks("license").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("blank").Range.Font.Hidden = False
End If
Exit Sub
Else: If cc.Title = "license" Then
If cc.Checked = True Then
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = True
ActiveDocument.Bookmarks("impact").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = True
ActiveDocument.Bookmarks("license").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("blank2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("blank3").Range.Font.Hidden = True
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = True
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = False
Else: ActiveDocument.Bookmarks("impact").Range.Font.Hidden = False
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = False
ActiveDocument.Bookmarks("license").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("blank2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("blank3").Range.Font.Hidden = False
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
Next
End Sub
Assuming that the content control Title is the same as the bookmark name you can try this simplified version of your code.
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim cc As ContentControl
For Each cc In ActiveDocument.ContentControls
If ActiveDocument.Bookmarks.Exists(cc.Title) Then
ActiveDocument.Bookmarks(cc.Title).Range.Font.Hidden = cc.Checked
End If
Next cc
End Sub
EDIT:
The issue you have with your original code is that it will only allow one row to be hidden.
To make your solution work you need to query the checked status of the corresponding content control for each bookmark. Your best option to achieve that is to ensure that the bookmark name matches either cc.Title or cc.Tag, otherwise you are back to complex and unwieldy code.
You actually don't need anything more complicated than:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
With CCtrl
If .Range.Information(wdWithInTable) = True Then
If .Checked = True Then
.Range.Tables(1).Range.Font.Hidden = True
.Range.Rows(1).Range.Font.Hidden = False
Else
.Range.Tables(1).Range.Font.Hidden = False
End If
End If
End With
End Sub
Looping through all the content controls is quite unnecessary. You don't even need any titles or bookmarks.

Switch between three radio buttons using keyboard shortcuts in vb.net project

I have three radio buttons in my vb.net windows forms application project. Now I want to switch (or toggle) between these three buttons using keyboard shortcuts like Alt+S. Please help.
tested and works, next time please make a bit more effort with your research
on load:
Me.KeyPreview = True
on keydown:
If e.Alt AndAlso e.KeyCode = Keys.S Then
if RB1.checked = true then
RB2.checked = true
elseif RB2.checked = true
RB3.checked = true
elseif RB3.checked = true
RB1.checked = true
end if
end if
Avoiding RBs check:
on load:
Me.KeyPreview = True
dim vall as integer = 1
on keydown:
If e.Alt AndAlso e.KeyCode = Keys.S Then
select case vall
case 1
RB2.checked = true
vall = 2
case 2
RB3.checked = true
vall = 3
case 3
RB1.checked = true
vall = 1
end select
end if

Microsoft Excel VBA - Run-Time Error 438

I appear to be having an error which I am struggling to figure out the reason. I have tried the help sections and also tried researching it online but have not come up with any results. I am hoping someone may be able to assist me in the matter.
Issue
I have created multiple forms for different sheets on my spreadsheet. I have made forms which can be used to hide/show select column(s) by user discretion. I have two forms which work perfectly fine, but on the third.
I get
Run-Time Error 438 "Object doesn't support this property or method"
What does this mean? The code is the exact same as the other forms. The only difference in them is that the names of the sheets are different.
I will paste the code below for the sheets. Hopefully you can distinguish which is which. I will try and do my best to explain.
Code below
Main sheet - contains button to form open form
Private Sub openUserForm_Click()
chkFormCooms.Show
End Sub
Userform
Option Explicit
Sub hideCol(C As Integer)
If Controls("CheckBox" & C) = True Then
Columns(C).Hidden = True
Else
Columns(C).Hidden = False
End If
ActiveWindow.ScrollColumn = 1
End Sub
Private Sub chkP1_Click()
If Me.chkP1.Value = True Then
Sheets("Cooms").Columns("T:W").Hidden = True
Sheets("chkCooms").chk1.Value = True
ElseIf Me.chkP1.Value = False Then
Sheets("Cooms").Columns("T:W").Hidden = False
Sheets("chkCooms").chk1.Value = False
End If
End Sub
Private Sub chkP2_Click()
If Me.chkP2.Value = True Then
Sheets("Cooms").Columns("X:AA").Hidden = True
Sheets("chkCooms").chk2.Value = True
ElseIf Me.chkP2.Value = False Then
Sheets("Cooms").Columns("X:AA").Hidden = False
Sheets("chkCooms").chk2.Value = False
End If
End Sub
Private Sub chkP3_Click()
If Me.chkP3.Value = True Then
Sheets("Cooms").Columns("AB:AE").Hidden = True
Sheets("chkCooms").chk3.Value = True
ElseIf Me.chkP3.Value = False Then
Sheets("Cooms").Columns("AB:AE").Hidden = False
Sheets("chkCooms").chk3.Value = False
End If
End Sub
Private Sub chkP4_Click()
If Me.chkP4.Value = True Then
Sheets("Cooms").Columns("AF:AI").Hidden = True
Sheets("chkCooms").chk4.Value = True
ElseIf Me.chkP4.Value = False Then
Sheets("Cooms").Columns("AF:AI").Hidden = False
Sheets("chkCooms").chk4.Value = False
End If
End Sub
Private Sub chkP5_Click()
If Me.chkP5.Value = True Then
Sheets("Cooms").Columns("AJ:AM").Hidden = True
Sheets("chkCooms").chk5.Value = True
ElseIf Me.chkP5.Value = False Then
Sheets("Cooms").Columns("AJ:AM").Hidden = False
Sheets("chkCooms").chk5.Value = False
End If
End Sub
Private Sub chkP6_Click()
If Me.chkP6.Value = True Then
Sheets("Cooms").Columns("AN:AQ").Hidden = True
Sheets("chkCooms").chk6.Value = True
ElseIf Me.chkP6.Value = False Then
Sheets("Cooms").Columns("AN:AQ").Hidden = False
Sheets("chkCooms").chk6.Value = False
End If
End Sub
Private Sub chkP7_Click()
If Me.chkP7.Value = True Then
Sheets("Cooms").Columns("AR:AU").Hidden = True
Sheets("chkCooms").chk7.Value = True
ElseIf Me.chkP7.Value = False Then
Sheets("Cooms").Columns("AR:AU").Hidden = False
Sheets("chkCooms").chk7.Value = False
End If
End Sub
Private Sub chkP8_Click()
If Me.chkP8.Value = True Then
Sheets("Coomst").Columns("AV:AY").Hidden = True
Sheets("chkCooms").chk8.Value = True
ElseIf Me.chkP8.Value = False Then
Sheets("Cooms").Columns("AV:AY").Hidden = False
Sheets("chkCooms").chk8.Value = False
End If
End Sub
Private Sub chkP9_Click()
If Me.chkP9.Value = True Then
Sheets("Cooms").Columns("AZ:BC").Hidden = True
Sheets("chkCooms").chk9.Value = True
ElseIf Me.chkP9.Value = False Then
Sheets("Cooms").Columns("AZ:BC").Hidden = False
Sheets("chkCooms").chk9.Value = False
End If
End Sub
Private Sub chkP10_Click()
If Me.chkP10.Value = True Then
Sheets("Cooms").Columns("BD:BG").Hidden = True
Sheets("chkCooms").chk10.Value = True
ElseIf Me.chkP10.Value = False Then
Sheets("Cooms").Columns("BD:BG").Hidden = False
Sheets("chkCooms").chk10.Value = False
End If
End Sub
Private Sub chkP11_Click()
If Me.chkP11.Value = True Then
Sheets("Cooms").Columns("BH:BK").Hidden = True
Sheets("chkCooms").chk11.Value = True
ElseIf Me.chkP11.Value = False Then
Sheets("Cooms").Columns("BH:BK").Hidden = False
Sheets("chkCooms").chk11.Value = False
End If
End Sub
Private Sub chkP12_Click()
If Me.chkP12.Value = True Then
Sheets("Cooms").Columns("BL:BO").Hidden = True
Sheets("chkCooms").chk12.Value = True
ElseIf Me.chkP12.Value = False Then
Sheets("Cooms").Columns("BL:BO").Hidden = False
Sheets("chkCooms").chk12.Value = False
End If
End Sub
Private Sub chkP13_Click()
If Me.chkP13.Value = True Then
Sheets("Cooms").Columns("BP:BS").Hidden = True
Sheets("chkCooms").chk13.Value = True
ElseIf Me.chkP13.Value = False Then
Sheets("Cooms").Columns("BP:BS").Hidden = False
Sheets("chkCooms").chk13.Value = False
End If
End Sub
Private Sub UserForm_Initialize()
Me.chkP1.Value = Sheets("chkCooms").chk1.Value
Me.chkP2.Value = Sheets("chkCooms").chk2.Value
Me.chkP3.Value = Sheets("chkCooms").chk3.Value
Me.chkP4.Value = Sheets("chkCooms").chk4.Value
Me.chkP5.Value = Sheets("chkCooms").chk5.Value
Me.chkP6.Value = Sheets("chkCooms").chk6.Value
Me.chkP7.Value = Sheets("chkCooms").chk7.Value
Me.chkP8.Value = Sheets("chkCooms").chk8.Value
Me.chkP9.Value = Sheets("chkCooms").chk9.Value
Me.chkP10.Value = Sheets("chkCooms").chk10.Value
Me.chkP11.Value = Sheets("chkCooms").chk11.Value
Me.chkP12.Value = Sheets("chkCooms").chk12.Value
Me.chkP13.Value = Sheets("chkCooms").chk13.Value
End Sub
I hope this all makes sense and that someone is able to assist me in this matter. If you need further explanation then please do not hesitate to ask. Thank you very much for your assistance.
Check the name of your userform its probably spelt incorrectly
For information about the error check this amazing description

Is there an easier/more efficient way to make Visible = False for multiple buttons in VB.NET?

not sure if there is an easier way to do this but I have a LOT of buttons on a form. Different ones are visible for different functions.
Is there a way to have something like this have an easier way to change their visibility without having each button coded to go False/True?
For simplicity sake, I created a quick app to handle the visibility but I want to hide the others when one set of buttons is visible. So if I select Row 1, it will make Visibility FALSE on Row 2 and 3.
Am I stuck with this or is there an easier way/more efficient way? THANKS IN ADVANCE!
Public Class Form1
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedItem.ToString = "Button Row 1" Then
Button1.Visible = True
Button2.Visible = True
Button3.Visible = True
Button4.Visible = True
Button5.Visible = True
Button6.Visible = True
Button7.Visible = True
Button8.Visible = True
Button9.Visible = True
Button10.Visible = True
Button11.Visible = False
Button12.Visible = False
Button13.Visible = False
Button14.Visible = False
Button15.Visible = False
Button16.Visible = False
Button17.Visible = False
Button18.Visible = False
Button19.Visible = False
Button20.Visible = False
Button21.Visible = False
Button22.Visible = False
Button23.Visible = False
Button24.Visible = False
Button25.Visible = False
Button26.Visible = False
Button27.Visible = False
Button28.Visible = False
Button29.Visible = False
Button30.Visible = False
ElseIf ComboBox1.SelectedItem.ToString = "Button Row 2" Then
Button1.Visible = False
Button2.Visible = False
Button3.Visible = False
Button4.Visible = False
Button5.Visible = False
Button6.Visible = False
Button7.Visible = False
Button8.Visible = False
Button9.Visible = False
Button10.Visible = False
Button11.Visible = True
Button12.Visible = True
Button13.Visible = True
Button14.Visible = True
Button15.Visible = True
Button16.Visible = True
Button17.Visible = True
Button18.Visible = True
Button19.Visible = True
Button20.Visible = True
Button21.Visible = False
Button22.Visible = False
Button23.Visible = False
Button24.Visible = False
Button25.Visible = False
Button26.Visible = False
Button27.Visible = False
Button28.Visible = False
Button29.Visible = False
Button30.Visible = False
ElseIf ComboBox1.SelectedItem.ToString = "Button Row 3" Then
Button1.Visible = False
Button2.Visible = False
Button3.Visible = False
Button4.Visible = False
Button5.Visible = False
Button6.Visible = False
Button7.Visible = False
Button8.Visible = False
Button9.Visible = False
Button10.Visible = False
Button11.Visible = False
Button12.Visible = False
Button13.Visible = False
Button14.Visible = False
Button15.Visible = False
Button16.Visible = False
Button17.Visible = False
Button18.Visible = False
Button19.Visible = False
Button20.Visible = False
Button21.Visible = True
Button22.Visible = True
Button23.Visible = True
Button24.Visible = True
Button25.Visible = True
Button26.Visible = True
Button27.Visible = True
Button28.Visible = True
Button29.Visible = True
Button30.Visible = True
End If
End Sub
End Class
Set the tag property of the buttons. You can do it in the properties window. I just show it in code to illustrate the point
Button1.Tag = "Button Row 1"
Then you can do
Dim selectedRow = ComboBox1.SelectedItem.ToString()
For Each c As Control In Controls
If (TypeOf c Is Button) Then
c.Visible = selectedRow.Equals(c.Tag)
End If
Next
Note that this automatically shows the buttons of the selected row and hides the others.
If this affects too many buttons, you can also check if the Tag is not Nothing instead of testing if it is a button.
You could use a for loop and keep track of which textbox is in which row, could put the row in the name, tag, use a custom property, etc.
You could put each row in a groupbox and just change the visibility of the group.
You could make a list of Buttons for each row, add the buttons, and loop through those lists.
Winforms support data binding.
' In form constructor
public Sub New()
InitializeComponent()
cmbEnableButtons.DataSource = New List(Of string) From
{
"Nothing",
"Button Row 1",
"Button row 2"
}
button1.Tag = "Button Row 1"
button2.Tag = "Button Row 1"
button3.Tag = "Button Row 2"
button4.Tag = "Button Row 2"
button1.DataBindings.Add(CreateBindingForVisible())
button2.DataBindings.Add(CreateBindingForVisible())
button3.DataBindings.Add(CreateBindingForVisible())
button4.DataBindings.Add(CreateBindingForVisible())
}
Private Function CreateBindingForVisible() As Binding
Dim buttonBinding =
New Binding("Visible",
cmbEnableButtons,
"SelectedValue",
true,
DataSourceUpdateMode.OnPropertyChanged)
' Every time selected value of combobox changed
' this event handler convert string to "visible" boolean
AddHandler buttonBinding.Format, AddressOf ButtonBinding_Format
return buttonBinding;
End Sub
Private Sub ButtonBinding_Format(object sender, ConvertEventArgs e)
Dim binding = DirectCast(sender, Binding)
Dim button = DirectCast(binding.Control, Button)
e.Value = Equals(button.Tag, e.Value)
End Sub
With data binding you can configure every button separately from each other, while having common logic in one place.

Infinite loop when validating input against array

The homework task is to simply allow the user to input a value (string) "FD__" and match it against a list of known inputs and return true/false. The ID's are already defined and it works well, when I type an ID which is defined like Products(2) which is "FD3" it will return true, if the value is not defined it will not only give no results but crash the program, so in conclusion true works but false does not. Any information could be helpful.
Design: http://i.imgur.com/bJnFAMX.png
Public Class Form1
'variables
Dim Products(9) As String
Dim Entered As String
Dim Found As Boolean
Public Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
'Product variable array elements
Products(0) = "FD1"
Products(1) = "FD2"
Products(2) = "FD3"
Products(3) = "FD4"
Products(4) = "FD5"
Products(5) = "FD6"
Products(6) = "FD7"
Products(7) = "FD8"
Products(8) = "FD9"
Products(9) = "FD10"
'process
Entered = txtFind.Text 'define entered value as variable
Found = FindNumber() 'sub function
If Found Then
lblResult.Text = Found 'change the results
End If
End Sub
Public Function FindNumber()
'If true statements
Do While (Found = False)
If Entered = Products(0) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(1) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(2) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(3) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(4) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(5) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(6) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(7) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(8) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(9) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
Else 'keeps crashing
Found = False
lblResult.ForeColor = Color.Red
End If
Loop
Return Found
End Function
End Class
You can actually do this with a lot less code... I'm going to go ahead and do some critique while at it.
'1. Functions should have return types
'2. You should pass the function what it needs to do its job
Public Function FindNumber(numberEntered As String) As Boolean
'Rather than evaluating if a condition is true or false, and then returning
'true or false, you can just directly return the condition.
Return Products.Contains(numberEntered)
End Function
Then in your main program:
'This doesn't need to be a class level variable
Dim entered = txtFind.Text
'Local variables should be lowercase
Dim found = FindNumber(entered)
If found Then
'With Option Strict you can't assign a Boolean to a String...
'but you can do .ToString() instead
lblResult.Text = found.ToString()
'And this was really dependent on the result of the function...
'Also it has nothing to do with finding the number...
lblResult.ForeColor = Color.LawnGreen
Else
lblResult.ForeColor = Color.Red
End If
It looks a lot longer, but take out the comments and I promise you it'll be a much simpler (and shorter) program.
You could optimise this whole code by using the .contains function this simple used like this:
if mylist.contains("sometext") then
'change forecolor to green
else
'change forecolor to red
end if
This would mitigate your problem.