Solved

Pass values of RadioButton from form to a class

Posted on 2011-03-03
4
511 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

730 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