How to change picturebox from visible to not visible if label with a certain number appears? - vb.net

Good morning,
I'm creating a Stock software and I create two circles, one red and one yellow that I am using on a Picture box. I want that when the stock of label x is = or > 5 that picture box 1 appear on my dashboard. When label x is between 6 - 10 the yellow one appears and none of them appear if the value of label x is bigger than 10.
I tried to type different codes but none of them work correctly.
My code right now is:
If StockDrillbits.GreenSquareQty.Text Or
StockDrillbits.RedSquareQty.Text Or
StockDrillbits.BlackSquareQty.Text Or
StockDrillbits.GreenStarQty.Text Or
StockDrillbits.RedStarQty.Text Or
StockDrillbits.BlackStarQty.Text = "5" Or "4" Or "3" Or "2" Or "1" Or "0" Then
LowStockDrillbits.Visible = True
MediumStockDrillbits.Visible = False
ElseIf StockDrillbits.GreenSquareQty.Text Or
StockDrillbits.RedSquareQty.Text Or
StockDrillbits.BlackSquareQty.Text Or
StockDrillbits.GreenStarQty.Text Or
StockDrillbits.RedStarQty.Text Or
StockDrillbits.BlackStarQty.Text = "10" Or "9" Or "8" Or "7" Or "6" Then
MediumStockDrillbits.Visible = True
LowStockDrillbits.Visible = False
Else
LowStockDrillbits.Visible = False
MediumStockDrillbits.Visible = False
End If
I can't think any other way of doing this. I tried to convert any of the labels to int32, tried to create variables as integer and as single. Everything else on the software is working amazing but this simple code is not.
Anyone could help me? Code is Visual Basic.

You have a common syntax mistake in your code. While the If statement makes sense in English, you are thoroughly confusing VB. As mentioned in my comment, you cannot say:
Text = "4" Or "5"
But instead you need to say:
Text = "4" Or Text = "5"
Applying this idea to your code, with a couple of enhancements, you end up with:
If Val(StockDrillbits.GreenSquareQty.Text) <= 5 Or Val(StockDrillbits.RedSquareQty.Text) <= 5 Or Val(StockDrillbits.BlackSquareQty.Text) <= 5 Or Val(StockDrillbits.GreenStarQty.Text) <= 5 Or Val(StockDrillbits.RedStarQty.Text) <= 5 Or Val(StockDrillbits.BlackStarQty.Text) <= 5 Then
LowStockDrillbits.Visible = True
MediumStockDrillbits.Visible = False
ElseIf Val(StockDrillbits.GreenSquareQty.Text) <= 10 Or Val(StockDrillbits.RedSquareQty.Text) <= 10 Or Val(StockDrillbits.BlackSquareQty.Text) <= 10 Or Val(StockDrillbits.GreenStarQty.Text) <= 10 Or Val(StockDrillbits.RedStarQty.Text) <= 10 Or Val(StockDrillbits.BlackStarQty.Text) <= 10 Then
LowStockDrillbits.Visible = False
MediumStockDrillbits.Visible = True
Else
LowStockDrillbits.Visible = False
MediumStockDrillbits.Visible = False
End If
I shortened the code somewhat by using <= instead of a hard-coded range of numbers. Also, I applied Val to the Text so you are actually comparing numbers instead of strings. If you like, Val can be replaced with, for instance, CType.

Add the values to be compared to arrays, this makes the comparison code more compact.
Dim lowLables As String() = {"0", "1", "2", "3", "4", "5"}
Dim highLables As String() = {"6", "7", "8", "9", "10"}
Dim quantities As String() = {
StockDrillbits.GreenSquareQty.Text,
StockDrillbits.RedSquareQty.Text,
StockDrillbits.BlackSquareQty.Text,
StockDrillbits.GreenStarQty.Text,
StockDrillbits.RedStarQty.Text,
StockDrillbits.BlackStarQty.Text }
LowStockDrillbits.Visible = quantities.Any(Function(q) lowLables.Contains(q))
MediumStockDrillbits.Visible = quantities.Any(Function(q) highLables.Contains(q))
My code uses LINQ Method Syntax. First, it creates 2 arrays containig the labels "0" to "5" and "6" to "10". Then it creates an array named quantities containing the text of the StockDrillbits textboxes or labels.
quantities.Any(...) contains a condition that must be True for any of the quantity strings. The condition is given as a Lambda Expression. The condition lambda Function(q) lowLables.Contains(q) tests whether a quantity string is contained in the lowLables array. The same is then done for the highLables array.
I also replaced the lengthy If-statement by direct assignment of the condition results to the picture boxes.

Related

my compare statement not working in vb.net

I was doing an exam algorithm earlier (Just started vb 5 days ago so be nice ;) ) and I realised after testing that for whatever reason my statement If 0 < CInt(Num(x)) < 10 was not working which completely baffled me as I would thinkk that of all things THIS would be the easiest but no. No matter what I did, it just would always be true even if I made Num(X) = 90 or 9000000 it wouldn't matter, so if anyone knows what obvious mistake I have made whether its because I made it a string or what let me know :)
Dim Num(2), INPUT As String
Dim Cond As Boolean = False
Dim Valid As Boolean = False
Dim Isnum As Boolean
Dim x, Number, IsTen As Integer
While Cond = False
Do Until IsTen = 3 And Number = 3
IsTen = 0
Number = 0
For x = 0 To 2
MsgBox("Please enter a number")
Num(x) = (Console.ReadLine)
Isnum = IsNumeric(Num(x))
If Isnum = True Then
IsTen = IsTen + 1
End If
If Isnum = True Then
If 0 < CInt(Num(x)) < 10 Then
Number = Number + 1
End If
End If
Next
If Number <> 3 Then
MsgBox("Sorry all three numbers werent between 0 and 10, Please enter again")
End If
If IsTen <> 3 Then
MsgBox("Sorry all three inputs werent numbers, Please enter again")
End If
Loop
MsgBox("All three numbers are valid")
End While
First thing I would advise is setting Option Strict On at the top of your code file. You can also make this the default via Visual Studio options. This will warn you about the implicit type conversion that is causing your problem.
Changing the line
If 0 < CInt(Num(x)) < 10 Then
I'm assuming the intent here is to ensure the number is greater than 1 but less than 10
To something like this:
If Isnum = True Then
Dim val As Integer = Integer.Parse(Num(x))
If (val > 0) AndAlso (val < 10) Then
Number = Number + 1
End If
End If
Notice the use of Integer.Parse() to type cast the string to Int, it's a little more robust but there is also the Integer.TryParse() which is another step up again, might be worth reading up on. Also notice AndAlso usage, this is a short circuit operator so if the First condition is false, doesn't bother evaluating the next.

Select Case only seeing one digit

Picture of problem. Instead of looking at the value of the TextBox which is 20 is returning 2 which is the first digit of the 2 digit number in the textbox
------------------//--------------------
Select case only looking at 1 digit.
I have a case function running through some numbers and its supposed to change textcolour if value is over 4, or over 3 or over 2 or over 1
It works fine for numbers up to 9, but when the numbers start having more than a single digit it only looks at the first digit. Code below:
For Each TextStatas In Arr_TextStat
Select Case TextStatas.Text
Case > 4
TextStatas.BackColor = Color.Green
Case > 3
TextStatas.BackColor = Color.GreenYellow
Case > 2
TextStatas.BackColor = Color.Gold
Case > 1
TextStatas.BackColor = Color.DarkOrange
Case > 0
TextStatas.BackColor = Color.Red
Case 0
TextStatas.BackColor = Color.Red
End Select
Next
I use a similar piece of code on a different form, connected to the same database and looking at the same table and column as this form, but in there it works fine.
Can't understand what's wrong!
Any help much appreciated.
You should cast the TextStatas to an integer using CInt(TextStatas)
<TestMethod()> Public Sub TestMethod1()
Dim Arr_TextStat(5) As String
Arr_TextStat(0) = "1"
Arr_TextStat(1) = "2"
Arr_TextStat(2) = "3"
Arr_TextStat(3) = "4"
Arr_TextStat(4) = "9"
Arr_TextStat(5) = "20"
Dim backColor As Color = Color.AntiqueWhite
For Each TextStatas In Arr_TextStat
Select Case CInt(TextStatas)
Case > 4
backColor = Color.Green
Case > 3
backColor = Color.GreenYellow
Case > 2
backColor = Color.Gold
Case > 1
backColor = Color.DarkOrange
Case > 0
backColor = Color.Red
Case 0
backColor = Color.Red
End Select
Debug.WriteLine("{0} - {1}", TextStatas, backColor)
Next
End Sub
Gives these results:
Debug Trace:
1 - Color [Red]
2 - Color [DarkOrange]
3 - Color [Gold]
4 - Color [GreenYellow]
9 - Color [Green]
20 - Color [Green]
You are comparing a string (the .Text property) to a number. Before VB makes the comparison, it must bring the values to a common ground.
Exactly how that is performed depends on the context, which is why it is advised to avoid loosely typed comparisons in the first place.
If you are comparing a string variable with number-like contents to a number:
Dim s As String = "26"
MsgBox(s > 2)
MsgBox(s > 3)
VB will convert the number-like string to a number and then perform the comparison. The result will be True in both cases.
And if s contains something that cannot be converted to a number, you will get a runtime error.
If you are using a string variable as a source of a Select Case clause, then the opposite happens, and the values in the individual Case clauses are first converted to strings, and then the comparison is performed, regardless of whether or not the variable in question contains a number-like string:
Dim s As String = "26"
Select Case s
Case Is > 3
MsgBox("> 3")
Case Is > 2
MsgBox("> 2, but < 3")
Case Else
MsgBox("Other")
End Select
You will see "> 2, but < 3", because "26" > "2", but "26" < "3".
And it will work the same if s contains something that cannot be converted to a number (try this example with s = "sdf").
If you are comparing an Object variable to a number, it will be first converted to a number regardless of whether it is used directly or as a source of a Select Case:
Dim s As Object = "26"
MsgBox(s > 2) ' True
MsgBox(s > 3) ' True
Select Case s
Case Is > 3
MsgBox("> 3") ' Displays
Case Is > 2
MsgBox("> 2, but < 3")
Case Else
MsgBox("Other")
End Select
And if s contains something that cannot converted to a number, it will be a runtime error in both cases.
That is why it did not work with TextStatas.Text, which is a String, but did work with TextStatas.Tag, which is an Object.
You should not rely on these rules, always use Option Strict On, and change the code to
Select Case Integer.Parse(TextStatas.Text)
...
End Select

how to if condition row hide when condition is false RDLC

How to hide row if condition is fale please check below image and please help me this
=IIf(Fields!OrderTypeId.Value = "1", Fields!Orders.Value,0)
=IIf(Fields!OrderTypeId.Value = "2", Fields!Orders.Value,0)
=IIf(Fields!OrderTypeId.Value = "3", Fields!Orders.Value,0)
I want Like This
But when condition is false its show 0
This might be helpful:
=((Fields!OrderTypeId.Value = "3" Or Fields!OrderTypeId.Value = "2" Or
Fields!OrderTypeId.Value = "1")
And (Fields!Orders.Value = 0))
Thanks

Define letter as number, include as part of string for loop

If SecretWordLength = 5 Then
Label3.Visible = True
Label4.Visible = True
Label5.Visible = True
Label6.Visible = True
Label7.Visible = True
End If
This is the current code for making the dash underneath a letter visible for my hangman game based on the length of the secret word. How can I change this into a FOR loop so that I don't have to repeat this code for every label individually?
I was thinking of using a FOR loop in this way:
For i = 3 To 7
Labeli.Visible = True
Next
But it does not work as it recognizes the i as the letter itself, not the number I want it to represent. Help please?

Simple Random generator generates blank item

I am creating a random generator of text.
It works ok but a small issue that I can't seem to resolve is occurring.
When I click my test button, every now and then a blank item appears. Here is my code.
Dim rng As New System.Random()
Dim RAND(16) As String
RAND(0) = "A"
RAND(1) = "B"
RAND(2) = "C"
RAND(3) = "D"
RAND(4) = "E"
RAND(5) = "F"
RAND(6) = "G"
RAND(7) = "H"
RAND(8) = "I"
RAND(9) = "J"
RAND(10) = "K"
RAND(11) = "L"
RAND(12) = "M"
RAND(13) = "N"
RAND(14) = "O"
RAND(15) = "P"
TextBox1.Text = RAND(rng.Next(RAND.Count()))
Your RAND(16) as declared contains 17 elements. Last one is blank, because you did not initialize it to anything. You can use a Watch window to verify:
Regarding how Nothing becomes a blank, it's a result of an implicit conversion behind the scenes.