Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8617933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:59:18+00:00 2026-06-12T05:59:18+00:00

I have set up UserControls in a FlowPanelLayout with the help in this question:

  • 0

I have set up UserControls in a FlowPanelLayout with the help in this question:
For Each DataTable Add UserControl to FlowLayoutPanel

I am now trying to implement a click event which allows me to put a border around the UserControl that has been selected. I have done this:

        private void User_Load(object sender, EventArgs e)
    {
        flowlayoutpanelUsers.HorizontalScroll.Visible = false;

        // Load and Sort Users DataTable
        DataTable datatableUsers = UserMethods.GetUsers().Tables["Users"];
        datatableUsers.DefaultView.Sort = "Name";
        DataView dataviewUsers = datatableUsers.DefaultView;

        // Loop Through Rows and Add UsersGrid to FlowLayoutPael
        foreach (DataRowView datarowviewUsers in dataviewUsers)
        {
            var UsersGrid = new UsersGrid
            {
                Username = datarowviewUsers["Username"].ToString(),
                User = datarowviewUsers["Name"].ToString(),
                Admin = datarowviewUsers["Administrator"].ToString(),
            };
            flowlayoutpanelUsers.Controls.Add(UsersGrid);
            UsersGrid.MouseClick += new MouseEventHandler(user_click);
        }
    }

    private UsersGrid selectedUser;

    void user_click(object sender, EventArgs e)
    {
        if (selectedUser != null)
            selectedUser.BorderStyle = BorderStyle.None;
        selectedUser = (UsersGrid)sender;
        selectedUser.BorderStyle = BorderStyle.FixedSingle;
    }

My issue is that it only works when I click in a white space in the UserControl but not when the user clicks on the two labels or image. How do I make it work for all child objects too?

Also, how can I then use the selected UserControl to do other things like open a form which shows all the details for that selected user?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:59:19+00:00Added an answer on June 12, 2026 at 5:59 am

    I have a few suggestions for you. At the bottom of my response I included code that demonstrates my suggestions.

    Suggestion 1: Fixing MouseClick in your UC
    When you register the MouseClick event for a UserControl (UC) you’re doing so for the UserControl itself, not for any controls that you place on the UserControl such as your Labels, etc. If you click one of these child controls the click won’t be ‘seen’ by the underlying UC.

    To fix this register the MouseClick event for all your child controls; you can even register the same MouseClick event handler you have for the UserControl itself.

    Suggestion 2: Setting your UC’s BorderStyle
    I’d move your code for setting the UC’s BorderStyle into the UC itself. Create public property IsSelected that’s set to true when the UC is selected. In the property’s setter update the UC’s BorderStyle property depending on the value of the property.

    Exposing an IsSelected property for your UC can be handy: you can query a group of these UCs to see which ones are selected rather than trying to track this status outside of the control like through a Form-level variable.

    Edit in response to your comment:
    Here’s an example of how you might query the UCs in a FlowLayoutPanel to see if any are selected and if one is found how you might take some action. In this case the action is to call an EditUser method that takes as parameters values you get from properties in the selected UC:

    var selectedUC = flowLayoutPanel.Controls.Cast<UserControl1>().FirstOrDefault(uc => uc.IsSelected);
    if (selectedUC != null) {
        // Use the properties of the UC found to be selected as parameters is method EditUser.
        EditUser(selectedUC.Name, selectedUC.Username, selectedUC.Administrator);
    }
    

    Suggestion 3: Managing selection in a group of your UCs
    If you want to unselect all UCs in a group except for the one that the user clicks (i.e. selects) you’ll need to create an event in your UC that fires when a UC is clicked. The handler for this event explicitly sets IsSelected to false for all UCs in a set (such as in a container type control such as a Form, FlowLayoutPanel, etc.), the MouseClick handler in the UC that was clicked will then set the clicked UC’s IsSelected to true.

    It’s worth considering creating another UserControl type that manages a group of your UCs. This new UserControl can encapsulate the code for the creation and state management of sets of your UC and would faciliate using your UCs in other projects as well as keeping the code of Forms hosting your UCs a bit cleaner.


    I figured that rather than include a series of disjointed code snippets for each of my suggestions I’d include what I’m hoping is the minimum amount of code to allow you to reproduce what I’m talking about.

    Create a new Visual Studio Winform project and use the following for class Form1:

    public partial class Form1 : Form
    {
        public Form1() {
            InitializeComponent();
    
            flowLayoutPanel = new FlowLayoutPanel {
                Dock = DockStyle.Fill,
            };
            this.Controls.Add(flowLayoutPanel);
            // Add several sample UCs.
            for (int i = 0; i < 10; i++) {
                var uc = new UserControl1();
                uc.WasClicked += UsersGrid_WasClicked;
                flowLayoutPanel.Controls.Add(uc);
            }
        }
    
        FlowLayoutPanel flowLayoutPanel;
    
        // Event handler for when MouseClick is raised in a UserControl.
        void UsersGrid_WasClicked(object sender, EventArgs e) {
            // Set IsSelected for all UCs in the FlowLayoutPanel to false. 
            foreach (Control c in flowLayoutPanel.Controls) {
                if (c is UserControl1) {
                    ((UserControl1)c).IsSelected = false;
                }
            }
        }
    }
    

    Next add a UserControl to the project. Keep the name UserControl1 and add a couple Labels and a PictureBox. Use this code for class UserControl1:

    public partial class UserControl1 : UserControl
    {
        public UserControl1() {
            InitializeComponent();
            this.Load += UsersGrid_Load;
        }
    
        // Event fires when the MouseClick event fires for the UC or any of its child controls.
        public event EventHandler<EventArgs> WasClicked;
    
        private void UsersGrid_Load(object sender, EventArgs e) {
            // Register the MouseClick event with the UC's surface.
            this.MouseClick += Control_MouseClick;
            // Register MouseClick with all child controls.
            foreach (Control control in Controls) {
                control.MouseClick += Control_MouseClick;
            }
        }
    
        private void Control_MouseClick(object sender, MouseEventArgs e) {
            var wasClicked = WasClicked;
            if (wasClicked != null) {
                WasClicked(this, EventArgs.Empty);
            }
             // Select this UC on click.
             IsSelected = true;
        }
    
        private bool _isSelected;
        public bool IsSelected {
            get { return _isSelected; }
            set {
                _isSelected = value;
                this.BorderStyle = IsSelected ? BorderStyle.Fixed3D : BorderStyle.None;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of aspx pages which are made of up usercontrols. Each
I wanna implement an interface(Add behavior) for set of WPF usercontrols. I'm using MVVM
I have this XAML: <UserControl x:Class=Foo.UserControls.Bar x:Name=FooBar xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml> <StackPanel> <WrapPanel Margin=4,0,0,0> <Button Command={Binding
Say I have a number of usercontrols, each usercontrol inside a tabitem, inside a
I have a general question. My page has several quiet 'heavy' UserControls, each of
I have UserControls containing other controls. I want that if I set a Foreground
Suppose I have a UserControl whose DataContext is set to an object that has
I have a UserControl SubjectListView. The binding is set to AllSubjects which is an
I have set of flat files (114 files) each file is named with database
I have a UI with nested WPF UserControls each with their own view models.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.