pgilfeather
asked on
How do you programmatilly select a value from a radiobuttonlist?
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
Suppose I have
id=Radiobuttonlist1
how do I automatically make a selection from this???
Regards
Paul G
Label1.Text = "You selected: " & RadioButtonList1.SelectedI tem.Text
~b
~b
ASKER
Thanks but, with all due respect none of the above is really a serious answer to my question
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.ICon tainer
'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.Group Box
Friend WithEvents RadioButton1 As System.Windows.Forms.Radio Button
Friend WithEvents RadioButton2 As System.Windows.Forms.Radio Button
Friend WithEvents RadioButton3 As System.Windows.Forms.Radio Button
Friend WithEvents RadioButton4 As System.Windows.Forms.Radio Button
Friend WithEvents RadioButton5 As System.Windows.Forms.Radio Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextB ox
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.GroupBox1 = New System.Windows.Forms.Group Box
Me.RadioButton1 = New System.Windows.Forms.Radio Button
Me.RadioButton2 = New System.Windows.Forms.Radio Button
Me.RadioButton3 = New System.Windows.Forms.Radio Button
Me.RadioButton4 = New System.Windows.Forms.Radio Button
Me.RadioButton5 = New System.Windows.Forms.Radio Button
Me.TextBox1 = New System.Windows.Forms.TextB ox
Me.GroupBox1.SuspendLayout ()
Me.SuspendLayout()
'
'GroupBox1
'
Me.GroupBox1.Controls.Add( Me.RadioBu tton5)
Me.GroupBox1.Controls.Add( Me.RadioBu tton4)
Me.GroupBox1.Controls.Add( Me.RadioBu tton3)
Me.GroupBox1.Controls.Add( Me.RadioBu tton2)
Me.GroupBox1.Controls.Add( Me.RadioBu tton1)
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.TextBox 1)
Me.Controls.Add(Me.GroupBo x1)
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.CheckedChange d, RadioButton2.CheckedChange d, _
RadioButton3.CheckedChange d, RadioButton4.CheckedChange d, RadioButton5.CheckedChange d
TextBox1.Text = sender.text
End Sub
End Class
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.ICon
'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.Group
Friend WithEvents RadioButton1 As System.Windows.Forms.Radio
Friend WithEvents RadioButton2 As System.Windows.Forms.Radio
Friend WithEvents RadioButton3 As System.Windows.Forms.Radio
Friend WithEvents RadioButton4 As System.Windows.Forms.Radio
Friend WithEvents RadioButton5 As System.Windows.Forms.Radio
Friend WithEvents TextBox1 As System.Windows.Forms.TextB
<System.Diagnostics.Debugg
Me.GroupBox1 = New System.Windows.Forms.Group
Me.RadioButton1 = New System.Windows.Forms.Radio
Me.RadioButton2 = New System.Windows.Forms.Radio
Me.RadioButton3 = New System.Windows.Forms.Radio
Me.RadioButton4 = New System.Windows.Forms.Radio
Me.RadioButton5 = New System.Windows.Forms.Radio
Me.TextBox1 = New System.Windows.Forms.TextB
Me.GroupBox1.SuspendLayout
Me.SuspendLayout()
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(
Me.GroupBox1.Controls.Add(
Me.GroupBox1.Controls.Add(
Me.GroupBox1.Controls.Add(
Me.GroupBox1.Controls.Add(
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.TextBox
Me.Controls.Add(Me.GroupBo
Me.Name = "frmRadioButt"
Me.Text = "frmRadioButt"
Me.GroupBox1.ResumeLayout(
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub CheckRadio(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChange
RadioButton3.CheckedChange
TextBox1.Text = sender.text
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
so i think you would want...
RadioButtonList.Items("id" ).Selected = True
RadioButtonList.Items("id"
sorry, omit the quotes
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
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
Hi,
Try this
radiobuttonlist1.item(x).s elected = true
GL
Try this
radiobuttonlist1.item(x).s
GL
sorry about the vb6 set call
ASKER
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.App Settings(" mydatasour ce"))
Dim objConnection as new sqlConnection(cnn)
Dim objCommand as new SqlCommand("procGetQuestio nAttribute s", ObjConnection)
objCommand.CommandType = CommandType.StoredProcedur e
Dim ObjQuestionID1 as new SqlParameter("@QuestionID" ,SqlDbType .int)
ObjCommand.Parameters.Add( ObjQuestio nID1)
ObjQuestionID1.Direction=P arameterDi rection.In put
ObjQuestionID1.Value=MyArr ay(0)
Try
objConnection.Open()
RadQ1.DataSource=ObjComman d.ExecuteR eader()
RadQ1.DataTextField="Answe rDescripti on"
RadQ1.DataValueField = "CorrectAnswers"
RadQ1.DataBind()
objConnection.Close()
Catch ex as Exception
Response.Write("Error:" & ex.Message & "<br/>")
End Try
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.App
Dim objConnection as new sqlConnection(cnn)
Dim objCommand as new SqlCommand("procGetQuestio
objCommand.CommandType = CommandType.StoredProcedur
Dim ObjQuestionID1 as new SqlParameter("@QuestionID"
ObjCommand.Parameters.Add(
ObjQuestionID1.Direction=P
ObjQuestionID1.Value=MyArr
Try
objConnection.Open()
RadQ1.DataSource=ObjComman
RadQ1.DataTextField="Answe
RadQ1.DataValueField = "CorrectAnswers"
RadQ1.DataBind()
objConnection.Close()
Catch ex as Exception
Response.Write("Error:" & ex.Message & "<br/>")
End Try
ASKER
GERENTE
what do I put in for x
radiobuttonlist1.item(x).s elected = true
what do I put in for x
radiobuttonlist1.item(x).s
X = index of the item
if you want the "first radio button" use.
radiobuttonlist1.item(0).s elected = true
GL
if you want the "first radio button" use.
radiobuttonlist1.item(0).s
GL
ASKER
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.Valu e = "" Then
'RadFSAQ1.SelectedItem.Tex t =NoSelectedText
RadFSAQ1.SelectedItem.Valu e =NoSelectedValue
End If
The compiler is stopping at the first line and moaning about it being set to a null object.
If RadFSAQ1.SelectedItem.Text
'RadFSAQ1.SelectedItem.Tex
RadFSAQ1.SelectedItem.Valu
End If
ASKER
GERENTE,
This is the error that occurred with radiobuttonlist1.item(0).s elected = true
BC30456: 'item' is not a member of 'System.Web.UI.WebControls .RadioButt onList'.
This is the error that occurred with radiobuttonlist1.item(0).s
BC30456: 'item' is not a member of 'System.Web.UI.WebControls
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(your Index).Sel ected = True
-------------------------- ------
also, there is no Item() member, its items() like i said...
--------------------------
geez, i read it totally wrong.
RadioButtonList.Items(your
--------------------------
ASKER
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
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
' this code must go into #Region " Web Form Designer Generated Code "
Protected WithEvents RadFSAQ1 As System.Web.UI.WebControls. RadioButto nList
If RadFSAQ1.Items.Count > 0 Then
If RadFSAQ1.SelectedItem.Text .Equals("" ) Or RadFSAQ1.SelectedItem.Valu e.Equals(" ") Then
RadFSAQ1.SelectedItem.Sele cted = False
RadFSAQ1.SelectedItem.Valu e = "NoSelectedValue"
End If
End If
If this is not the solution can you explain me what you need to do ?
GL
Protected WithEvents RadFSAQ1 As System.Web.UI.WebControls.
If RadFSAQ1.Items.Count > 0 Then
If RadFSAQ1.SelectedItem.Text
RadFSAQ1.SelectedItem.Sele
RadFSAQ1.SelectedItem.Valu
End If
End If
If this is not the solution can you explain me what you need to do ?
GL
ASKER
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
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
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.Chec ked = False Then
'Your next line is commented
'RadFSAQ1.SelectedItem.Tex t = NoSelectedText
RadFSAQ1.SelectedItem.Chec ked = NoSelectedValue
End If
regards
Corey
The Checked value is boolean
I don't know if that is what is causing the problem though
If RadFSAQ1.SelectedItem.Text
RadFSAQ1.SelectedItem.Chec
'Your next line is commented
'RadFSAQ1.SelectedItem.Tex
RadFSAQ1.SelectedItem.Chec
End If
regards
Corey
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.Coun t - 1
If RadioButtonList.Items(nCou nt).Select ed = False Then
RadioButtonList.Items(nCou nt).Value = "NoSelectedValue"
End if
Next
For nCount = 0 To RadioButtonList.Items.Coun
If RadioButtonList.Items(nCou
RadioButtonList.Items(nCou
End if
Next
Regards,
Corey