Wrox Programmer Forums
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 16th, 2007, 02:09 AM
Registered User
 
Join Date: Jun 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Multiple Radio Buttons List

Hi,

I was trying to build a list of 5 options. The user has to choose only 1 of these options. I tried to do it with radio buttons but till now I've only managed to do it with 2 options (2 radio buttons). Can someone help me to code for 5 options to choose 1 pls? I just can't find the way to it
:(
 
Old June 18th, 2007, 01:23 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I just created a new project and added 5 radio buttons to the default form. When I ran it, the buttons worked as you would expect radio buttons to operate—only one at a time having the black button in the center.

Does your attempt run this well, and it is a deeper question you are asking; or are you unable to get this minimal behavior to operate?
 
Old June 19th, 2007, 01:29 AM
Registered User
 
Join Date: Jun 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by BrianWren
 I just created a new project and added 5 radio buttons to the default form. When I ran it, the buttons worked as you would expect radio buttons to operate—only one at a time having the black button in the center.

Does your attempt run this well, and it is a deeper question you are asking; or are you unable to get this minimal behavior to operate?
I am still new to VB and OOP in general and for now I'm trying to do some discovery learning. My problem is that I don't know how to code for those 5 options in order to make the user choose only one of the radio buttons. I can only make the user choose 1 out of 2 radio buttons. So i would be grateful if someone had to send me the code to reach my intended result. Perhaps it's not radio buttons I should be using? I'm quite confused...

 
Old June 19th, 2007, 08:19 AM
Authorized User
 
Join Date: May 2006
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A little example :

Public Class Form1

   Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
      ButtonsChanged()
   End Sub

   Private Sub RadioButton2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
      ButtonsChanged()
   End Sub


   Private Sub RadioButton3_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
      ButtonsChanged()
   End Sub


   Private Sub RadioButton4_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
      ButtonsChanged()
   End Sub


   Private Sub RadioButton5_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged
      ButtonsChanged()
   End Sub

   Private Sub ButtonsChanged()
      If RadioButton1.Checked = True Then

      End If

      If RadioButton2.Checked = True Then

      End If


   End Sub
End Class

 
Old June 19th, 2007, 11:36 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Well, when you put radio buttons on the form, the interaction between the form and the radio-button controls handles this exclusivity (only 1 at a time selected) for you--you don't have to write that code. Or am I missing something in your question.
 
Old June 19th, 2007, 11:51 AM
Authorized User
 
Join Date: Nov 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196
Default

Is it possible that some of the RadioButtons are in a panel or groupbox? If so they would operate independent of the others.




Best Regards,
Earl Francis
 
Old June 21st, 2007, 03:27 PM
Registered User
 
Join Date: Jun 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry but I can't understand what ButtonsChanged() is.
Please be patient with me since I'm still trying to grasp the very little basics of VB express 2005.

My little project in mind would be this. I hope I am clear in my wording:

So, I would like the user to have 5 options, from which he would be able to choose just one.
After choosing that option that he wants, he would click a button.
This button would be displaying a message box informing the user about the option he has just chosen.

Till now i've tried this with radio buttons but i haven't much grasped how it all works :/

Practically that is the problem case I'm trying to solve. and being still new I would appreciate it a lot if someone were to come out with the code to help me out pls.

 
Old June 22nd, 2007, 06:52 AM
Authorized User
 
Join Date: Nov 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196
Default

You should probably start with a new project and try the code below. Something in your current code is not allowing all of the radio buttons to function as a group.

Public Class Form1

    '* This variable will store the "Text" value of the radio button chosen.
    Dim strRadioButtonValue As String

    '* The "CheckChanged" code below functions for all radio buttons
    '* Read up on "Handles" as it pertains to multiple controls having
    '* the same functionality.
    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
        RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, _
        RadioButton3.CheckedChanged, RadioButton4.CheckedChanged, _
        RadioButton5.CheckedChanged

        Dim rb As RadioButton = CType(sender, RadioButton)

        '* The text of the selected radio button is set here.
        Me.strRadioButtonValue = rb.Text

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '* Show the results.
        MessageBox.Show(Me.strRadioButtonValue, "", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub
End Class




Best Regards,
Earl Francis
 
Old June 28th, 2007, 12:28 AM
Registered User
 
Join Date: Jun 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Earl Francis,

10x for your help. I have actually tried your code and it worked, even though I admit I still didn't know you could declare a RadioButton, don't know what (CType and server, RadioButton). For now i'm ok that it worked :)

However, I had managed to get to another conclusion. Suddenly the radio buttons started working. I guess there really was something wrong in the code before. It's probabbly more time-consuming in the long-run but, hehe, I was simply happy that I found a solution.

This is what I've managed to get :


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If RadioButton1.Checked = True Then
            MsgBox("b1")
        End If

        If RadioButton2.Checked = True Then
            MsgBox("b2")
        End If

        If RadioButton3.Checked = True Then
            MsgBox("b3")
        End If

        If RadioButton4.Checked = True Then
            MsgBox("b4")
        End If
    End Sub


Thanks a lot for your help!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Radio Buttons and Calculations Student101 Java Basics 4 June 21st, 2009 11:02 AM
validation in radio buttons MunishBhatia ASP.NET 2.0 Professional 5 December 11th, 2007 11:15 AM
Validation Radio Buttons jonsey Classic ASP Professional 1 June 6th, 2007 11:48 PM
How to use the radio buttons? ben_VB VB.NET 2002/2003 Basics 1 January 18th, 2005 12:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.