Hi, I'm trying to loop through multiple textboxes in a C++ form using visual studio.net 2008 to set the selection start for each. The code for a single textbox is easy:

textBox1->SelectionStart = 4;

When I try to assign textBox1 to a Control variable, and assign selection start, I get an error:

Control^ myControl;
myControl = textBox1;
myControl->SelectionStart = 4;

The error says:
error c2039: 'SelectionStart' : is not a member of 'System::Windows::Forms::Control'

I have no trouble assigning text this way:

Control^ myControl;
myControl = textBox1;
myControl->Text = "hello";

Does anyone know how I can do this?

SelectionStart is a property specific to TextBox. Text is a property that all Controls have. To access that property you need to downcast the controls to their particular type using a dynamic_cast to TextBox^ type. You'll have to do some checking to make sure that you have the right controls on your form, perhaps by tagging your textboxes and using that as a criterion to cast them.

Here's an example that gives you something tangible when you click the button. Set the "Tag" property of each of the textboxes to "Text" (or some other moniker).

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
	for(int i = 0;i<Controls->Count;i++)
		if(Controls[i]->Tag=="Text")	 
		{
	  	  dynamic_cast<TextBox^>(Controls[i])->Multiline = true;
		   dynamic_cast<TextBox^>(Controls[i])->Height = 5;
		}
}
commented: Wow, your post really saved me time and frustration! :) +0

Here's an example that gives you something tangible when you click the button. Set the "Tag" property of each of the textboxes to "Text" (or some other moniker).

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
	for(int i = 0;i<Controls->Count;i++)
		if(Controls[i]->Tag=="Text")	 
		{
	  	  dynamic_cast<TextBox^>(Controls[i])->Multiline = true;
		   dynamic_cast<TextBox^>(Controls[i])->Height = 5;
		}
}

Wow, this works perfectly, Thanks!
Tony