Solved

Pass values of RadioButton from form to a class

Posted on 2011-03-03
4
508 Views
Last Modified: 2013-12-14
Hey guys,
I have a problem with the Radio Buttons.
I have a form and it has a 2 radio buttons,so depending on which one they select im saving this info like

In Form1.cs ->
public bool radiobutton1
{
 get{ radioButton1.checked;}
}
public bool radiobutton2
{
 get{ radioButton2.checked;}
}




and in my other class where i would like to use it i have

public class checkButtons.
{
public void Write()
{
Form1 form = new Form1();
 if(form.radiobutton1)
{
 //i do something here
}else if(form.radiobutton2)
{
   //do something.
}
}
but for some reason i cant get this to work,
plz help
0
Comment
Question by:ferocious
  • 2
4 Comments
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 35032175
You need to get the instance of the actual form that has the radio buttons. You are instancing a new form and you are not assigning an instantiated form to your new form. That sounds confusing but here:


So if your form with the buttons is frmRadioSelected then do this

Dim Form1 as frmRadioSelected // Now you have an instance.

Open in new window

Now test your Form1 to see if it exists and create it if it doesn't.

If Form1 Is Nothing Then
    Form1 = New frmRadioSelected
End If
Form1.Show()

Open in new window

0
 

Author Comment

by:ferocious
ID: 35032263
i did that but im still not able to get the actual radiobutton.checked
i am using this radiobuttons inside a class where i do something based on their selection and then
im calling this back into the Form1.cs

i don't know if i have made it more difficult .
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35032338
You need to make a public variable in checkButtons to hold the reference to the actual instance of Form1:
public class checkButtons
{

    public Form1 f1; // <-- Here is the public variable

    public void Write()
    {
    if(f1.radiobutton1)
    {
        //i do something here
    }else if(f1.radiobutton2)
    {
        //do something. 
    }
}

Open in new window


In Form1, when you create your instance of checkButtons, you set the reference:
// ...from within Form1...
    private void Foo()
    {
        checkButtons cb = new checkButtons();
        cb.f1 = this; // <-- setting the reference
    }

Open in new window

0
 

Author Comment

by:ferocious
ID: 35033223
Hey Thanks Guys
Its working now.
cheers
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

895 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

21 Experts available now in Live!

Get 1:1 Help Now