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

Recommended Answers

All 2 Replies

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

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.

commented: 5 YEARS LATE, and still the code is horribly broken - now go read the manual for sprintf -3
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.