Solved

How do you programmatilly select a value from a radiobuttonlist?

Posted on 2004-10-12
21
294 Views
Last Modified: 2010-04-23
How do you programmatilly select a value from a radiobuttonlist?

Suppose I have

id=Radiobuttonlist1

how do I automatically make a selection from this???

Regards

Paul G
0
Comment
Question by:pgilfeather
  • 7
  • 6
  • 4
  • +2
21 Comments
 
LVL 13

Expert Comment

by:Corey2
ID: 12287898
radioButton1.PerformClick()

Regards,

Corey
0
 
LVL 8

Expert Comment

by:bramsquad
ID: 12287912
Label1.Text = "You selected: " & RadioButtonList1.SelectedItem.Text

~b
0
 

Author Comment

by:pgilfeather
ID: 12287974
Thanks but, with all due respect  none of the above is really a serious answer to my question
0
 
LVL 27

Expert Comment

by:planocz
ID: 12288021
Sample code....

Public Class frmRadioButt
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton3 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton4 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton5 As System.Windows.Forms.RadioButton
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.RadioButton1 = New System.Windows.Forms.RadioButton
        Me.RadioButton2 = New System.Windows.Forms.RadioButton
        Me.RadioButton3 = New System.Windows.Forms.RadioButton
        Me.RadioButton4 = New System.Windows.Forms.RadioButton
        Me.RadioButton5 = New System.Windows.Forms.RadioButton
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.RadioButton5)
        Me.GroupBox1.Controls.Add(Me.RadioButton4)
        Me.GroupBox1.Controls.Add(Me.RadioButton3)
        Me.GroupBox1.Controls.Add(Me.RadioButton2)
        Me.GroupBox1.Controls.Add(Me.RadioButton1)
        Me.GroupBox1.Location = New System.Drawing.Point(12, 24)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(244, 196)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "GroupBox1"
        '
        'RadioButton1
        '
        Me.RadioButton1.Location = New System.Drawing.Point(12, 28)
        Me.RadioButton1.Name = "RadioButton1"
        Me.RadioButton1.Size = New System.Drawing.Size(212, 24)
        Me.RadioButton1.TabIndex = 0
        Me.RadioButton1.Text = "RadioButton1"
        '
        'RadioButton2
        '
        Me.RadioButton2.Location = New System.Drawing.Point(12, 60)
        Me.RadioButton2.Name = "RadioButton2"
        Me.RadioButton2.Size = New System.Drawing.Size(212, 24)
        Me.RadioButton2.TabIndex = 1
        Me.RadioButton2.Text = "RadioButton2"
        '
        'RadioButton3
        '
        Me.RadioButton3.Location = New System.Drawing.Point(12, 92)
        Me.RadioButton3.Name = "RadioButton3"
        Me.RadioButton3.Size = New System.Drawing.Size(212, 24)
        Me.RadioButton3.TabIndex = 2
        Me.RadioButton3.Text = "RadioButton3"
        '
        'RadioButton4
        '
        Me.RadioButton4.Location = New System.Drawing.Point(12, 124)
        Me.RadioButton4.Name = "RadioButton4"
        Me.RadioButton4.Size = New System.Drawing.Size(212, 24)
        Me.RadioButton4.TabIndex = 3
        Me.RadioButton4.Text = "RadioButton4"
        '
        'RadioButton5
        '
        Me.RadioButton5.Location = New System.Drawing.Point(12, 156)
        Me.RadioButton5.Name = "RadioButton5"
        Me.RadioButton5.Size = New System.Drawing.Size(212, 24)
        Me.RadioButton5.TabIndex = 4
        Me.RadioButton5.Text = "RadioButton5"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(44, 236)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(132, 20)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = ""
        '
        'frmRadioButt
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.GroupBox1)
        Me.Name = "frmRadioButt"
        Me.Text = "frmRadioButt"
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region


    Private Sub CheckRadio(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, _
    RadioButton3.CheckedChanged, RadioButton4.CheckedChanged, RadioButton5.CheckedChanged
        TextBox1.Text = sender.text
    End Sub
End Class
0
 
LVL 8

Accepted Solution

by:
bramsquad earned 500 total points
ID: 12288027
geez, i read it totally wrong.

RadioButtonList.Items(yourIndex).Selected = True
0
 
LVL 8

Expert Comment

by:bramsquad
ID: 12288046
so i think you would want...

RadioButtonList.Items("id").Selected = True
0
 
LVL 8

Expert Comment

by:bramsquad
ID: 12288088
sorry, omit the quotes
0
 
LVL 13

Expert Comment

by:Corey2
ID: 12288108
Or if you want to figure out which radiobutton has been selected

select case True
   case id(1)


   case id(2)


  case id(3)

End select

or

dim ThisButton as object
Dim SelectedButton as radiobutton

For each Thisbutton in ID
     if thisbutton.value = True then
        set selectedbutton = thisbutton
        exit for
    end if
next

Corey
0
 
LVL 10

Expert Comment

by:Hans Langer
ID: 12288124
Hi,
Try this
radiobuttonlist1.item(x).selected = true
GL
0
 
LVL 13

Expert Comment

by:Corey2
ID: 12288145
sorry about the vb6 set call
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:pgilfeather
ID: 12288192
Corey2,

This is exactly what I want to do loop through and check which ones have been selected and if not selected then select an incorrect answer

here is how I am generating each question from the datasource

Dim cnn As String = (ConfigurationSettings.AppSettings("mydatasource"))
    Dim objConnection as new sqlConnection(cnn)
      Dim objCommand as new SqlCommand("procGetQuestionAttributes", ObjConnection)
    objCommand.CommandType = CommandType.StoredProcedure
      
      Dim ObjQuestionID1 as new SqlParameter("@QuestionID",SqlDbType.int)
      ObjCommand.Parameters.Add(ObjQuestionID1)
      ObjQuestionID1.Direction=ParameterDirection.Input
      ObjQuestionID1.Value=MyArray(0)
      
      Try
      objConnection.Open()
      RadQ1.DataSource=ObjCommand.ExecuteReader()
      RadQ1.DataTextField="AnswerDescription"
      RadQ1.DataValueField = "CorrectAnswers"
      RadQ1.DataBind()
      
      objConnection.Close()
      
      Catch ex as Exception
      Response.Write("Error:" & ex.Message & "<br/>")
      End Try
0
 

Author Comment

by:pgilfeather
ID: 12288222
GERENTE

what do I put in for x

radiobuttonlist1.item(x).selected = true
0
 
LVL 10

Expert Comment

by:Hans Langer
ID: 12288270
X = index of the item

if you want the "first radio button" use.
radiobuttonlist1.item(0).selected = true
GL
0
 

Author Comment

by:pgilfeather
ID: 12288480
Heres the code that is generating the error

The compiler is stopping at the first line and moaning about it being set to a null object.


If RadFSAQ1.SelectedItem.Text = "" or RadFSAQ1.SelectedItem.Value = "" Then
'RadFSAQ1.SelectedItem.Text =NoSelectedText
RadFSAQ1.SelectedItem.Value =NoSelectedValue
End If
0
 

Author Comment

by:pgilfeather
ID: 12288518
GERENTE,

This is the error that occurred with radiobuttonlist1.item(0).selected = true

BC30456: 'item' is not a member of 'System.Web.UI.WebControls.RadioButtonList'.
0
 
LVL 8

Expert Comment

by:bramsquad
ID: 12288520
i hate to do this...but i stated that exact answer before GERENTE did...

also, there is no Item() member, its items() like i said...


--------------------------------
geez, i read it totally wrong.

RadioButtonList.Items(yourIndex).Selected = True
--------------------------------
0
 

Author Comment

by:pgilfeather
ID: 12288598
bramsquad

Im really sorry bram, it wasnt intentional, Im just getting stressed cos I havent solved this problem yet.

I will have another look at your original postings. Sorry.

Regards

Paul
0
 
LVL 10

Expert Comment

by:Hans Langer
ID: 12288636
' this code must go into #Region " Web Form Designer Generated Code "
Protected WithEvents RadFSAQ1 As System.Web.UI.WebControls.RadioButtonList


If RadFSAQ1.Items.Count > 0 Then
         If RadFSAQ1.SelectedItem.Text.Equals("") Or RadFSAQ1.SelectedItem.Value.Equals("") Then
            RadFSAQ1.SelectedItem.Selected = False
            RadFSAQ1.SelectedItem.Value = "NoSelectedValue"
         End If
End If

If this is not the solution can you explain me what you need to do ?
GL
0
 

Author Comment

by:pgilfeather
ID: 12288743
GERENTE,

I have accepted another answer but you have helped me a lot on this question so I am going to open up another question to continue the rest of what I am trying to do. Please check this VB.NET section tomorrow.

Thanks very much for all your help, I really do appreciate it very much.

I'll continue this thread tomorrow.

Thanks Guys/Gals

Paul G
0
 
LVL 13

Expert Comment

by:Corey2
ID: 12288758
Radiobutton value is stored in the Checked property.
The Checked value is boolean
I don't know if that is what is causing the problem though

If RadFSAQ1.SelectedItem.Text = "" or _
RadFSAQ1.SelectedItem.Checked = False Then

'Your next line is commented
'RadFSAQ1.SelectedItem.Text = NoSelectedText
RadFSAQ1.SelectedItem.Checked = NoSelectedValue

End If

regards
Corey
0
 
LVL 8

Expert Comment

by:bramsquad
ID: 12288783
you said the purpose of your code was to select all "incorrect" values which have not been selected?  what about this?

For nCount = 0 To RadioButtonList.Items.Count - 1
      If RadioButtonList.Items(nCount).Selected = False Then
             RadioButtonList.Items(nCount).Value = "NoSelectedValue"
      End if
Next
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now