Setting Control Focus

Z

zacks

I am working on an application that has a UI that supports multiuple
functions by means of a group of "plug ins". Each plugin is a class
library. Each plugin contains a User Control that defines the controls
for that plugin's main form. The UI has a PictureBox control that is
nothing but a contain to add the UserControl from the selected plugin,
so that when the user selects a plugin, the main form changes
appearance to the UserControl in the selected plugin. When the UI
first launches, it loads the plugin from the last time the app was
shut down.

My problem is this. Whether the application first launches or the user
selects a different plugin, I cannot get the first control in the
UserControl to have focus. Each plugin's UserControl has a special
Initialize method that the plugin inovkes when it is loaded to set
some initial values. In one plugin, it populates a ComboBox. I have a
statement in the UserControl's Initialize method to set the focus to
the first control in the UserControl, but it doesn't seem to work. The
first control in each plugin's UserControl never gets focus
automatically, I have to click in the control for it to get focus.

Any idea how I can achieve this? I have tried to invoke the first
control's Select method instead of the Focus method and that doesn't
help.
 
I

Ignacio Machin ( .NET/ C# MVP )

I am working on an application that has a UI that supports multiuple
functions by means of a group of "plug ins". Each plugin is a class
library. Each plugin contains a User Control that defines the controls
for that plugin's main form. The UI has a PictureBox control that is
nothing but a contain to add the UserControl from the selected plugin,
so that when the user selects a plugin, the main form changes
appearance to the UserControl in the selected plugin. When the UI
first launches, it loads the plugin from the last time the app was
shut down.

My problem is this. Whether the application first launches or the user
selects a different plugin, I cannot get the first control in the
UserControl to have focus. Each plugin's UserControl has a special
Initialize method that the plugin inovkes when it is loaded to set
some initial values. In one plugin, it populates a ComboBox. I have a
statement in the UserControl's Initialize method to set the focus to
the first control in the UserControl, but it doesn't seem to work. The
first control in each plugin's UserControl never gets focus
automatically, I have to click in the control for it to get focus.

Any idea how I can achieve this? I have tried to invoke the first
control's Select method instead of the Focus method and that doesn't
help.

what if the plugin interface you define which control should have teh
focus?
in this case the container is responsible to give focus when needs to
(like in onLoad)
 
Z

zacks

[...]
My problem is this. Whether the application first launches or the user
selects a different plugin, I cannot get the first control in the
UserControl to have focus. [...]
Any idea how I can achieve this? I have tried to invoke the first
control's Select method instead of the Focus method and that doesn't
help.

A concise-but-complete code sample would go a long way.

That said, the first thing I'd suggest is to stop abusing the PictureBox  
class.  It's not a container control.  Use something more appropriate,  
like Panel or even UserControl (both of which are intended for use as  
containers).

It's possible (I didn't look into it closely myself) that the PictureBox  
class just doesn't accept focus.  That may have implications for  
attempting to set focus to it or its children.

Thanks for the suggestions.

After plugging away on thisd I figured out what was missing. After I
add the plugin's UserControl to the PictureBox's controls, I then
invoke the Select method for PictureBox.Controls[0].

And as far as using a PictureBox control as a container, I did not do
the design, but I will pass along your comments to the person who did.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top