0

Am new to C++ and don't know if I'm doing something fundementally wrong. Have a form with a textbox and buttons. When I click on button, I am running a function in my .cpp file. However, in this function I need to update the text box in the form. Have created a public function set_address in the .h file form1 class definition that uses the following code.

public:
		void Form1::set_address(char *add)
		{
			char temp_str[80];
			sprintf(temp_str,add);
			textBox1->AppendText(temp_str);
		}

When I call it from my .cpp file with

Form1* pFm = new Form1;          
	pFm->set_address(temp_str);

It compiles OK and even runs the code, but the text box is not updated. If however I call the function from a clickbutton

private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
			 {
				char temp_str[80];
				sprintf(temp_str,"from button");
				set_address(temp_str);
			 }

It happily updates the text box.

Have been pulling my hair out for 2 days, so any help would be GREATLY appreciated

Using Microsoft Visual C++.net Version 2003

Thanks

Dave C

Edited by Nick Evan: n/a

3
Contributors
2
Replies
3
Views
12 Years
Discussion Span
Last Post by gpushee
0

Sounds like you need to invalidate the text box so it will display itself. Try calling textBox1->Invalidate().

-2

You need the button to do activate the code. The line set_address(temp_str); runs everything that's inside of set_address(char *add). Maybe instead of a button, try using the Form Load function.

public: void Form1::set_address(char *add)
{
char temp_str[80];
sprintf(temp_str,add);
textBox1->AppendText(temp_str);
}

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
set_address(temp_str);
}

Hope this helps any.

Edited by Nick Evan: Added code-tags

Votes + Comments
5 YEARS LATE, and still the code is horribly broken - now go read the manual for sprintf
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.