Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. C# UserControl Focus

C# UserControl Focus

Scheduled Pinned Locked Moved C#
helpquestioncsharp
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    ZawMinTun
    wrote on last edited by
    #1

    Hi all, Please help me or share me any information regarding to the issue below. I have a UserControl, e.g. with a number of text boxes on it. That UserControl is created in run-time as needed and can be overlapped each other. If I want to bring that UserControl to front, I need to click on one particular textbox. That particular textbox has a mouse-down event handler, which will bring the whole UserControl to front. The other textboxes don't have this mouse-down handler. So if I click on those textboxes, the UserControl will still stay behind. So if I want to bring the UserControl to the front-most when the user clicks on anywhere on it or any control on it, how can I make it done? Is there a way without implementing mouse-down event for every control? Thanks & best regards, Zaw Min Tun

    B 1 Reply Last reply
    0
    • Z ZawMinTun

      Hi all, Please help me or share me any information regarding to the issue below. I have a UserControl, e.g. with a number of text boxes on it. That UserControl is created in run-time as needed and can be overlapped each other. If I want to bring that UserControl to front, I need to click on one particular textbox. That particular textbox has a mouse-down event handler, which will bring the whole UserControl to front. The other textboxes don't have this mouse-down handler. So if I click on those textboxes, the UserControl will still stay behind. So if I want to bring the UserControl to the front-most when the user clicks on anywhere on it or any control on it, how can I make it done? Is there a way without implementing mouse-down event for every control? Thanks & best regards, Zaw Min Tun

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #2

      Assuming this is WinForms, and that you create multiple UserControls of the same Type at run-time: 1. obviously, if UserControl1 is completely covered (behind) UserControl2, or a bunch of other UserControls, you have no opportunity to click on anything in UserControl1: if that case ever exists: then you might want to do something like make a context-click (right-click for most folks) on any of your UserControls to send them to the back ? Or have a menu-item in the Main Form menu, or a context menu on every UserControl, that lets you select any one of the available UserControls, which it will then bring to the front ? 2. so, let's assume your UserControl1 is partially covered: let's assume there's going to be some area of UserControl1 visible and click-able: then assign a Click EventHandler in the UserControl definition code that brings it to the front; or if it already has a Click EventHandler, add a this.BringToFront(); line to it. 3. if we assume it could be partially covered, but what's visible is only TextBoxes, or some other object that completely fills the UserControl: then you don't have much choice but to enumerate all the Controls in the UserControl that could cause this condition, and assign a Click EventHandler that moves them to the front, or modify their existing MouseDown or Click EventHandlers as suggested in #2 above. Do keep in mind that delegates/events are "multi-cast" and can have more than one EventHandler assigned to their "Invocation Lists." So if you had a "Click" handler for some object that you did not want to (or could not ?) modify, you can add an extra Click EventHandler to that object (assuming the entire object is not somehow "sealed," "locked," whatever). So this is quite legal:

      // at some point in your initialization code:
      panel1.Click += new EventHandler(panel1_Click1);
      panel1.Click += new EventHandler(panel1_Click2);
      //
      private void panel1_Click1(object sender, EventArgs e)
      {
      Console.WriteLine("hello");
      }

      private void panel1_Click2(object sender, EventArgs e)
      {
      Console.WriteLine("goodbye");
      }

      Discussion: Both those Click EventHandlers will be executed on a Click on the UserControl. If you are using C# 2.0 or later, you can add the EventHandler directly without using 'new EventHandler' syntax:

      panel1.Click += panel1_Click1;
      panel1.Click += panel1_Click2;

      However, most folks like the auto-completion feature of Visual Studio that will generate the EventHandler stub for you ... so ... up to you.

      Z 1 Reply Last reply
      0
      • B BillWoodruff

        Assuming this is WinForms, and that you create multiple UserControls of the same Type at run-time: 1. obviously, if UserControl1 is completely covered (behind) UserControl2, or a bunch of other UserControls, you have no opportunity to click on anything in UserControl1: if that case ever exists: then you might want to do something like make a context-click (right-click for most folks) on any of your UserControls to send them to the back ? Or have a menu-item in the Main Form menu, or a context menu on every UserControl, that lets you select any one of the available UserControls, which it will then bring to the front ? 2. so, let's assume your UserControl1 is partially covered: let's assume there's going to be some area of UserControl1 visible and click-able: then assign a Click EventHandler in the UserControl definition code that brings it to the front; or if it already has a Click EventHandler, add a this.BringToFront(); line to it. 3. if we assume it could be partially covered, but what's visible is only TextBoxes, or some other object that completely fills the UserControl: then you don't have much choice but to enumerate all the Controls in the UserControl that could cause this condition, and assign a Click EventHandler that moves them to the front, or modify their existing MouseDown or Click EventHandlers as suggested in #2 above. Do keep in mind that delegates/events are "multi-cast" and can have more than one EventHandler assigned to their "Invocation Lists." So if you had a "Click" handler for some object that you did not want to (or could not ?) modify, you can add an extra Click EventHandler to that object (assuming the entire object is not somehow "sealed," "locked," whatever). So this is quite legal:

        // at some point in your initialization code:
        panel1.Click += new EventHandler(panel1_Click1);
        panel1.Click += new EventHandler(panel1_Click2);
        //
        private void panel1_Click1(object sender, EventArgs e)
        {
        Console.WriteLine("hello");
        }

        private void panel1_Click2(object sender, EventArgs e)
        {
        Console.WriteLine("goodbye");
        }

        Discussion: Both those Click EventHandlers will be executed on a Click on the UserControl. If you are using C# 2.0 or later, you can add the EventHandler directly without using 'new EventHandler' syntax:

        panel1.Click += panel1_Click1;
        panel1.Click += panel1_Click2;

        However, most folks like the auto-completion feature of Visual Studio that will generate the EventHandler stub for you ... so ... up to you.

        Z Offline
        Z Offline
        ZawMinTun
        wrote on last edited by
        #3

        Thanks a lot, Bill. As you said, I have no choice but to add click event handler to every control on that user control. Best regards, Zaw Min Tun

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups