Forum guidelines
Areeb Quazi
How do I find Last active control
Posted By Areeb Quazi on 25 Nov 2012

I have multiple text boxes and buttons.
On print button I am saving form as .xps  using Me.PrintForm1.Print and I need to unselected all text boxes before this
and then again select same text box after save. Please suggest how can i do this functionality.

I tried with this.Activecontrol but always getting clicked button(i.e. print button). how I'll get last active control or is there any alternative.


  • 0
  • 0
Re: How do I find Last active control
Posted By Vulpes on 25 Nov 2012   Accepted Answer
f you mean that you want to restore the input focus to whatever textbox had the input focus before the print button was pressed, then one thing you could do would be handle the Leave event of all textboxes (using the same handler for them all) and save a reference to the last one which had the focus in a private field on your form:

private TextBox lastTB = null;

private void textBox_Leave(object sender, System.EventArgs e)
   lastTB = (TextBox)sender;

// after saving form, restore focus to last TB which had input focus

if(lastTB != null) lastTB.Focus();

C# Corner Annual Conference 2016


Spire.Doc - Free .NET Word API
Use Spire.Doc to create, read, write, print, and convert word documents to OpenXML, RTF, TXT, XPS, EPUB, EMF, HTML, Image and PDF.