I made a user control and placed it in a panel. The user control has its own mouse click event that changes color. If I click on a control on the panel, I want the other controls to be deselected. Like radio buttons. How can I do that?
Here is a picture of my panel. There is both of two user controls is selected.
public partial class list : UserControl
{
void chgtxt(Label lbl, string s)
{
lbl.Text = s;
}
public list()
{
InitializeComponent();
}
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
panel1.BackColor = Color.Yellow;
chgtxt(label1, "Changed");
}
}
You can enumerate the panel's children and deselect the ones that aren't the current control:
foreach (list listControl in Parent.Controls.Cast<Control>().OfType<list>())
{
if (list != this)
{
list.Deselect();
}
}
Then you just have to create a Deselect()
method on your control:
public void Deselect()
{
// Do whatever to show this control as deselected.
}
To expand on itsme86's answer , you need to look at the parent control your control is part of and deselect the other items in the list.
public partial class list : UserControl
{
void chgtxt(Label lbl, string s)
{
lbl.Text = s;
}
public list()
{
InitializeComponent();
}
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
panel1.BackColor = Color.Yellow;
chgtxt(label1, "Changed");
if(this.Parent != null)
{
foreach (list listControl in this.Parent.Controls.Cast<Control>().OfType<list>())
{
if (listControl != this)
{
listControl.Deselect();
}
}
}
}
private void Deselect()
{
// Do whatever to show this control as deselected.
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.