Need help GroupBoxes - and radio buttons I am very new to VB - vb.net

I want to make a program that depending on which radio buttons someone clicks (only 1 button per GroupBox) and a total of 5 different group boxes. a specific value would be added To a num1Textbox Value that was entered.
The question is Can it be done? or am i wasting my time trying to learn it. I am very new to VB.. and have only done a cpl programs that add - mulitpy - divide and subtract values for the golf program for putting.. this time it will be more complicated I know.
Below is a pic of the program.
Someone would enter Yard to Hole = 145 (would be different number each time)
They would then select 1 radio button per GroupBox (5 different options) and the value 145 would increase or decrease accordingly depending which options is selected.
Sorry it would not allow me to uplaod a picture here :( so i uploaded it here.
sorry did not know you needed an account to view form there.. i will try to find a new place. 1 min
lets try here
http://i1311.photobucket.com/albums/s667/Steve_Lunney/design_zps9c2f89da.jpg
ANY help would be greatly appreciated... Thank you in advance
this is what i was trying.. as someones suggestion
but under each of these staements spinGroupBox.spin1RadioButton.Checked I get wavy lines and when i put my mouse over it it says - 'upRadioButton' is not a member of 'System.Windows.Forms.Groupbox"
Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
Dim spinGroupBox As New GroupBox()
Dim spin1RadioButton As RadioButton
Dim spin2RadioButton As RadioButton
Dim spin3RadioButton As RadioButton
Dim spin4RadioButton As RadioButton
Dim spin5RadioButton As RadioButton
Dim num1TextBox As Integer
Dim num2TextBox As Integer
Dim spin1b As Decimal
Dim resultA As Integer
Select Case True
Case spinGroupBox.spin1RadioButton.Checked
spin1b = 0.095
Case spinGroupBox.spin2RadioButton.Checked
spin1b = 0.085
Case spinGroupBox.spin3RadioButton.Checked
spin1b = 0.08
'etc
End Select
resultA = spin1b
' Create and initialize a GroupBox and a Button control.
Dim windGroupBox As GroupBox
Dim upRadioButton As RadioButton
Dim ulRadioButton As RadioButton
Dim urRadioButton As RadioButton
Dim downRadioButton As RadioButton
Dim dlRadioButton As RadioButton
Dim drRadioButton As RadioButton
Dim leftRadioButton As RadioButton
Dim rightRadioButton As RadioButton
Dim wind1b As Decimal
Dim resultb As Integer
Select Case True
Case windGroupBox.upRadioButton.Checked
wind1b = 0.095
Case windGroupBox.ulRadioButton.Checked
wind1b = 0.085
Case windGroupBox.urRadioButton.Checked
wind1b = 0.075
Case windGroupBox.downRadioButton.Checked
wind1b = 0.07
Case windGroupBox.dlRadioButton.Checked
wind1b = 0.065
Case windGroupBox.drRadioButton.Checked
wind1b = 0.06
Case windGroupBox.leftRadioButton.Checked
wind1b = 0.055
Case windGroupBox.rightRadioButton.Checked
wind1b = 0.05
End Select
resultb = wind1b
End Sub

I have tested it now, and as I posted in the comment, upRadioButton (and all the others) are (most probably) direct children of the form. That means you can write
Case Me.upRadioButton.Checked
(or you can leave out the Me.)
Btw., if you have pulled the controls onto the form in the designer, you should remove those dim ... lines. The controls are declared in another file that is hidden by default. Your lines declare variables again that shadow the access to the actual form controls, meaning that you get errors when you try to access them without Me..

Related

vb.net chnage textbox.text value with for-next loop

I intend to get the value of a Textbox (TNumb.Text) and then use this value (an integer) in other buttons (Private Subs). The number should start at 1 and end at a comboboxvalue (CmbMax.Text) specified by the user. Starting from 1, the program should run through the code lines, perform clicks of the following buttons - (BFirst, BSecond, BFinal). When done with the clicks of these buttons in succession, the TNumb.Text should increase by 1 to 2 and then BFirst, BSecond & BFinal are clicked. Afterward this, another increment of 1 should be done and TNumb should increase again by 1 to 3. This should continue till TMax.Text is reached. All the three buttons should be clicked too. I have used a for-next loop and several other modifications but haven't been successful. Let me also add that in the Load event of the form, I made TNumb.Text=0. The major problem is that when TNumb.text = 1 runs, the value is not increased to 2 for the use and subsequent clicking of the other three buttons. Help will be greatly appreciated. Below is the unsuccessful code I have used:
`
Private Sub Timer1_Tick(sender As Object, e as EventArgs) Handles Timer1.Tick
For j As Integer = 1 to CInt(CmbMax.Text)
TLine.Text = j
Dim h as string = "10:00:00 AM"
Dim g as string = TimeOfDay
Dim w as string = tCpyT.Text
Dim n as string = tTmNow.Text
If h = g or w = n Then
BFirst.PerformClick()
BSecond.PerformClick()
BFinal.PerformClick()
Next
End Sub
`

Vb.net Multiplying textbox with time and combo box value

I have a masked textbox with time value ("hh:mm" the duration the task wil take) and a combo box with 1-10.
How do I mutilply the masked textbox with the combo box to get the total dutation it would take in "hh:mm.
I have txttime1 as the first textbox that holds the time. (masked textbox)
Cmb1 with 1-10, this indicates how many times the txttime1 must be multiplied.
Txtdura1 as the answer to the multiplication. Basically the total duration of the event.
Any direction would be great.
Convert the string from the textbox to a TimeSpan. Then change to TotalMinutes which is a Double so you can perform multiplication. Take the result of the multiplication and change it to a TimeSpan .FromMinutes. You can then format the result in various ways.
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim ts As TimeSpan = TimeSpan.ParseExact(TextBox2.Text, "h\:m", CultureInfo.CurrentCulture)
Dim dblTotalMinutes As Double = ts.TotalMinutes
Dim Multiplier As Integer = CInt(ComboBox1.SelectedItem)
Dim MultipliedMinutes As Double = dblTotalMinutes * Multiplier
Dim MultipliedTimeSpan As TimeSpan = TimeSpan.FromMinutes(MultipliedMinutes)
'For days:hours:minutes
TextBox3.Text = MultipliedTimeSpan.ToString("d\:hh\:mm")
'Total Hours as double
TextBox4.Text = MultipliedTimeSpan.TotalHours.ToString
End Sub
Split the hours and minutes apart from each other, create a TimeSpan object and multiply that by the factor from the ComboBox:
Dim duration As TimeSpan = TimeSpan.Parse(txttime1.Text)
Txtdura1 = duration.Multiply(Integer.Parse(Cmb1.Text)).ToString("hh\:mm")
Relevant documentation:
TimeSpan.Parse()
TimeSpan.Multiply()
TimeSpan.ToString()

Make a list of all Textbox values in a form

I asked a question before, related to this topic. I wanted to know how to make a loop that adds together all of the textbox.text values in a form. This is the response I got, and it works perfectly.
sum = 0
For Each ctrl In Me.Controls.OfType(Of TextBox)()
Dim txt As TextBox = DirectCast(ctrl, TextBox)
Dim i As Integer = 0
If Integer.TryParse(txt.Text, i) Then
sum = sum + i
End If
Next
What I want to know is how can I, using the same button, get the values of each text box in a form, and put them all together in one list/array?
It's quite a simple problem, but I just can't find the exact syntax for this anywhere.
Also, if a use a similar loop, but do list.Add(text), it just breaks the first loop.
Thanks in advance!
You just need to add the parsed integers to a List(Of Int32):
Dim list As New List(Of Int32)
For Each txt In Me.Controls.OfType(Of TextBox)()
Dim num As Int32 = 0
If Integer.TryParse(txt.Text, num) Then
list.Add(num)
End If
Next
Dim sum = list.Sum() ' easier ;-)
The cast to TextBox is not needed because ctrl is already a TextBox thanks to OfType.
Dim listOfTextBox = Me.Controls.OfType(Of TextBox).ToList()
Dim listOfValue = listOfTextBox.Select(Function(e) Integer.Parse(e.Text))
Dim sum = listOfValue.Sum()

Accessing buttons names using variables

In visual basic I want to be able to access a button's name using the number stored in a variable.
For example if I have 24 buttons that are all named 'Button' with the numbers 1, 2, 3... 22, 23, 24 after it. If I want to then change the text in the first eight buttons how would I do that.
Here's my example to help show what I mean:
For i = 1 to 8
Button(i).text = "Hello"
Next
The proposed solutions so far will fail if the Buttons are not directly contained by the Form itself. What if they are in a different container? You could simple change "Me" to "Panel1", for instance, but that doesn't help if the Buttons are spread across multiple containers.
To make it work, regardless of the Buttons locations, use the Controls.Find() method with the "searchAllChildren" option:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ctlName As String
Dim matches() As Control
For i As Integer = 1 To 8
ctlName = "Button" & i
matches = Me.Controls.Find(ctlName, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is Button Then
Dim btn As Button = DirectCast(matches(0), Button)
btn.Text = "Hello #" & i
End If
Next
End Sub
For index As Integer = 1 To 8
CType(Me.Controls("Button" & index.ToString().Trim()),Button).Text = "Hello"
Next
Use LINQ and you're good to go:
Dim yourButtonArray = yourForm.Controls.OfType(of Button).ToArray
' takes all controls whose Type is Button
For each button in yourButtonArray.Take(8)
button.Text = "Hello"
Next
Or
Dim yourButtonArray = yourForm.Controls.Cast(of Control).Where(
Function(b) b.Name.StartsWith("Button")
).ToArray
' takes all controls whose name starts with "Button" regardless of its type
For each button in yourButtonArray.Take(8)
button.Text = "Hello"
Next
In any case, .Take(8) will iterate on the first 8 items stored inside yourButtonArray

How to find the highest number in all textboxes

I'm close to finishing a small program that i started, but got stuck with the last part. And I'm just starting to learn programming, so might be a stupid question.
How can i get the text box with the highest number out of 16 boxes,each having its own number, but at the same time keep track of which one still has the highest number every second? Quite confused about the updating it every second part.
All help is appreciated!
Code suggestion
Private Sub findTopTextBox()
Dim topValue As Integer
Dim topTextBox As TextBox
For Each ctrl As Control In Me.Controls 'all the controls on your form
If TypeOf ctrl Is TextBox Then
Dim thisValue As Integer
If Integer.TryParse(DirectCast(ctrl, TextBox).Text, thisValue) Then
If thisValue > topValue Then
topValue = thisValue
topTextBox = DirectCast(ctrl, TextBox)
End If
End If
End If
Next
Debug.Print(String.Concat(topTextBox.Name, " has the top value at: ", topValue))
End Sub
In order to test it each second, you'll need to add a Timer and call this method repeatedly.
You don't really need to check every second only check when a change was made in one of those textboxes
You could handle all your textboxes LostFocus event (using the same method to handle them all) ; get it's text, verify it's a number and if it's greater than the current max update it (along with it's "location" : the textbox control)
That way you always know which one is the greatest
Something along this should do it (typed directly here so not tested) :
Dim maxNumber As Integer, maxTextBox As TextBox
Sub TextBoxes_LostFocus(sender As Object, e As EventArgs) Handles textbox1.LostFocus, textbox2.LostFocus ' ... for all textboxes
Dim tbSender = DirectCast(sender, TextBox)
Dim number As Integer
' Should we update maxTextBox if number = maxNumber ? (if yes change the > to >=)
If Integer.TryParse(tbSender.Text, number) AndAlso number > maxNumber Then
maxNumber = number
maxTexBox = tbSender
End If
End Sub