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);
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
}
}
So I would like to know what is wrong with the following code, especially from a theoretical point of view.
I have a user control in which I've added a text box.
When I click in the text box I would like the Mouse clicked event raised in the user control.
To my mind, the solution should be:
Create an event handler for the mouse click event in the text box.
in this event handler, raise the mouse click event for the user control.
so this is what i have:
private void txtLog_MouseClick(object sender, MouseEventArgs e)
{
this.OnMouseClick(e);
}
i have tried it and it doesn't work, why is this?
P.S. I would really like to know why this is wrong! A correct solution is great, but I'm really trying to understand where I'm going wrong here. Thank :-)
Well, you could just click on your textbox in design mode and in the property window in events tab add the click event. or if you want to do it in runtime you can do it like this:
textbox.Click += Txt_Click;
private static void Txt_Click(object sender, EventArgs e)
{
// do your thing
}
or even shorter:
textbox.Click += (s,e) =>
{
//do your thing
};
you should do these three steps
declare an MouseClick delegation method for textbox
assign method to textbox
add this delegation to the this (form) OnMouseClick event [on user control constructor]
Step1:
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
}
Step2:
this.textBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseClick);
Step3:
public myUserControl()
{
InitializeComponent();
this.MouseClick += new MouseEventHandler(textBox1_MouseClick);
}
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;
...
}
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 have an application with a Panel containing children Form objects. When I click one of the children Form it brings to front. I would like to know which one is in front now...
I've looked in event list but cant find proper event form my purpose :(
These methods doesn't work:
protected void OpenedFileForm_Enter(object sender, EventArgs e)
{
MessageBox.Show("enter");
}
protected void OpenedFileForm_Click(object sender, EventArgs e)
{
MessageBox.Show("click");
}
protected void OpenedFileForm_Activated(object sender, EventArgs e)
{
MessageBox.Show("activated");
}
protected void OpenedFileForm_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("mouse click");
}
protected void OpenedFileForm_Shown(object sender, EventArgs e)
{
MessageBox.Show("shown");
}
OpenFileDialog openFile1 = new OpenFileDialog();
openFile1.DefaultExt = "*.txt";
openFile1.Filter = "TXT Files|*.txt|RTF Files|*.rtf";
if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
openFile1.FileName.Length > 0)
{
switch (Path.GetExtension(openFile1.FileName))
{
case ".txt":
txtForm childTXT = new txtForm();
this.childForms.Add(childTXT);
childTXT.Parent = this.mainPanel;
childTXT.richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText);
childTXT.Show();
break;
}
}
Have you tried the Form.Activated Event?
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activated(v=vs.80).aspx
Edit:
If you are in an MDI application, you might need to use MdiChildActivate instead.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.mdichildactivate.aspx
This code can only work when you set the Form.TopLevel property to false. Which makes it turn into a child control, almost indistinguishable from a UserControl.
This has many side-effects, for one there is no notion of "front" anymore. The Z-order of child controls is determined by their position in their parent's Controls collection. And it affects the events it fires, Activated and Deactivated will never fire. Furthermore, the Form class was designed to be a container control, it doesn't like taking the focus itself. Its child controls get the focus, the Form class doesn't have any use for focus. Which is why the Enter, Click and MouseClick events don't fire, they are events that require focus.
Long story short, what you are trying to do doesn't make a wholeheckofalot of sense. If it is strictly the Z-order you want to fix then write an event handler for the MouseDown event:
void OpenedFileForm_MouseDown(object sender, MouseEventArgs e) {
var frm = (Form)sender;
frm.BringToFront();
}
You could add frm.Select() to get the Enter event to fire, but only do that if the form doesn't contain any focusable controls itself. Do note that there is evidence that you don't assign the events correctly in your code. The Shown event does fire. It is also important that you set the FormBorderStyle to None, the title bar cannot indicate activation status anymore.
Ok, I got this! Thx for help everyone. You gave me a hint to think about equity of my strange MDI idea where Panel is parent for other Forms. I Removed SplitContainer containing Panel and just did standard MDI application, where Forms are MDIChildren of main Form.
childTXT.MdiParent = this;