Radiobuttons Databind

  • Thread starter Thread starter Rourke Eleven
  • Start date Start date
R

Rourke Eleven

I have looked and searched. What good is the databind property on
Radiobuttons? How does one go about actually using it? What is a good
resource on this? I understand that I can easily get/set the information I
need programmatically, but what about the databind property I just don't
understand it's value.







Rourke Eleven
Cthulhu saves sinners - in case he's hungry later!
 
OK, to illustrate. Create an access database with two fields in it. A ID (
PK ) field and a Yes/No field called 'Data'. Add Three records with the
centre on unchecked.

Using server explorer, drag the table to the form and create a DataSet
DS(DS1) using the wizard.

Drag a Checkbox onto your form (cb1) and a button (btnUP), put the following
code in and run it. You will see by clicking the button once the CB
disapears and then once again, it appears. You have bound the Data to the
control and are using the binding context of the form to move through the
bound records.

Voilla !!

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
DA.Fill(Ds1)
CB1.DataBindings.Add("Checked", Ds1.Tables(0), "Data")
End Sub

Private Sub btnUP_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnUP.Click
Dim bm As BindingManagerBase
bm = Me.BindingContext.Item(Ds1.Tables(0))
bm.Position += 1
End Sub
 
For all others this is regarding VB.Net Windows Forms not ASP.Net

Terry,

Ok I understand that but what about in the the context of a group of
Radiobuttons and using mutual exclusivity.

For example I have field in a database that I want to have the values of 0,
1, 2 or 3 (or actually any values I want to Assign)

So Radiobutton 1 sets the value to 0 and so on.

How does databinding work in this instance?

Or even if I had a field in a database that was bool and I wanted to use
Radiobuttons instead of a Checkbox.

I would love to be able to set all of this with properties instead of
programmatically with a decision stucture and then binding.
 
OK, that was not clear in your OP. Ken posted link but I have not checked
that out, if you are still having difficulties by tomorrow, I'll try and
help
 
Ken's answer is great and I think I will use it, but it still doesn't answer
1 of my questions. Why have databinding on Radiobuttons? What is its use?

I can't think of a time that I would have just a single radiobutton on a
form. As the Article that Ken sent points out Microsoft needs to think
about building in a control that does what Duncan Mackenzie does in his user
control. Make a Radiobutton ListControl with a simple interface like that
of a Web Combobox which you can input your own Members with a Text/values in
a listcontrol container or you can databind.
 
This is great. It will work for what I am doing. Makes me wonder why
Microsloth didn't do it first.

Thanks Ken!!!
 
No of course you would not have a single Radio button on a form, however, I
personally have used bound radio buttons on a form along with other
controls. I wrote a helpdesk system and some of the selections were bound to
Radio buttons and checkboxes.

It's also possible to display checkboxes on a datagrid bound in the same way
with a bit of binding magic and a dice of Controls. But that gets a little
complex.

In asnwer to you question ( and im not Microsoft ) ; why not ?
 
I am not just trying to argue, but what would you bind to the radiobutton?
The Text property? Help me here I am a little lost. Normally I would want
to bind data so that I could effect a change to the backend data or display
the data.
 
Well, in my first reply I showed you how to bind a checkbox to a field in a
database. Is this not a valid use ?
 
Here we have an Class Person. Within it we declare a public event called
CheckedChanged, this is made up of the property which the binding manager
listens for an event for once binding has been set. When the Property
changes from checked to unchecked we call a helper function which raises
this event and the binding manager syncs the control ( rad1 ) to this
property.

Button1.click changes the property of the aPerson object to checked, this is
then reflected through the bindings in the rad1 state.

On_Load is where we set the bindings

HTH

Private WithEvents aPerson As New Person

Public Class Person

Public Event CheckedChanged As EventHandler

Private m_checked As Boolean

Public Sub New()
m_checked = False
End Sub

Public Property Checked() As Boolean
Get
Return m_checked
End Get
Set(ByVal value As Boolean)
m_checked = value
PropertyChanged(EventArgs.Empty)
End Set
End Property

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
rad1.DataBindings.Add("Checked", aPerson, "Checked")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
aPerson.checked = True
End Sub
 
I forgot to include the forms class level declaration of the aPerson

Private WithEvents aPerson As New Person



Cheers
 
Back
Top