How to fire click event of panel programmatically? - c#

I have a winforms app in vs2010 and a panel whose click event I would like to fire programatically. How do I do this? Button has a PerformClick but I cannot find the same in Panel.

Your panel's Click event is going to be attached to an event handler, right?
Then just call that event handler from the button's click event handler:
public void Panel1_Click(object sender, EventArgs e)
{
//Do whatever you need to do
}
public void Button1_Click(object sender, EventArgs e)
{
//Do anything you need to do first
Panel1_Click(Panel1, EventArgs.Empty);
}
The effect will be the same as clicking on the panel.

Related

Why my custom event fires twice?

I've searched relevant posts but I got nothing much
I have created a user control. In my user control there is a text box. I want to have an event in my user control that fires whenever text box TextChanged event raises. This is what I have done so far : (This is code of user control)
public event EventHandler txtchnged;
public void ontxtchnged()
{
txtchnged(this, EventArgs.Empty);
}
public MyTextBox()
{
InitializeComponent();
textBox1.TextChanged += textBox1_TextChanged;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
ontxtchnged();
}
Here is where I have used user control
public RegisterMainFrm()
{
InitializeComponent();
myUserControl1.txtchnged += myUserControl1_txtchnged;
}
private void myUserControl1_txtchnged(object sender, EventArgs e)
{
Console.WriteLine("hello");
}
This works and I know that the code might not be clean but that's not the problem. Problem is : "hello" will be printed in console twice and I really don't know why and how to fix it.
From MSDN on TextBox.TextChanged:
Note:This event fires when the TextBox control is created and
initially populated with text.
Could this be your problem that you get the initial event?
UPDATE:
From Adriano Repetti Hint in Comments: Did you get the textBox1_TextChanged event handler by double clicking in the designer?
Then you have added a second hook to the TextChanged Event.
Check the code inside InitializeComponent of your UserControl if it is already hooking the event.

changing a button with a checkbox on another form. c#

I am trying to change a buttons visible status with a checkbox that is on another form for example;
when the checkbox checked state changes on form 2 than the visible property for button 1 on form1 changes. If someone could point me in the right direction i would appropriate it thank you.
Define event on Form2. E.g.
public event EventHandler StateChanged;
Raise this event when checkbox checked state changes:
private void Checkbox_CheckedChanged(object sender, EventArgs e)
{
if (StateChanged != null)
StateChanged(this, EventArgs.Empty);
}
Subscribe to this event in Form1
form2.StateChanged += Form2_StateChanged;
Change a button in this event handler:
private void Form2_StateChanged(object sender, EventArgs e)
{
button.Visible = !button.Visible;
}
Normally when I do something like this I add a method that calls it.
Form1 with the button
public void SetVisibility(bool visible)
{
Button.Visible = visible;
}
Form 2 With CheckBox
CheckBox_CheckChanged(object sender, EventArgs e)
{
Form1.SetVisibility(CheckBox.Checked);
}
Adding a public method on your form also enables you to control it from others forms. Not sure what the best answer is, thats up to you.
EDIT:
Not related to the question but...
On Form 2
var form1Control = new Form1();
form1Control.SetVisibility(true);

Call a function of parent aspx page from web user control

i have a aspx page on it i'm dynamically adding the web user controls in a placeholder. and know i have cancel button on the one user control and on click of that Cancel button i want to load other user control inside the aspx page (placeholder).
how can i do that, if i create an event handler in usercontrol then that become null.
thanks
A user control should never, ever have any kind of dependency to the page where it lives. Add an event to the control and fire it from the Cancel button event handler.
public event EventHandler CancelClicked;
protected Cancel_Click(object sender, EventArgs e)
{
if(CancelClicked != null)
{
CancelClicked(this, e);
}
}
From your page, subscribe to the controls's CancelClicked event and do whatever operation should be done on that other user control.
I believe the best way to achieve this would to create an event in the user control that is fired when you need it to communicate, then in the page you can subscribe to this event.
Your User Control
Put this in your control as an event variable.
public event EventHandler CancelRequested;
Then in the cancel button click event :
CancelRequested(this, new CommandEventArgs("CancelClicked", SomeVariable));
Your Page (or parent)
Then subscribe to the event in the parent page like this (when you dynamically add it in):
ctrlYourControl.CancelRequested += new EventHandler(ctrlYourControl_CancelRequested);
Declare the event handler :
void ctrlYourControl_CancelRequested(object sender, EventArgs e)
{
// display other user control
}
Add this event to your user controls:
public event EventHandler CancelClicked;
Now, this method on your parent aspx page.
public void OnCancelClicked (object sender, EventArgs data)
{
// do your stuffs like loading other controls and
// whatever when event fired
}
When you are loading your user control dynamically, add this:
var someControl = LoadControl(#"~\SomeControl.ascx") as SomeControl;
someControl.CancelClicked += new EventHandler(OnCancelClicked);
And on your user controls:
public void btnCancel_Click(object sender, EventArgs e)
{
if(CancelClicked != null)
{
CancelClicked(this, e);
}
}
This should do !

How do I Manually Invoke/Raise Mouse Events in Silverlight 4?

In Silverlight 4, I wish to invoke one of the mouse button click events when the right mouse button is clicked. I have the RightMouseButtonDown click event wired up, but I don't know how to manually fire the MouseLeftButtonUp event.
I have tried raising the event in the following fashion.
private void MainLayoutRootMouseButton(object sender, MouseButtonEventArgs e)
{
MouseLeftButtonDown(sender, e);
}
However, a compiler error occurs:
"The event 'System.Windows.UIElement.MouseLeftButtonDown' can only appear on the left hand side of += or -=
What do I need to do to manually raise this event?
The other answers are correct in that they show you how to call those same code paths as you have for the other mouse events.
It should be clear that in Silverlight, you cannot raise (or automate) actual mouse button clicks, for security reasons.
Only user initiated actions such as the actual mouse moving can create real MouseEventArgs and fire the handlers directly, through the Silverlight input system.
You really shouldn't be raising the event itself. Instead, make the code inside the MouseLeftButtonUp its own function and then call that function from both the MouseLeftButtonUp and MouseRightButtonUp events.
e.g.
private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DoTheSameThing(sender, e);
}
private void MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
DoTheSameThing(sender, e);
}
private void DoTheSameThing(object sender, MouseButtonEventArgs e)
{
//Handle left or right mouse buttons up event
}
In your page's init function you need to have this line of code
someObjectOnPage.MouseLeftButtonDown += new MouseDownEventHandler(DoSomething);
someObjectOnPage.MouseRightButtonDown += new MouseDownEventHandler(DoSomething);
This is pure psuedo code but should lead you on the right track
also your method will need to look like this
void DoSomething(object sender, MouseButtonEventArgs e) { }

WPF popup capturing mouse doubleclick events

In my main application window, there are controls, each of which opens a popup that presents more controls to the user.
Other controls in the main application window have mousedoubleclick event handlers. My problem is that when the a user double clicks in the popup, the controls behind the popup are receiving the mousedoubleclick events.
I've tried added a mousedoubleclick event handler to the popup's parent, and handling the event, but it still gets through to the main application window.
private void ParentControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
I've also tried invoking Popup.CaptureMouse() in a MouseEnter event handler in the popup, but the method always fails (returns false).
void popup_MouseEnter(object sender, MouseEventArgs e)
{
e.Handled = true;
Popup popup = sender as Popup;
bool success = popup.CaptureMouse();
}
Are there any other ways to prevent the mouse events from firing in the main application window when the popup is open?
Easy! Instead of using the control's MouseDoubleClick event
private void myControl_MouseDoubleClick(System.Object sender, System.Windows.Input.MouseButtonEventArgs e)
{
MessageBox.Show("MouseDoubleClick on control");
}
use the PreviewMouseDoubleClick event.
private void myControl_PreviewMouseDoubleClick(System.Object sender, System.Windows.Input.MouseButtonEventArgs e)
{
MessageBox.Show("PreviewMouseDoubleClick on control");
}
Now double-clicking on your control will not also invoke the parent's DoubleClick event.

Categories