Solved

Pass values of RadioButton from form to a class

Posted on 2011-03-03
4
509 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

785 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