3 Replies - 292 Views - Last Post: 15 November 2017 - 02:15 PM Rate Topic: -----

#1 PLOstyle  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 25-May 15

Adding to two listboxes from one textbox with two radio buttons

Posted 15 November 2017 - 11:18 AM

I'm doing a exercise from the book for practice, to enter a grade and the put in a male or female listbox then do the average.
I choose to use a radio button for male and female. But to no avail i can't even get it to add in the list box. After i plan to do the count of the list box and the average in the calculate button . Any Advice would be greatful.....
 

Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
 
        gardeTextBox.Text = String.Empty


        
        Double.TryParse(gradeTextBox.Text, CDbl(grade > 0 And grade<= 4.0))



        If maleRadioButton.Checked = True Then
             
            MaleStudents.Items.Add(gradeTextBox.Text)

        ElseIf femaleRadioButton.Checked = True Then
             
            FemaleStudents.Items.Add(gradeTextBox.Text)

        End If
End Sub
      


Is This A Good Question/Topic? 0
  • +

Replies To: Adding to two listboxes from one textbox with two radio buttons

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5121
  • View blog
  • Posts: 13,790
  • Joined: 18-April 07

Re: Adding to two listboxes from one textbox with two radio buttons

Posted 15 November 2017 - 01:17 PM

The part where you have...

CDbl(grade > 0 And grade<= 4.0)



Doesn't make a whole lot of sense. The expression grade > 0 And grade <= 4.0 is going to return a boolean value. So not a numeric value that you can convert to a double (theoretically). Not only that, Double.Tryparse expects the first parameter to be a string (so what you have is fine) but the second value to be an output variable.

So take a quick look at the definition of the TryParse method at the page below and study it a bit.

https://msdn.microso...(v=vs.110).aspx

Lastly, why are you trying to parse your textbox if just before you erase the textbox using a String.Empty assignment? I would be looking to do something similar to this...

Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
 
        ' This will be where we store the grade
        Dim grade as Double

        ' Try parsing the textbox and stick it in our grade variable. If it was successful
        ' It will return true, otherwise false. This will go into "result"
        Dim result as Boolean = Double.TryParse(gradeTextBox.Text, out grade)

        ' If successful and grade is in range, then move onto the listboxes
        ' We can also use the grade variable here to add to the listboxes.
        If result and (grade > 0 And grade <= 4.0) then
            If maleRadioButton.Checked = True Then
             
                MaleStudents.Items.Add(grade.ToString())

            ElseIf femaleRadioButton.Checked = True Then
             
                FemaleStudents.Items.Add(grade.ToString())

            End If
        Elseif 
            MessageBox.Show("Please enter a valid grade")
        EndIf
End Sub



This is off the top of my head, so be sure to check it out and see if this makes sense to your application. :)

This post has been edited by Martyr2: 15 November 2017 - 01:18 PM

Was This Post Helpful? 1
  • +
  • -

#3 PeterH  Icon User is offline

  • D.I.C Regular

Reputation: 58
  • View blog
  • Posts: 281
  • Joined: 03-September 09

Re: Adding to two listboxes from one textbox with two radio buttons

Posted 15 November 2017 - 01:20 PM

Where is 'grade' supposed to be coming from? You are initialising the text box and then trying to get a value from it. Normal use for .tryparse would be something like this,

dim grade as double
If Double.TryParse('your user input', grade) Then
   If grade > 0 AndAlso grade <= 4.0 Then
       ' check your radio button and add to the appropriate list
   End If
End If

Was This Post Helpful? 1
  • +
  • -

#4 PLOstyle  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 25-May 15

Re: Adding to two listboxes from one textbox with two radio buttons

Posted 15 November 2017 - 02:15 PM

Thank You for replying........I'm working on it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1