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
Solved

Pass values of RadioButton from form to a class

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

837 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