Windows Forms, New Form

How to make a button change the form's label

Page 1 of 1

2 Replies - 2269 Views - Last Post: 20 April 2010 - 06:44 AM

#1 AliceNWunderland   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 11-October 09

Windows Forms, New Form

Posted 20 April 2010 - 06:29 AM

Sorry for this VERY newbie question but I'm a PHP writer trying to learn C++ windows forms.

I can create a program with a button and a label, but how do I make pressing the "OK" button change the label or change to a new form?

Right now all I can do is make it exit the program.

#pragma once


namespace forms {

	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
			//
		}

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


	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->label1 = (gcnew System::Windows::Forms::Label());
			this->dateTimePicker1 = (gcnew System::Windows::Forms::DateTimePicker());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(36, 32);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(81, 13);
			this->label1->TabIndex = 0;
			this->label1->Text = L"Choose a Date:";
			// 
			// dateTimePicker1
			// 
			this->dateTimePicker1->Location = System::Drawing::Point(39, 70);
			this->dateTimePicker1->Name = L"dateTimePicker1";
			this->dateTimePicker1->Size = System::Drawing::Size(200, 20);
			this->dateTimePicker1->TabIndex = 1;
			this->dateTimePicker1->ValueChanged += gcnew System::EventHandler(this, &Form1::dateTimePicker1_ValueChanged);
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(106, 113);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 2;
			this->button1->Text = L"Ok";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click_1);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(284, 264);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->dateTimePicker1);
			this->Controls->Add(this->label1);
			this->Name = L"Form1";
			this->Text = L"Date Chooser";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			 }
	private: System::Void dateTimePicker1_ValueChanged(System::Object^  sender, System::EventArgs^  e) {label1->Text=String::Format("New date: {0}", dateTimePicker1->Text);
			 }
	private: System::Void button1_Click_1(System::Object^  sender, System::EventArgs^  e) {
				 Application::Exit();
			 }
	};
}




I have searched high and low on how to make a button do anything but exit the program and I cannot find an example of anything.

Is This A Good Question/Topic? 0
  • +

Replies To: Windows Forms, New Form

#2 muballitmitte   User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: Windows Forms, New Form

Posted 20 April 2010 - 06:36 AM

As GUI programming in .NET is event based you will have to handle the click event of the button object like so
	private: System::Void button1_Click_1(System::Object^  sender, System::EventArgs^  e) {
label1->Text = L"SOME NEW TEXT";
			 }



inside the button_Click function you set the text property of the label object to a new value.

This post has been edited by muballitmitte: 20 April 2010 - 06:40 AM

Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades   User is online

  • Saucy!
  • member icon

Reputation: 6248
  • View blog
  • Posts: 24,016
  • Joined: 23-August 08

Re: Windows Forms, New Form

Posted 20 April 2010 - 06:44 AM

Were I you, I would put my effort into learning C# for Windows Forms GUI development. Not a fan of the C++/CLI, and I think it's much easier to find resources to help you in C# than C++/CLI.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1