Setting Control Focus

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zacks@construction-imaging.com

    Setting Control Focus

    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.
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: Setting Control Focus

    On Aug 4, 3:55 pm, za...@construct ion-imaging.com wrote:
    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)

    Comment

    • Peter Duniho

      #3
      Re: Setting Control Focus

      On Mon, 04 Aug 2008 12:55:36 -0700, <zacks@construc tion-imaging.comwrot e:
      [...]
      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.

      Pete

      Comment

      • zacks@construction-imaging.com

        #4
        Re: Setting Control Focus

        On Aug 4, 4:23 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
        wrote:
        On Mon, 04 Aug 2008 12:55:36 -0700, <za...@construc tion-imaging.comwrot e:
        [...]
        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.Cont rols[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.

        Comment

        Working...