I am trying to use a radiobutton that I have attached to a from in VC++ .NET 2008

I have Named the RadioButton "BuyRadio". From what I know a radiobutton can have to stated, either true or false, wich meen pressed or not pressed.

What I am trying to do is this code wich meens that if the RadioButton is pressed this will evaluate but this does not compile. It does compile if I only write: if (BuyRadio).
Any idéas to this.

int SetNumber = 0;

if (BuyRadio = true)
{
     SetNumber = 1;
}

jennifer, does the program work when you use (BuyRadio)??

I am trying to use a radiobutton that I have attached to a from in VC++ .NET 2008

I have Named the RadioButton "BuyRadio". From what I know a radiobutton can have two states, either true or false, wich meen pressed or not pressed.

What I am trying to do is this code wich meens that if the RadioButton is pressed this will evaluate but this does not compile. It does compile if I only write: if (BuyRadio).
Any idéas to this.

int SetNumber = 0;

if (BuyRadio = true)
{
     SetNumber = 1;
}

No it doesn´t. I am trying with this approch with a messagebox. If write this code below with only if (BuyRadio) and then run the program without pressing the BuyRadio Button, this messagebox will show anyway.
So it should only show if the radiobutton is pressed.

if (BuyRadio)
{
         MessageBox::Show("BuyRadio is pressed");
}

jennifer, does the program work when you use (BuyRadio)??

BuyRadio is a c++ class object, not a POD (Plain Old Data) type so you can't treat it like that. You must give the class a method that will return the button's state.

if( BuyRadio.GetState() == true)
{
   SetNumber = 1;

}

Thanks Acient Dragon. I did try all of these codelines and only the last one wich is
BuyRadio-> had members/library. The other 2, no libraries/member comes up to choose GetState(). I use VC++ 2008 .NET.

BuyRadio.       //No members
BuyRadio::     //No members
BuyRadio->    //This one has members but not GetState();

First, don't rely on the compiler's IDE to give you all the methods -- look them up in MSDN so that you can read their description. RadioButton class is here. Look at the members and you will find IsChecked method.

if( BuyRadio->IsChecked() == true)
{
   SetNumber = 1;

}

If understand you right, I will find "IsChecked" as a member like this. Strangely I cant choose that either. It is not in the list.
So I cant choose either GetType() or IsChecked()

BuyRadio->GetType()
BuyRadio->IsChecked()

Forget about the two I mentioned above. You need to handle the Click event. You need to have your own bool variable to indicate whether the button has been clicked or not, then implement an onClick event handler to set or reset that variable. Something like below. IN the below, IsClicked is a class bool variable. Now wherever you want just look at the IsClicked varialbe.

private: System::Void BuyRadio_Click(System::Object^  sender, System::EventArgs^  e) {
                 if( IsClicked )
                     IsClicked = false;
                 else
                     IsClicked = true;
             }

Sorry if I am asking a bit here but I am not completely sure how this will be done.
I do understand the whole logic I beleive.
I will create this onClick event handler. BuyRadio_Click.
What I then need is a class bool variable. I do understand the logic with this too. It is not a usual bool, it is a class bool so this variable can be reached outside the BuyRadio_Click.

(Wich then will be referd to if (IsClicked == true) etc...

I have never declared a class bool variable. I tried it like this inside BuyRadio_Click:
class bool IsClicked;
But suppose this is wrong.
How will I declare this class bool variable and where will this be declared.
Thanks.

private: 
System::Void BuyRadio_Click(System::Object^ sender, System::EventArgs^ e) 
{
	 if( IsClicked ) 
	IsClicked = false; 
	else 
 	IsClicked = true; 
}

The way I did it was like this: (see line 10 and 28)

public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
                           this->IsClicked = false;
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
    private: System::Windows::Forms::Button^  BuyRadio;
    protected: 

	private:
        bool IsClicked;
<snip>

Thanks I managed to put
this->IsClicked = false; in the right place as you did and under this I declared a class bool variable like this and this did compile.

private: bool IsClicked;

So now when I have this statement below inside a button control and have clicked the BuyRadio on the form it should display a messageBox I beleive, but this is not happening. I have trid to press the button without pressing the radiobutton also.

Though if I change to this: this->IsClicked = true; with the code below then the MessageBox shows without pressing any radiobutton.

if ( IsClicked  == true )
{
	MessageBox::Show("RadioButton is pressed");
}

>> I have trid to press the button without pressing the radiobutton also.
what does that mean?

At this point I think you need to post the code for the form that contains the radio button.

I ment that as my if statement said this (true) perheps it could work if the radiobutton wasn´t pressed if something was the opposite way.

if (IsClicked == true)
{ 
       MessageBox::Show("RadioButton Pressed")
}

Okay so this is my code for the form.

#pragma once


namespace Form2 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//

			this->IsClicked = false;

		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::RadioButton^  BuyRadio;
	protected: 

	private: bool IsClicked;
	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->BuyRadio = (gcnew System::Windows::Forms::RadioButton());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(376, 261);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 0;
			this->button1->Text = L"button1";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// BuyRadio
			// 
			this->BuyRadio->AutoSize = true;
			this->BuyRadio->Location = System::Drawing::Point(244, 126);
			this->BuyRadio->Name = L"BuyRadio";
			this->BuyRadio->Size = System::Drawing::Size(71, 17);
			this->BuyRadio->TabIndex = 1;
			this->BuyRadio->TabStop = true;
			this->BuyRadio->Text = L"BuyRadio";
			this->BuyRadio->UseVisualStyleBackColor = true;
			this->BuyRadio->CheckedChanged += gcnew System::EventHandler(this, &Form1::radioButton1_CheckedChanged);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(782, 467);
			this->Controls->Add(this->BuyRadio);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
			 }
	private: System::Void radioButton1_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
			 }
	private: System::Void radioButton1_Click(System::Object^  sender, System::EventArgs^  e)
		 {

			    if ( IsClicked )
                     IsClicked = true;
               
				else
                     IsClicked = false;
    
			 }
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
			 
			 if ( IsClicked  == true )
		{
			MessageBox::Show("RadioButton Pressed");
			
		}

			 
			 }
	};
}

>> I have trid to press the button without pressing the radiobutton also.
what does that mean?

At this point I think you need to post the code for the form that contains the radio button.

lines 113-117 are wrong, you have them backwards. See the lines I posted. I think you also have them in the wrong event handler. Should be

private: System::Void BuyRadio_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
			    if ( IsClicked )
                     IsClicked = false;
               
				else
                     IsClicked = true;
             }

This was really difficult. I did change to this but still it is not working. I have tried this code within radiobutton1_CheckedChanged, BuyRadio_CheckedChanged and it is not working in any of the event handlers.

private: System::Void BuyRadio_CheckedChanged(System::Object^  sender, System::EventArgs^  e)
{

    if ( IsClicked )
    IsClicked = false;
               
     else
     IsClicked = true;
    
}

I must have doing things to fast. I tried it like this and this worked.
I have to say thank you very much for time and help I got with this.
Thank You...

private: System::Void radioButton1_CheckedChanged(System::Object^  sender, System::EventArgs^  e) 
{
			 
	 if ( IsClicked )
                 IsClicked = false;
               
	 else
                 IsClicked = true;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.