Modifying background color based on if statement - vb.net

I am relatively new to vb.net coding.
I am looking to write a code that will modify the label (calculation result output) color based on some criteria. I have a drop down menu with 2 options, Dust and Metal.
The color is not changing and i am not sure why.
This is the code;
Dim concentrationcheck As String = Form8.materialType.SelectedIndex
Select Case concentrationcheck
Case "Dust"
If Val(concentrationValue.Text) < 4 Then
concentrationValue.BackColor = Color.Red
MsgBox("Add more suppressant or contact factory")
Else
concentrationValue.BackColor = Color.Green
End If
Case "Metal"
If Val(concentrationValue.Text) < 20 Then
concentrationValue.BackColor = Color.Red
MsgBox("Add more suppressant, or contact factory")
Else
concentrationValue.BackColor = Color.Green
End If
End Select

SelectedIndex is a number, not the SelectedItem
Dim concentrationcheck As String = Form8.materialType.SelectedItem.ToString
Your Form8 name sounds like the name of the form, and not the instance. My guess would be to change it to me if this is all running in the one form:
Dim concentrationcheck As String = Me.materialType.SelectedItem.ToString
If nothing is selected, an exception will be thrown, so you might have to do a simple check:
If materialType.SelectedIndex > -1 Then
code here
End If

I guess your drop down menu is combobox .. Try this ..
Dim concentrationcheck As String = Form8.materialType.Text

I'm suspicious of SelectedIndex in your original post.
SelectedIndex is an integer, not the value at that integer location.
SelectedValue might get you what you need.

Related

How to subtract from a number with a variable amount of other numbers?

Sorry for the edits, im tired and im a moron, so heres my problem=
Lets say this is my code :
Dim ogquantity,subtractedquantity as double
Private Sub CalculateMe
ogquantity = numericupdown1.value
subtractedquantity = ogquantity - numericupdown2.value
subtractedquantity = ogquantity - numericupdown3.value
subtractedquantity = ogquantity - numericupdown4.value
subtractedquantity = ogquantity - numericupdown5.value
label1.text = subtractedquantity
End Sub
And i set "CalculateMe" as code for 1-5 numericupdowns_valuechanged
This wont output a changed value..
And the reason i have it set up like this is because in the actual code numericupdown might change subtractedquantity1 or subtractedquantity28, it is decided with a dropdown menu for each numericupdown.
something like this:
Dim item1stock, item1originalstock As Double
If itemselector1name.Text = itemdefiner1name.text Then
item1stock = item1originalstock - itemselector1quantity.value
itemselector1label.text = item1stock
End If
If itemselector2name.Text = itemdefiner1name.text Then
item1stock = item1originalstock - itemselector2quantity.value
itemselector2label.text = item1stock
End If
and i would repeat check for every item like this but if two different 'itemselectorquantity' is supposed to subtract from the same original stock quantity that i dont know how to set up.. Im trying my best here but im new to coding and bad at explanation. Thank you!
Your question is rather muddled but, as an example of the sort of thing you might do if you want to use zero, one or more of a set of controls based on another set of controls, consider this example that uses CheckBoxes to specify which NumericUpDowns to subtract from an initial quantity:
Private Function SubtractCheckedValues(value As Decimal) As Decimal
Dim checkBoxes = {CheckBox1, CheckBox2, CheckBox3, CheckBox4}
Dim numericUpDowns = {NumericUpDown1, NumericUpDown2, NumericUpDown3, NumericUpDown4}
For i = 0 To checkBoxes.GetUpperBound(0)
If checkBoxes(i).Checked Then
value -= numericUpDowns(i).Value
End If
Next
Return value
End Function

Split String inside If Condition does not show all split value. VB.NET

I am comparing the splitted Source_Doc_No_P of PER MAP to PER SAWT then if it matched it will color the BackColor of DataGridview into yellow but there is something wrong in my code when the split value of Source_Doc_No_P is inside the If condition it only get 1 value of splitted string. Please see the screen-shot and code to help you understand my problem.
For x As Integer = 0 To Me.DataGridView4.Rows.Count - 1
Dim sdoc_map As String = DataGridView4.Rows(x).Cells("Source_Doc_No_P").Value.ToString
Dim split_sdoc() As String = sdoc_map.Split("/")
For Each part As String In split_sdoc
'Output of split_doc after For each loop
'BS091
'BS092
'BS093
If part = Me.DataGridView4.Rows(x).Cells("Source_Doc_No").Value.ToString Then
'Output of split_doc after If condition
'BS091
'It should be
'BS091
'BS092
'BS093A
'-------Will automatically color the yellow of mathced Source Document----------'
Me.DataGridView4.Rows(x).Cells(0).Style.BackColor = Color.Yellow
Me.DataGridView4.Rows(x).Cells("Source_Doc_No").Style.BackColor = Color.Yellow
Me.DataGridView4.Rows(x).Cells("ATC").Style.BackColor = Color.Yellow
Me.DataGridView4.Rows(x).Cells("Prepaid_Tax").Style.BackColor = Yellow
Me.DataGridView4.Rows(x).Cells("Source_Doc_No_P").Style.BackColor = Color.Yellow
Me.DataGridView4.Rows(x).Cells("ATC_P").Style.BackColor = Color.Yellow
Me.DataGridView4.Rows(x).Cells("Tax_Withheld_P").Style.BackColor = Color.Yellow
'-----------------------------------End---------------------------------------'
End If
Next
Next
Current Result
What should be the result
Dim sdoc_map As String = DataGridView4.Rows(x).Cells("Source_Doc_No_P").Value.ToString
check Here.. I think just Confused With Cell Name . because Cells("Source_Doc_No_P") not Present here. Check the cell Name and then Proceed.

Visual Basic DataRow/DataSet/DataGrid change specific cell text color

For Each row As DataRow In ds.Tables(0).Rows
If (row("Flag") And 1 = 1) Then
row("Col_1") = Color.Red
End If
If (row("Flag") And 2 = 2) Then
row("Col_2") = Color.Red
End If
If (row("Flag") And 4 = 4) Then
row("Col_3") = Color.Red
End If
If (row("Flag") And 8 = 8) Then
row("Col_4") = Color.Red
End If
Next
I would like to set a specific cell to red based on a flag. I have access to a DataSet and DataGrid, but not DataGridView. Is there a simple way to accomplish this with DataGrid? I believe what I have now doesn't work as the DataRow is just memory data.
why are you using 1=1 , just use True ... wait a minute
looking at your code again ... you are doing bit manipulation ... i think that you are missing some brackets in your if statements
should be If ((row("Flag") And 1) = 1) Then .... same for the rest

Search field in text file

I have some coding which displays a label if the value of a textbox matches any of the first values of each line in a textfile.
Dim sList As New List(Of String)(IO.File.ReadAllLines("Path"))
Dim i As Integer
For i = 0 To sList.Count - 1
If sList(i).StartsWith(textbox1.Text) Then
Label1.Visible = True
Exit For
Else
Label1.Visible = False
End If
Next
The problem is if the textbox has 1 and the textfile has 11 it will display the label, what would be the best way around this?
I have tried sList(i).Contains etc but none of them are doing the job.
I have tried all the suggestions here and nothing works, my textfile has numbers like the following
11
15
18
and for example if i have the number 1 in the textbox then the label is visible.
Try this:
Label1.Visible = IO.File.ReadAllLines("Path.txt").Any(Function(f) f = TextBox1.Text)
I think LINQ can be used here:
Dim text = textbox1.Text
Dim textWithSpace = String.Format("{0} ", text)
Label1.Visible = IO.File.ReadAllLines("Path").Any(Function(line) line.StartsWith(textWithSpace) OrElse line = text)
You need import System.Linq to make it work.
I assumed that space ends each word in the file.
If you want the Label to be visible when at least one of the lines starts with the text in the TextBox, you can use LINQ and Enumerable.Any:
Dim matchingLines = From l In IO.File.ReadLines("Path")
Where l.StartsWith(textbox1.Text)
Label1.Visible = matchingLines.Any()
Try changing the following line, assuming you are reading from a text file and looking for an exact match of the whole line you could try this:
If sList(i).StartsWith(textbox1.Text + Environment.NewLine) Then
That should check to make sure its the only thing on that line as it is now looking for a new line and will not match '11'

VB.NET..... Loop help

I am using My.Settings in visual studio 2008 to store information, for when the user runs the program again.
I have that working fine... but as I am using 12 textboxes I don't want to write...
my.settings.grade1 = textbox1.text
for each one, and I am also making calculations using the stored information, so I dont want to be writing my.settings.grade1 + my.settings.grade2 etc..
Any help welcome
Thanks =)
On your form that has the textboxes, add them to a collection or an array of textboxes when the form is initialised.
Then, iterate through the collection or array of textboxes to assign to the setting value.
If you don't want to manually code the assignment of the textboxes into the array, then in your form initialisation code, iterate through all controls on the form and check for the control type or a specfici 'Tag' you assign to each textbox, then add each textbox to the array that way.
For Each c as Control in Me.Controls
If c.Tag.ToString() = "Grade" Then
' Add Items to collection here '
End If
Next c
Have you considered using ApplicationSettings Binding to automatically bind your values to your Textboxes.Text properties. This will support 2-way binding and then all you have to do is Call Save when you close.
or you could do something like this:
given the your textboxes are named along the lines of: Grade1, Grade2, Grade3, etc.
you could store the Grades in an Array and then loop through the array:
((TextBox)form.findControl("Grade" + i.ToString())).Text = Grade(i)
Depending on your calculation, then you could also execute the calculation inside the loop.
Populate a List of grade textboxes:
'at the class level'
Public GradeBoxes(11) As TextBox
Const grade As String = "GRADE"
'when the form is created'
Dim i As Integer = 0
For Each ctr As Control In Controls
If TypeOf (ctr) Is TextBox AndAlso ctr.Name.ToUpper.StartsWith(grade) Then
i = CInt(ctr.Name.SubString(grade.Length))
If i >= 0 AndAlso i < GradeBoxes.Length Then GradeBoxes(i) = ctrl
End If
Next ctr
For Each box As TextBox in GradeBoxes
If box IsNot Nothing AndAlso My.Settings(box.Name) IsNot Nothing Then
box.Text = My.Settings(box.Name)
End If
Next box
Save grades:
For Each box As TextBox in GradeBoxes
If box IsNot Nothing AndAlso My.Settings(box.Name) IsNot Nothing Then
My.Settings(box.Name) = box.Text
End If
Next box
My.Settings.Save()
Do you mean something like?
Dim sum As Long
Dim grades(11) As Long
Dim i As Integer = 0
For Each ctr In Controls
If TypeOf (ctr) Is TextBox Then
grades(i) = CLng(ctr.Text)
sum = sum + grades(i)
i = i + 1
End If
Next