How can I give a (display) value to a textbox in form?

Recommended Answers

All 40 Replies

It depends.
If this is on Windows (and dot net), you can reach the ->Text property of your textbox.
like: Form1->TextBox1->Text = "hey";

Thanks for the quick answering! Ma next question is?
How can I give a numeric value to a textbox?

Convert the numeric value using the .toString() method.
TextBox1-> Text += iNum.toString();

Thanks, but I also can't find the tonum syntax!

ToNum?

No

ToString();

I would like to convert a textbox to a numeric value.

double.Parse(textBox1->Text);
int.Parse(textBox1->Text);
long.Parse(textBox1->Text);

double NA;
NA=double.Parse(this->A->Text);

error C2062: type 'double' unexpected

Thank you for your help. I have successed.
My next question is: Where I have to put user defined functions in a form sulution?
For example I want to define a round() function, but I can't.

It depends.
If the functions specifically have to do with the form, you can put them in the .h file that is the name of the form and create a .cpp file.

You really can JUST bury them in the .h file (just for a test).
If you double-click on the form, Visual studio will take you to the .h file for that form.

If they will be separate classes with particular functions, you should create a new class, put the functions in the class and #include the header file in the module that needs to call the methods in that class.

1. I don't want to give a disabled textbox to give grey color. What can I do?

2. I have put on exit button on my form. What code must contain it?

1) You can make it read-only and change the background color to white (or what ever color it is normally).
2) Application::Exit();

Thanks a lot! I have up-voted your answers. However there are some questions remaind:
1. How do I prevent cursor to go a readonly textbox?
2. Is it passible to use Enter insted of TAB?

1. I would like to put cursor back to the first input textbox.

textBox1->Focus();

Where can I find a description/help abou text1->"methods"?

I have Visual studio 2010 express edition, so I think I have no intellisense.

I have found a usefuul code in help, see below, but I don't know how to implement it to a textbox. Where to put the code, and how, etc? Please help me!

// Boolean flag used to determine when a character other than a number is entered.
private:
   bool nonNumberEntered;

   // Handle the KeyDown event to determine the type of character entered into the control.
   void textBox1_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e )
   {
      // Initialize the flag to false.
      nonNumberEntered = false;

      // Determine whether the keystroke is a number from the top of the keyboard.
      if ( e->KeyCode < Keys::D0 || e->KeyCode > Keys::D9 )
      {
         // Determine whether the keystroke is a number from the keypad.
         if ( e->KeyCode < Keys::NumPad0 || e->KeyCode > Keys::NumPad9 )
         {
            // Determine whether the keystroke is a backspace.
            if ( e->KeyCode != Keys::Back )
            {
               // A non-numerical keystroke was pressed.
               // Set the flag to true and evaluate in KeyPress event.
               nonNumberEntered = true;
            }
         }
      }
      //If shift key was pressed, it's not a number.
      if (Control::ModifierKeys == Keys::Shift) {
         nonNumberEntered = true;
      }
   }

   // This event occurs after the KeyDown event and can be used to prevent
   // characters from entering the control.
   void textBox1_KeyPress( Object^ /*sender*/, System::Windows::Forms::KeyPressEventArgs^ e )
   {
      // Check for the flag being set in the KeyDown event.
      if ( nonNumberEntered == true )
      {         // Stop the character from being entered into the control since it is non-numerical.
         e->Handled = true;
      }
   }

You won't be able to just use this code unless you create the event handlers for the key presses. Are you making a calculator or something?

Yust resolving an equation of the second degree.

Have you got an email address to send the whole project there?
My address is [snipped] .

My email address have blocked by moderator. I don't know how to continue our conversation.

Here you are: the code of form.h

#include <math.h>
#include "masodfokuform.h"
#pragma once

namespace masodfokuform {

	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
	/// </summary>
	double NA,NB;

	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::TextBox^  A;
	private: System::Windows::Forms::TextBox^  B;
	private: System::Windows::Forms::TextBox^  C;
	private: System::Windows::Forms::TextBox^  X1;
	private: System::Windows::Forms::TextBox^  X2;
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::TextBox^  Diszkriminans;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::Label^  label5;
	private: System::Windows::Forms::Label^  label6;
	private: System::Windows::Forms::Button^  button2;
	protected: 

	protected: 





	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)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
			this->A = (gcnew System::Windows::Forms::TextBox());
			this->B = (gcnew System::Windows::Forms::TextBox());
			this->C = (gcnew System::Windows::Forms::TextBox());
			this->X1 = (gcnew System::Windows::Forms::TextBox());
			this->X2 = (gcnew System::Windows::Forms::TextBox());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->Diszkriminans = (gcnew System::Windows::Forms::TextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->label6 = (gcnew System::Windows::Forms::Label());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// A
			// 
			this->A->Location = System::Drawing::Point(37, 12);
			this->A->Name = L"A";
			this->A->Size = System::Drawing::Size(100, 20);
			this->A->TabIndex = 0;
			this->A->TextChanged += gcnew System::EventHandler(this, &Form1::A_TextChanged);
			// 
			// B
			// 
			this->B->Location = System::Drawing::Point(37, 38);
			this->B->Name = L"B";
			this->B->Size = System::Drawing::Size(100, 20);
			this->B->TabIndex = 1;
			// 
			// C
			// 
			this->C->Location = System::Drawing::Point(37, 64);
			this->C->Name = L"C";
			this->C->Size = System::Drawing::Size(100, 20);
			this->C->TabIndex = 2;
			// 
			// X1
			// 
			this->X1->BackColor = System::Drawing::SystemColors::Info;
			this->X1->ForeColor = System::Drawing::Color::DarkRed;
			this->X1->Location = System::Drawing::Point(155, 141);
			this->X1->Name = L"X1";
			this->X1->ReadOnly = true;
			this->X1->Size = System::Drawing::Size(100, 20);
			this->X1->TabIndex = 6;
			this->X1->TextChanged += gcnew System::EventHandler(this, &Form1::X1_TextChanged);
			// 
			// X2
			// 
			this->X2->BackColor = System::Drawing::SystemColors::Info;
			this->X2->ForeColor = System::Drawing::Color::DarkRed;
			this->X2->Location = System::Drawing::Point(155, 167);
			this->X2->Name = L"X2";
			this->X2->ReadOnly = true;
			this->X2->Size = System::Drawing::Size(100, 20);
			this->X2->TabIndex = 7;
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(180, 67);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 3;
			this->button1->Text = L"Számítás";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// Diszkriminans
			// 
			this->Diszkriminans->BackColor = System::Drawing::SystemColors::Info;
			this->Diszkriminans->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(238)));
			this->Diszkriminans->ForeColor = System::Drawing::Color::DarkRed;
			this->Diszkriminans->Location = System::Drawing::Point(155, 109);
			this->Diszkriminans->Name = L"Diszkriminans";
			this->Diszkriminans->ReadOnly = true;
			this->Diszkriminans->Size = System::Drawing::Size(100, 20);
			this->Diszkriminans->TabIndex = 5;
			this->Diszkriminans->TextChanged += gcnew System::EventHandler(this, &Form1::Diszkriminans_TextChanged);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(15, 15);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(20, 13);
			this->label1->TabIndex = 7;
			this->label1->Text = L"A=";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(14, 41);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(20, 13);
			this->label2->TabIndex = 8;
			this->label2->Text = L"B=";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(14, 67);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(20, 13);
			this->label3->TabIndex = 9;
			this->label3->Text = L"C=";
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(72, 112);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(80, 13);
			this->label4->TabIndex = 10;
			this->label4->Text = L"Diszkrimináns =";
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Location = System::Drawing::Point(126, 144);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(26, 13);
			this->label5->TabIndex = 11;
			this->label5->Text = L"X1=";
			// 
			// label6
			// 
			this->label6->AutoSize = true;
			this->label6->Location = System::Drawing::Point(127, 170);
			this->label6->Name = L"label6";
			this->label6->Size = System::Drawing::Size(26, 13);
			this->label6->TabIndex = 12;
			this->label6->Text = L"X2=";
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(180, 219);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(75, 23);
			this->button2->TabIndex = 4;
			this->button2->Text = L"Kilépés";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"$this.BackgroundImage")));
			this->ClientSize = System::Drawing::Size(292, 266);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->label6);
			this->Controls->Add(this->label5);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->Diszkriminans);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->X2);
			this->Controls->Add(this->X1);
			this->Controls->Add(this->C);
			this->Controls->Add(this->B);
			this->Controls->Add(this->A);
			this->Name = L"Form1";
			this->Text = L"Másodfokú egyenlet megoldása";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void textBox2_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			 }
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
	
				double NA,NB,NC,Diszkr,Gyok1,Gyok2;
				NA = double::Parse(this->A->Text);
				NB = double::Parse(this->B->Text);
				NC = double::Parse(this->C->Text);
				Diszkr=(NB*NB)-4*NA*NC;
				Diszkriminans->Text=Diszkr.ToString();
				if(Diszkr>0)
				{
				  if(NA==0)
				  {
				    Gyok1=-NC;
					X1-> Text = Gyok1.ToString(); 
					X2-> Text = ""; 
				  }
				  else
				  {
				    Gyok1=round((-NB+sqrtl(Diszkr))/(2*NA),6);
				    Gyok2=round((-NB-sqrtl(Diszkr))/(2*NA),6);
			        X1-> Text = Gyok1.ToString(); 
			        X2-> Text = Gyok2.ToString(); 
				  }
				}
				else
				{
			      X1-> Text = ""; 
			      X2-> Text = ""; 
				}
				A->Focus();
			 }
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
		 {
			Application::Exit(); 		
		 }
private: System::Void X1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void Diszkriminans_TextChanged(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void A_TextChanged(System::Object^  sender, System::EventArgs^  e) {
		 }
};
}

Well, do you have a question?

I have found a usefuul code in help, see below, but I don't know how to implement it to a textbox. Where to put the code, and how, etc? Please help me!
C++ Syntax (Toggle Plain Text)

// Boolean flag used to determine when a character other than a number is entered.
    private:
    bool nonNumberEntered;
     
    // Handle the KeyDown event to determine the type of character entered into the control.
    void textBox1_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e )
    {
    // Initialize the flag to false.
    nonNumberEntered = false;
     
    // Determine whether the keystroke is a number from the top of the keyboard.
    if ( e->KeyCode < Keys::D0 || e->KeyCode > Keys::D9 )
    {
    // Determine whether the keystroke is a number from the keypad.
    if ( e->KeyCode < Keys::NumPad0 || e->KeyCode > Keys::NumPad9 )
    {
    // Determine whether the keystroke is a backspace.
    if ( e->KeyCode != Keys::Back )
    {
    // A non-numerical keystroke was pressed.
    // Set the flag to true and evaluate in KeyPress event.
    nonNumberEntered = true;
    }
    }
    }
    //If shift key was pressed, it's not a number.
    if (Control::ModifierKeys == Keys::Shift) {
    nonNumberEntered = true;
    }
    }
     
    // This event occurs after the KeyDown event and can be used to prevent
    // characters from entering the control.
    void textBox1_KeyPress( Object^ /*sender*/, System::Windows::Forms::KeyPressEventArgs^ e )
    {
    // Check for the flag being set in the KeyDown event.
    if ( nonNumberEntered == true )
    { // Stop the character from being entered into the control since it is non-numerical.
    e->Handled = true;
    }
    }

First make sure you have a control named textBox1 (even if just for a test).

In your Form header file
In the location where the other event handlers and properties are set,
add the command:

this->textBox1->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::textBox1_KeyDown);

After that, you can paste that new code inside the same header.

The code is set to work on textBox1, so you might need to adjust the name.

After you get it to compile, you can modify it to make it actually do something.

What is : Object^ /*sender*/?

In spite of reading help I cant find the right way for programing next line:
void textBox1_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e )
After all I don't understand where to put it in my code?
Please help!
You wrote: "After that, you can paste that new code inside the same header."
Which header do you mean, and which point I should insert it?
Thanks' a lot.

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.