User Control Click - Windows Forms - c#

I have a custom user control on my windows forms. This control has a few labels on it.
I will be dynamically displaying an array of these controls on my form which will contain different bits of data.
What I am trying to do is know which user control was selected when I click on it.
This works when I click on an empty space on the user control, however, if I click on any label on the user control it will not recognize the user control click.
Any thoughts on how I can do a full user control click, even if a label on the control is being clicked?
If this question is not clear, or you need more info, please leave a comment.
I am doing this in c#.
Thanks!

User control's click event won't fire when another control is clicked on the user control. You need to manually bind each element's click event. You can do this with a simple loop on the user control's codebehind:
foreach (Control control in Controls)
{
// I am assuming MyUserControl_Click handles the click event of the user control.
control.Click += MyUserControl_Click;
}
After this piece of code workd, MyUserControl_Click will fire when any control on the user control is clicked.

foreach (Control c in this.Controls)
{
c.Click += new EventHandler(SameAsForm_Click);
}
Keep in mind that this won't add labels' clickevents in groupboxes, panels etc to the "SameAsForm_Click"-EventHandler.

Related

add custom web user control in onclick event handler function

I have an asp.net project and I have a panel in it in which I want to add multiple user control on the panel by clicking on the button, It does this when clicking on the button
System.Web.UI.Control _msg = (System.Web.UI.Control)Page.LoadControl("msgsend.ascx");
pnl.control.add(_msg)
but it clears all the user controls on the panel before and adds only one user control. What should I do to avoid this?

C# winform: Accessing controls in a usercontrol from every tab page

I have a user control and in this control i have a bunch of text boxes and labels. Now I have linked this user control to another form's tab control. Here is the code I am using
TabPage tp = new TabPage();
tp.Controls.Add(TipUserControl);
tp.Text = "Tab "+ tabctrl_Fields.TabCount + 1;
tabctrl_Fields.TabPages.Add(tp);
When I click on a "Add another tab" button, the above code gets executed and a new tab page with the text boxes (similar to Tab 1) is created.
Now what I am looking for is When the user click on "Done" button in the form (not in the user control), it should be able to loop through every tab and every control (textboxes, labels etc) within that tab. Can anyone suggest on how to write this code?
Thanks in advance,
Swamy
I would add a Tag to the controls that you are searching for and use that approach:
Ability to find WinForm control via the Tag property
private void FindTag(Control.ControlCollection controls)
{
foreach (Control c in controls)
{
if (c.Tag != null)
//logic
if (c.HasChildren)
FindTag(c.Controls); //Recursively check all children controls as well; ie groupboxes or tabpages
}
}
Or iterate recursively over the Tab controls

How to refresh user control grid by using another user control event

I am developing windows application using C#. Need solution for below mentioned scenario. I have windows form containing two user control. First user control contains grid and when user click on any row of grid another user control display details of selected cell. when I modify any data from details control I need to refresh data grid in parent control.
I am using below code to load child control.
private void GridInquiry_CellClick(object sender, DataGridViewCellEventArgs e)
{
PanelParentControl.Controls.Clear();
InquiryDetailsCls.InquiryID = Convert.ToInt32(GridInquiry.SelectedRows[0].Cells[0].Value.ToString());
CtrlInqDetails inqDetails = new CtrlInqDetails(InquiryDetailsCls.InquiryID, 1);
inqDetails.Dock = DockStyle.Fill;
PanelParentControl.Controls.Add(inqDetails);
}
I can recommend to you to use the Notification Center for C#,With this you can make a global event, register for this event anywhere in you code and to fire this event from anywhere in your code.

Detect in Control.GotFocus whether user navigates forward or backward?

Having the following WinForms dialog form, I am handling the GotFocus event of MyControl:
MyControl derives from the DevExpress XtraUserControl which in turn derives from the Microsoft WinForms standard UserControl.
What I want to achieve is that when MyControl gets the focus when the user navigates with the Tab and MyControl gets the focus, that the focus is forwarded to the child controls.
I do this successfully with the following code:
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
// Forward.
foreach (Control control in Controls)
{
if (control.TabStop)
{
control.Select();
break;
}
}
}
I.e. if Button 1 is focused and the user presses the Tab key, the focus is set to Button 2.
What I'm not able to solve is if the user navigates backward. I.e. if Button 4 is focused and the user presses the Shift+Tab keys, the focus should be set to Button 3.
My two questions are:
Is there a way to detect the navigation order of the user inside the GotFocus event?
Am I doing it the right way at all? Maybe there is a built-in function/flag I can set to MyControl to automatically forward the focus to its child controls?
So many possibilities:
use the OnLostFocus event to store the current control and calculate whether TAB or SHIFT TAB was pressed
override ProcessKeyPreview to calc the action to be performed in OnGotFocus (SO answer)
override ProcessCmdKey as in this answer

How to bypass the enter/leave event in c sharp

I'm having a Picture box in a user control window(Windows custom control library). and some functionality in the Form's Enter event and leave event.
Now my sample application is having two instances of the control. So when i run my sample application the fist control got selected and the enter event is triggered, and when i select the second control the first's leave and second's enter events are getting triggered.
Now, problem is that when i select(click) the second control's picturebox, the events are not triggering, i.e the control form is not getting the event.
So if i click whereever in the control(in the picturebox or in the control) the enter event should be triggered.
How to do this?
A picture box can't get focus. So clicking on it won't take the focus away from the previous control thus not triggering the events.
You need to add a click handler on the picture box in which you manually give focus to the associated focusable control.
private void PictureBox_Click(object sender, EventArgs e)
{
focusableControl.Focus();
}

Categories