Detecting Form Has Got Focus

Use the ‘Activated’ event

Bring Form To Front


this->BringToFront();

Does A Specific Control Have Focus?


if (txtMyTextBox->ContainsFocus)

Give an object on the form focus


	//Use the objects focus property:
	ObjectName->Focus();
	//or give the form itself focus:
	frmMyForm->Focus();

Forcing a form to always be on top

Set the forms ‘Top Most’ property to true. This will keep the form above all forms including forms in other applicaitons, unless they have got focus and also have their top most property set.

Stopping Specific Form Objects From Being Selected By A User (e.g. text boxes being used as multiline labels)

Add this function


	//**************************************************
	//**************************************************
	//********** REMOVE FOCUS FROM TEXT BOXES **********
	//**************************************************
	//**************************************************
	private: System::Void ControlRemoveFocus(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)
	{
		 this->btnDone->Focus();		//Some object on the form that we'll give focus to instead
	}

Then or each control you don’t want to be selecable set its MouseDown event to be ControlRemoveFocus, which looks like this in code:


	this->txtMyTextBox->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &frmCellSetup::ControlRemoveFocus);
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.