Solved

Pass values of RadioButton from form to a class

Posted on 2011-03-03
4
507 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hey Thanks Guys
Its working now.
cheers
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

744 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

17 Experts available now in Live!

Get 1:1 Help Now