I have 29 textboxes and 29 labels and lots of (IF)'s - vb.net

i have 29 textBoxes and 29 labels
how can i make it easier instead of writning all 29's every time
and there is 29 (IF)
Label1.ForeColor = c2
Label2.ForeColor = c1
Label3.ForeColor = c2
' There is lot of texts and labels and (IF) condition

For those cases usually there are some controls that you need to change and other that need to be static. If that is the case you can use a List(T) to store the elements you want to change and then as #FreeMan stated use a For Each loop
For your case the code would be something similar to this(please note this is an idea and you may need to tweak it):
Dim lblList as New List(Of Label)({Label1, Label2, Label3})
'Then you loop the List to assign the values. Even can include if statements
For Each lbl As Label In lblList
lbl.ForeColor = c1
Next
Please give it a try and let me know your comments

Related

If cell is blank then make variable equivalent to any value

I have a workbook with 3 List Boxes. Each List Box is populated by a list that is created, each List Box contains a "Blank" data point. I am trying to write a script that goes:
For lpRows = 1 to 100
For lpColumns = 1 to 8
If ListBox1 = "" and ListBox2 = "Val" And ListBox3 = "Var" Then
Sheets(RndmSheet).Cells(lprows,lpcolumns) = Sheets(DiffSheet).Cells(lprows,lpColumns)
else
end if
However, my code isn't working because VBA is simply searching for a blank cell, where I would prefer it to return any value that contains both ListBox2 and ListBox3, but Listbox1 would be irrelevant.
Is there an easy way to use a wildcard to achieve what I want or will I need to simply make a rather large nested if statements to handle this?
I just added in an if statement
if sheets(rndmsheet).cell(lpRows,lpColumns) = "" then
VarCell = Sheets(rndmsheet).cell(lprows,lpcolumns)
end if
Sorry if that doesn't really make sense.

Dice Roller Populate TextBox(s)

I am still learning basic programming, literally off Visual Basic 2012 v11.
Here is what I am stumped on:
I made a stupid-simple Table-Top Role-Playing Character Log application.
I made a simple dice roller/"random number" procedure and set it to fill a ListBox with each button click of "ROLL" until the max of 6 was reached. That worked fine.
However, I changed my app to link with a database to store simple character information for all of the people playing,etc. Which means I now have separate dataset TextBoxes for each click of "ROLL". Would someone please advise me on the best way to populate one TextBox per button click with a maximum of 6 clicks, please?
I was using a loop to fill the ListBox with exactly 6 entries, and started that line of thought for filling the TextBoxes, but my late-night tired brain cannot find a way to fill ONE box EACH TIME instead of all boxes every single time.
THANK YOU FOR YOUR HELP!
My Code:
Private Sub btnRoll_Click(sender As Object, e As EventArgs) Handles btnRoll.Click
'Roll a character stat up to 6 and add it to the lisbox
'D20 is standard die, but stats below 6 are forgiven for this campagin
Dim Dice As New Random
Dim DiceRoll As Integer = Dice.Next(6, 20)
Dim intCount As Integer = 0
Dim Stat As String = Convert.ToString(DiceRoll)
Do While intCount < 7
Loop
End Sub
I don't believe you've included enough information to give an informed answer.
From my understanding, you'd like to take the result of the roll, and input it into one of several textboxes (you haven't given a number of textboxes, but you'd like it done in less than 6 clicks).
Depending on what the different fields will be, you may want to include your calculations within the loop, along with this, to specify the output and then roll again X times within the loop:
Do While intcount < 7
Select Case intcount
Case 1
txtBox1 = stat
Case 2
txtBox2 = stat
Case 3
txtBox3 = stat
Case 4
txtBox4 = stat
Case 5
txtBox5 = stat
Case 6
txtBox6 = stat
Case Else
'Nothing if not specified with other arguments.
'Roll again
stat = Convert.ToString(Dice.Next(6,20))
Loop

How can i get a large number of text boxs to subtract from each other

I'm trying to make a program where a user inputs numbers into a subtraction equation and the program tells them if they are right or wrong and what the correct answer is in a label. There are 20 different equations with 3 text boxes each. The first two text boxes are for the two numbers that are being subtracted and the third text box is the answer. I declared them into a array but I can't figure out how make them subtract. The code i have so far is:
Dim i As Integer
Dim txtNumber1() As TextBox = {txt1Number1, txt2Number1, txt3Number1, txt4Number1, txt5Number1, txt6Number1, txt7Number1, txt8Number1, txt9Number1, txt10Number1, txt11Number1, txt12Number1, txt13Number1, txt14Number1, txt15Number1, txt16Number1, txt17Number1, txt18Number1, txt19Number1, txt20Number1}
Dim txtNumber2() As TextBox = {txt1Number2, txt2Number2, txt3Number2, txt4Number2, txt5Number2, txt6Number2, txt7Number2, txt8Number2, txt9Number2, txt10Number2, txt11Number2, txt12Number2, txt13Number2, txt14Number2, txt15Number2, txt16Number2, txt17Number2, txt18Number2, txt19Number2, txt20Number2}
Dim txtAnswer() As TextBox = {txt1Answer, txt2Answer, txt3Answer, txt4Answer, txt5Answer, txt6Answer, txt7Answer, txt8Answer, txt9Answer, txt10Answer, txt11Answer, txt12Answer, txt13Answer, txt14Answer, txt15Answer, txt16Answer, txt17Answer, txt18Answer, txt19Answer, txt20Answer}
Dim intAnswer() As Integer
For i = 0 To txtNumber1.Length - 1
intAnswer(i) = txtNumber1(i) - txtNumber2(i)
Next
I also can't figure out how i would make each answer display into a label. I think it would be some like
If intAnswer(0) = txtAnswer(0) Then
Me.lblAnswer1.Text = "Correct:" & intAnswer(0)
Else
Me.lblAnswer1.Text = "Incorrect:" & intAnswer(0)
End If
But I'm not sure how i would loop that to make it do all 20 labels, or would i just need to have it 20 different times, one for each label.
Thanks for the help.
Best to create a user control with 3 labels and 3 textboxes on each. Then you only need to code this much, and wrap this logic in a loop to repeat as many times as you want. Basically, narrow down your problem to "I only have 1 equation", solve it using this approach, the rest is as easy as using adding a loop to your code.

Graphical representation of data

I am totally new to the graphical representation of the data. I want to make a progress report of students, based on marks achieved each year.
For example, in year 2005 marks were 750. in 2006 780 in 2007 800
I want to show it graphically. Could any body give me code example?
Thanks a lot.
Use a label and set the Label.Width = intValue where intValue is the value you want to show (you can divide it by a factor to make sure it stays within a certain bound). They you an play around with lable colors or backgrounds for different value ranges. E.g.
assume valuelist is the list of values
Dim graphicallabels(n) As Label
For i As Integer = 0 To valuelist.length
graphicallabels(i) = New Label
With graphicallabels(i)
.Location = New Point(0, i * 2 * graphicallabels(i).Height)
.Width = valuelist(i)
End With
If valuelist(i)> 50 Then
graphicallabels(i).BackColor = Color.Green
Else
graphicallabels(i).BackColor = Color.Red
End If
Next
Note: untested code
HTH

VBA display selection options

I am trying to write a code that will display a value depending on what checkbox is selected. There are a total of 5 checkboxes and I will be adding additional checkboxes in the future so I was wondering if there is an easy way to determine which checkboxes are checked to determine which values to display. I can do this in a really round about way but I would like minimize my code if possible.
In other words, if i write each scenario out I would have to write a separate code for all of the different selection possbilities:
1 only,2 only,3 only,4 only,5 only
1+2, 1+3, 1+4, 1+5, 2+3, 2+4, 2+5, 3+4, 3+5, 4+5
1+2+3, 1+2+4,1+2+5, 1+3+4,1+3+5, 1+4+5,2+3+4, 2+3+5,3+4+5
1+2+3+4, 1+2+3+5, 1+3+4+5, 2+3+4+5
1+2+3+4+5
Each value is associated with a sub that will fill the array if it is selected. And after the arrays are filled I need to perform an additional function on the ones that are selected. The function performed is the same but I do not want to perform the function if a value is not selected because it will defeat the purpose of my function otherwise. The function itself is to select duplicates from the arrays that were selected into another array.
You can use binary numbers for each checkbox:
First value is 1 (=20)
Second value is 2 (=21)
Third value is 4 (=22)
Fourth value is 8 (=22)
Fifth value is 16 (=24)
Hence, when you sum each permutation, you have a unique number you can check.
1 only is 0, 2 only is 1 and so on
1+2 is 3, 1+3 is 5, 1+4 is 9, 1+5 is 17
and so on
You can create arrays with every case you want to check.
At least, i hope this will give you some ideas or tips.
Regards,
What #JMax is referring to is more commonly known as bit-masking. Here's a quick tutorial:
Create a sample form named Form1 with 5 checkboxes named Check1, Check2, Check3, Check4, Check5. Then add the following two functions in a standard code module:
Function GetSelectedBoxes() As Long
Dim Ctl As Control, Total As Long
For Each Ctl In Forms!form1
If Ctl.ControlType = acCheckBox Then
If Ctl Then
Total = Total + 2 ^ CLng(Mid(Ctl.Name, 6))
End If
End If
Next Ctl
GetSelectedBoxes = Total
End Function
Sub ShowSelectedBoxes()
Dim Total As Long, i As Integer
Total = GetSelectedBoxes
For i = 1 To 5
If Total And 2 ^ i Then Debug.Print "Check" & i & " selected"
Next i
End Sub
Now open Form1 and check boxes 1, 3, and 4. Run the ShowSelectedBoxes routine and you should get the following output in the immediate window:
Check1 selected
Check3 selected
Check4 selected
I used the For...Loop for compactness in my sample, but you can just as easily break it out into separate If...Then statements to do something more meaningful with the checked boxes.
This approach will support up to 31 or 32 separate checkboxes.