I have a parent panel in a form.
This panel will display an array of different user control which will take up the full panel dimension.
I tried to use the panel 'Click' event. However when the user control is added to the panel, it does not fire the event when click.
Due to there are many widget in each user control, it would be tedious to implement 'Click' on each widget.
Is there anyway when i click on a user control, it fires the form panel event instead?
I suggest to loop the panel's controls on the form loading:
private void MyClick(object sender, EventArgs e) {
...
}
private void MyForm_Load(object sender, EventArgs e) {
foreach (Control control in myPanel.Controls)
control.Click += MyClick;
...
}
Related
I have two panels in my winform. panel1 is blue, and panel2 is red. I would like to know if there is a function that tells what panel i clicked on, otherwise then using private void panel1_Click(object sender, EventArgs{} function. Thank you, here is my code:
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Control control = this.GetChildAtPoint(e.Location);
if (control is Panel)
{
Panel clickedPanel = (Panel)control;
if (clickedPanel.Name.Equals("panel1"))
{
MessageBox.Show("you clicked on blue panel");
}
else
{
MessageBox.Show("you clicked on red panel");
}
}
}
This function doesn't work, and I don't know what to do.
I tried many functions, but i can't search for any "universal" function, that tells what object i clicked on.
You have attached a function for the form click event handler; this will handle mouse click events on the form and not on the panel. If you can view the Panels on the designer, then you can double-click each and the IDE will auto generate the method that will handle the mouse click event for each Panel.
Alternatively, you can add the event handlers for each Panel in the constructor of the form like below:
public class MyForm: Form{
public MyForm(){
InitializeComponent();
//register the event listeners for the panels
panel1.MouseClick += panel1_Clicked;
panel2.MouseClick += panel2_Clicked;
}
//implement the methods
private void panel1_Clicked(object sender, MouseEventArgs e){
//handle the click for panel1
}
private void panel2_Clicked(object sender,
MouseEventArgs e){
//handle the click for panel2
}
}
i have a problem with user control.
i create it dynamically on my aspx page after clicking on a button:
protected void btnAddRules_Click(object sender, EventArgs e)
{
RuleProperty Control = (RuleProperty)LoadControl("RuleProperty.ascx");
MyPanel.Controls.Add(Control);
}
when i click on a button of my user control, the button event wont fire and the user control will disappear. here is the button event:
protected void btnAdd_Click1(object sender, EventArgs e)
{
WowzaRule rule = GetRuleFromGUI();
RuleList.Add(rule);
//Session["RuleList"] = RuleList;
//List<WowzaRule> test = new List<WowzaRule>();
SaveToXMLFiles(txtdbnum.Text, RuleList);
}
i understand that after pressing the button on mypage the usercontrol is released and if its not created on pag_init or page Load it wont stay, but i need to create it on my button click event and find a way for it not to disapper.
thanks in advance, Daniel
You might have to add an event handler that it can fire the click event and call your delegate
Control.Click += btnAdd_Click1;
Dynamically created controls, once added, have to be on a page on every page load in order to work correctly. What happens in your case:
RuleProperty is added after the button click
Page loads with this control
User clicks on the button within RuleProperty
Control is not added to the control tree during the page load (corresponding code is only in the button click handler, and that button was not clicked)
ASP.NET does not know which control triggered the event, so the event is not processed
To go around this issue you need to add you control on every page loading, for example using some flag stored in ViewState:
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["AddRuleProperty"] != null && (bool)ViewState["AddRuleProperty"])
{
AddRulePropertyControl();
}
}
protected void btnAddRules_Click(object sender, EventArgs e)
{
AddRulePropertyControl();
ViewState["AddRuleProperty"] = true;
}
private void AddRulePropertyControl()
{
RuleProperty Control = (RuleProperty)LoadControl("RuleProperty.ascx");
MyPanel.Controls.Add(Control);
}
Update.
If you want to remove the control from the page later on in the control's click handler, you need to remove corresponding ViewState key. This is not possible from the control directly, since property Page.ViewState is protected, and also this would have created an unwanted dependency.
What seems as the right way to do this is to subscribe to the very same event from the Page (you might need to make this event visible from the controller) and reset the key in there. Like this:
private void AddRulePropertyControl()
{
RuleProperty Control = (RuleProperty)LoadControl("RuleProperty.ascx");
Control.ButtonClick += RuleProperty_ButtonClick;
MyPanel.Controls.Add(Control);
}
private void RuleProperty_ButtonClick()
{
ViewState["AddRuleProperty"] = false;
}
Please note that event name here is not real, this is just a sketch of what can be done.
I was on my way to create a custom Progress Bar when I hit this problem [WinForm]
**The Structure:**
-panel
-> panel
so I have panel which inside the panel, there another panel.
**The Goals:**
-I want to use my parent panel as all event handler,
while make the child panel have no event at all.
**The Problem:**
- when I press my mouse inside the child panel. the event in parent wont called.
explanation : -> I still wanted to call parent panel mouse down
even if I click on top of my child panel.
So you want the click event to fire on the parent Panel when the child panel is clicked.
I can think of 2 ways you can do this.
First way is to simply call the Click event handler method for Panel1 from inside the Panel2's Click event handler method:
private void panel1_Click(object sender, EventArgs e)
{
MessageBox.Show("Panel 1 clicked.");
}
private void panel2_Click(object sender, EventArgs e)
{
this.panel1_Click(sender, e);
}
Probably a better way would be to register both click events to the 1 handler method:
private void panel1_Click(object sender, EventArgs e)
{
MessageBox.Show("Panel 1 clicked.");
}
Then from the Form Designer or manually register the event for the second panel:
this.panel2.Click += new System.EventHandler(this.panel1_Click);
Yesterday I have implemented some validating events for controls in a groupbox located on a WinForm. I have set the AutoValidate property of the form to Disabled, I have set the CausesValidation property of the controls to true and implemented the Validating event of the controls. By calling the ValidateChildren() method of the form I force that validating events are executed. This was working all fine.
But after placing this groupbox on top of a picturebox and setting the picturebox as parent of the groupbox then validating events aren't executed anymore....
Below some demo code. Form is only containing a picturebox, groupbox, textbox and button.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
MessageBox.Show("Validating textbox");
e.Cancel = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (ValidateChildren())
MessageBox.Show("Validation not executed :-(");
else
MessageBox.Show("Validation executed :-)");
}
private void Form1_Load(object sender, EventArgs e)
{
groupBox1.Parent = pictureBox1;
}
}
The ValidateChildren() method calls ValidateChildren(ValidationConstraints.Selectable) to get the job done. That's a problem for a PictureBox, it isn't selectable. So none of its children get validated either.
Calling it with ValidationConstraints.None doesn't work either, the ability to validate child controls is implemented by ContainerControl and PictureBox doesn't derive from it. So you can't call ValidateChildren on the PictureBox either. Enumerating the controls yourself and triggering the Validating event can't work either, the PerformControlValidation() method is internal.
You'll need to re-think the idea of trying to turn the PictureBox into a ContainerControl. Most any control can resemble a picturebox, if not through the BackgroundImage property then through the Paint event.
I want to add MouseOver and MouseLeave events to dynamical created panels in a flowLayoutPanel.
I added all panels in a list named "panels" and they are accessible with "panels[index]".
Now I want to dynamical add a MouseOver and MouseLeave event to each panel.
I thought it could be possible to get the panelname the Mouse is over and use just one method for each event and identify the panel the mouse is over with its panelname (panel.Name) but I found nothing in "sender".
Is there a way to do this?
My code:
//Method
private void PanelsMouseEnter(object sender, EventArgs e)
{
var panel = sender as Control;
foreach (Control control in this.fLpKoerper.Controls)
{
if (control.Name == panel.Name)
{
foreach (Panel panels in panelsKoerper)
{
if (panels.Name == panel.Name)
panels.BackColor = Color.DarkGray;
}
}
}
}
//Event
panelsKoerper[y].MouseEnter += PanelsMouseEnter;
var panel = sender as Control;
var thePanelName = panel.Name;
I believe you can generate one mouseover event for a control, copy that event method name and then paste it into another controls mouseover event box and that should work
So you would have this event
private void label1_MouseHover(object sender, EventArgs e)
{
//Code...
}
and then you could put 'label1_MouseHover' in any controls mouseover event