I have a class (which extends Framework Element) which contains within it a number of other Elements.
// Click event coverage area
private Rectangle connectorRectangle;
These shapes all have their event handlers, and when the user clicks on them its working well. Now what I want is to be able to 'handle' a right-click on my class from outside the scope of the class.
So I figured the best way to do it is to handle the event internally, and somehow bubble it to the top
private void connectorRectangle_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
MouseButtonEventArgs args = new MouseButtonEventArgs();
//???
e.Handled = true;
}
The problem is that I have no idea how to raise the event. this.OnMouseRightButtonUp doesn't exist, and all the tutorials I'm finding are for raising custom events.
I'm pretty new to silverlight, so bear with me if I missed something obvious.
Try it :
public Rectangle
{
this.Click += new System.EventHandler(Function);
}
private void Function(object sender, System.EventArgs e)
{
if (((MouseEventArgs)e).Button == MouseButtons.Right)
{
//Your code
}
}
Your "exteded Framework Element class" shouldn't handel the mouse event (or if they handel them, set e.Handled to false). Then the event should bubble up automatically (without reraise the event).
EDIT
public class ExtendedFrameworkElement : Grid
{
public ExtendedFrameworkElement()
{
Border b1 = new Border();
b1.Padding = new Thickness(20);
b1.Background = Brushes.Red;
b1.MouseRightButtonUp += b1_MouseRightButtonUp;
Border b2 = new Border();
b2.Padding = new Thickness(20);
b2.Background = Brushes.Green;
b2.MouseRightButtonUp += b2_MouseRightButtonUp;
b1.Child = b2;
this.Children.Add(b1);
}
private void b1_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//DoSomeThing
e.Handled = false;
}
private void b2_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//DoSomeThing
e.Handled = false;
}
}
Xaml:
<Window x:Class="WpfApplicationTest.MainWindow">
<wpfApplicationTest:ExtendedFrameworkElement MouseRightButtonUp="UIElement_OnMouseRightButtonUp"/>
</Window>
Code Behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void UIElement_OnMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
//DoSomeThing
}
}
Related
I have a host class which inherits from Form. I use it to embed an WPF user control to use it in a winform application. This is customizable, I mean, you can choose to make form borderless or not.
If borderless, you cannot move the window, so I need to implement it manually. In this case I subscribe to some event which do the trick (I have removed the not necessary things from the class to focus only on the important parts):
public class MyHostDialog : Form
{
public MyDialogHost()
{
InitializeComponent();
}
public MyHostDialog(MyDialog myDialog) : this()
{
this.ElementHost.Child = new MyHostDialogView(myDialog);
if (this.Borderless)
{
this.ElementHost.Child.MouseDown += Child_MouseDown;
this.ElementHost.Child.MouseUp += Child_MouseUp;
this.ElementHost.Child.MouseMove += Child_MouseMove;
}
}
// BELOW CODE DOES NOT WORK, IT WOULD WORK IF Input.MouseButtonEventArgs and Input.MouseEventArgs WERE
// System.Windows.FORMS.MouseEventArgs INSTEAD.
private bool dragging = false;
private System.Drawing.Point startPoint = new System.Drawing.Point(0,0);
private void Child_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
dragging = true;
startPoint = new System.Drawing.Point(e.X, e.Y);
}
private void Child_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
dragging = false;
}
private void Child_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (dragging)
{
System.Drawing.Point p= PointToScreen(e.Location);
Location = new System.Drawing.Point(p.X - this.startPoint.X, p.Y - this.startPoint.Y);
}
}
}
Above code would work if instead of using Input.MouseButtonEventArgs and Input.MouseEventArgs, they were System.Windows.FORMS.MouseEventArgs. So, how to convert that code to work with MouseButtonEventArgs?
I have 18 buttons on the child form "Control Test" which send event to the parent form
Out of 18 buttons, 14 are ON and OFF functionality, making 7 pairs as in the picture
The problem is raising the event for each button, it causes very long and messy code, both in the child and the parent form,
Is there any less complex way to do it? like I have done with the menu.
Child Form:
Child Form:
// B Plus Relay On Button
public event EventHandler BPRElayOnBtnClicked;
protected virtual void WhenBPRelayOnBtnClicked(EventArgs e)
{
BPRElayOnBtnClicked.Invoke(this, e);
}
private void BPRelayOn_btn_Click(object sender, EventArgs e)
{
WhenBPRelayOnBtnClicked(e);
}
// B Plus Relay OFF Button
public event EventHandler BPRElayOffBtnClicked;
protected virtual void WhenBPRelayOffBtnClicked(EventArgs e)
{
BPRElayOnBtnClicked.Invoke(this, e);
}
private void BPRelayOff_btn_Click(object sender, EventArgs e)
{
WhenBPRelayOffBtnClicked(e);
}
// B Minus Relay ON Button
public event EventHandler BMRElayOnBtnClicked;
protected virtual void WhenBMRelayOnBtnClicked(EventArgs e)
{
BMRElayOnBtnClicked.Invoke(this, e);
}
private void BMRelayOn_btn_Click(object sender, EventArgs e)
{
WhenBMRelayOnBtnClicked(e);
}
// B Minus Relay OFF Button
public event EventHandler BMRElayOffBtnClicked;
protected virtual void WhenBMRelayOffBtnClicked(EventArgs e)
{
BMRElayOffBtnClicked.Invoke(this, e);
}
private void BMRelayOff_btn_Click(object sender, EventArgs e)
{
WhenBMRelayOffBtnClicked(e);
}
...... //event for each button
Parent Form:
private void viewToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem menu = sender as ToolStripMenuItem;
switch (menu.Name)
{
case "controlTestToolStripMenuItem":
if (Application.OpenForms["CtrlTest"] is CtrlTest ctrlTest)
{
ctrlTest.Focus();
return;
}
ctrlTest = new CtrlTest();
ctrlTest.BPRElayOnBtnClicked += CtrlTest_BPRElayOnBtnClicked;
ctrlTest.BPRElayOffBtnClicked += CtrlTest_BPRElayOffBtnClicked;
ctrlTest.BMRElayOnBtnClicked += CtrlTest_BMRElayOnBtnClicked;
ctrlTest.BMRElayOffBtnClicked += CtrlTest_BMRElayOffBtnClicked;
ctrlTest.PreRElayOnBtnClicked += CtrlTest_PreRElayOnBtnClicked;
ctrlTest.PreRElayOffBtnClicked += CtrlTest_PreRElayOffBtnClicked;
ctrlTest.MdiParent = this;
ctrlTest.Show();
break;
.........//Other menus ...
default:
break;
private void CtrlTest_BPRElayOnBtnClicked(object sender, EventArgs e)
{
//Do something here
}
private void CtrlTest_BPRElayOffBtnClicked(object sender, EventArgs e)
{
//Do something here
}
private void CtrlTest_BMRElayOnBtnClicked(object sender, EventArgs e)
{
//Do something here
}
private void CtrlTest_BMRElayOffBtnClicked(object sender, EventArgs e)
{
//Do something here
}
For the on/off, take a look at the following control. For the others setup one event and work out logic similar to the on/off buttons.
Download the source, add the class project to your Visual Studio solution. Open the class project and change the .NET Framework currently set to 4, to your project's .NET Framework version.
Setup an enum where for each on/off button set it's tag to one of the members. this ways things are clearer using a switch and enums.
public enum OperationType
{
BPlusRelay,
BMinusRelay,
PreRelay,
CycleCount,
PairDown,
TestMode,
StandbyMode
}
In the child form, setup and event and set tags for each on/off control. Change the names to reflect their purpose, I simply added them quickly for demoing purposes.
public partial class ChildForm : Form
{
public delegate void OnClicked(OperationType operationType, bool state);
public event OnClicked ClickedEvent;
public ChildForm()
{
InitializeComponent();
SetProperties();
}
public void SetProperties()
{
ToggleSwitch1.Tag = OperationType.BPlusRelay;
ToggleSwitch2.Tag = OperationType.BMinusRelay;
ToggleSwitch3.Tag = OperationType.PreRelay;
ToggleSwitch4.Tag = OperationType.CycleCount;
ToggleSwitch5.Tag = OperationType.PairDown;
ToggleSwitch6.Tag = OperationType.TestMode;
ToggleSwitch7.Tag = OperationType.StandbyMode;
var list = Controls.OfType<JCS.ToggleSwitch>().ToList();
foreach (var toggleSwitch in list)
{
toggleSwitch.CheckedChanged += ToggleSwitchOnCheckedChanged;
}
}
private void ToggleSwitchOnCheckedChanged(object sender, EventArgs e)
{
var current = (JCS.ToggleSwitch)sender;
ClickedEvent?.Invoke((OperationType)current.Tag, current.Checked);
}
}
In the main form, show the child form, subscribe to the event above.
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void ShowChildFormButton_Click(object sender, EventArgs e)
{
var childForm = new ChildForm();
childForm.ClickedEvent += ClickedEvent;
try
{
childForm.ShowDialog();
}
finally
{
childForm.Dispose();
}
}
private void ClickedEvent(OperationType operationType, bool state)
{
switch (operationType)
{
case OperationType.BPlusRelay:
// TODO
break;
case OperationType.BMinusRelay:
// TODO
break;
case OperationType.PreRelay:
// TODO
break;
case OperationType.CycleCount:
// TODO
break;
case OperationType.PairDown:
// TODO
break;
case OperationType.TestMode:
// TODO
break;
case OperationType.StandbyMode:
// TODO
break;
}
}
}
Partly done form, and note you can change the size of the buttons.
I am trying to implement a horizontal swipe event handler in the following app. However, the gr_CrossSliding cross sliding event handler never fires.
What do I need to do to fire gr_CrossSliding?
public sealed partial class MainPage : Page
{
private GestureRecognizer gr;
public MainPage()
{
this.InitializeComponent();
gr = new GestureRecognizer();
gr.GestureSettings = GestureSettings.CrossSlide;
gr.CrossSlideHorizontally = true;
gr.CrossSliding += gr_CrossSliding;
}
void gr_CrossSliding(GestureRecognizer sender, CrossSlidingEventArgs args)
{
// handle swipe event
}
}
You need to set the GestureRecognizer on the handlers of the UI Element that is getting the gestures.
In this case, I'm using a Grid (GrdFoto).
public MainPage()
{
this.InitializeComponent();
gestureRecognizer.GestureSettings = Windows.UI.Input.GestureSettings.Drag;
gestureRecognizer.Dragging += gestureRecognizer_Dragging;
GrdFoto.PointerPressed += GrdFoto_PointerPressed;
GrdFoto.PointerMoved += GrdFoto_PointerMoved;
GrdFoto.PointerReleased += GrdFoto_PointerReleased;
GrdFoto.PointerCanceled += GrdFoto_PointerCanceled;
}
void GrdFoto_PointerPressed(object sender, PointerRoutedEventArgs e)
{
this.gestureRecognizer.ProcessDownEvent(e.GetCurrentPoint(this.GrdFoto));
this.GrdFoto.CapturePointer(e.Pointer);
e.Handled = true;
}
void GrdFoto_PointerMoved(object sender, PointerRoutedEventArgs e)
{
this.gestureRecognizer.ProcessMoveEvents(e.GetIntermediatePoints(this.GrdFoto));
}
void GrdFoto_PointerReleased(object sender, PointerRoutedEventArgs e)
{
this.gestureRecognizer.ProcessUpEvent(e.GetCurrentPoint(this.GrdFoto));
e.Handled = true;
}
void GrdFoto_PointerCanceled(object sender, PointerRoutedEventArgs e)
{
this.gestureRecognizer.CompleteGesture();
e.Handled = true;
}
void gestureRecognizer_Dragging(GestureRecognizer sender, DraggingEventArgs args)
{
// Drag completed.
}
I have a form with 2 tabs on it. I can chose the tab viewed after initialization and I need some initial code every time after the tab2 is initialized:
public partial class SetupComponent : Form
{
public SetupComponent(bool tab2)
{
InitializeComponent();
if (tab2)
{
this.tabControl1.SelectedTab = tabPage2;
}
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
textBox2.SelectionStart = textBox2.Text.Length;
textBox2.Focus();
}
}
if I call this class with tab2=false and then click onto tab2, tabControl1_SelectedIndexChanged is called.
But if I select the tab2=true during SetupComponent, I find no possibility to do that code. All the TabControl1_Events I found are too early and I don`t find a matching TabPage2_Event.
How can I manage it?
I managed this problem using the Paint_Event:
bool activated = false;
private void tabPage2_Paint(object sender, PaintEventArgs e)
{
if (!activated)
{
tabControl1_SelectedIndexChanged(null, null);
activated = true;
}
}
I use the variable because the Paint_Event is called many times.
I need to be able to determine when ContainsFocus changes on a Control (specifically a windows form). Overriding OnGotFocus is not the answer. When I bring the form to the foreground, ContainsFocus is true and Focused is false. So is there an OnGotFocus equivalent for ContainsFocus? Or any other way?
Note: GotFocus events of the child controls are fired if you have a child control. Otherwise OnGotFocus of the form is called.
If I understood the question correctly, then this should work:
bool lastNotificationWasGotFocus = false;
protected override void OnControlAdded(ControlEventArgs e)
{
SubscribeEvents(e.Control);
base.OnControlAdded(e);
}
protected override void OnControlRemoved(ControlEventArgs e)
{
UnsubscribeEvents(e.Control);
base.OnControlRemoved(e);
}
private void SubscribeEvents(Control control)
{
control.GotFocus += new EventHandler(control_GotFocus);
control.LostFocus += new EventHandler(control_LostFocus);
control.ControlAdded += new ControlEventHandler(control_ControlAdded);
control.ControlRemoved += new ControlEventHandler(control_ControlRemoved);
foreach (Control innerControl in control.Controls)
{
SubscribeEvents(innerControl);
}
}
private void UnsubscribeEvents(Control control)
{
control.GotFocus -= new EventHandler(control_GotFocus);
control.LostFocus -= new EventHandler(control_LostFocus);
control.ControlAdded -= new ControlEventHandler(control_ControlAdded);
control.ControlRemoved -= new ControlEventHandler(control_ControlRemoved);
foreach (Control innerControl in control.Controls)
{
UnsubscribeEvents(innerControl);
}
}
private void control_ControlAdded(object sender, ControlEventArgs e)
{
SubscribeEvents(e.Control);
}
private void control_ControlRemoved(object sender, ControlEventArgs e)
{
UnsubscribeEvents(e.Control);
}
protected override void OnGotFocus(EventArgs e)
{
CheckContainsFocus();
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
CheckLostFocus();
base.OnLostFocus(e);
}
private void control_GotFocus(object sender, EventArgs e)
{
CheckContainsFocus();
}
private void control_LostFocus(object sender, EventArgs e)
{
CheckLostFocus();
}
private void CheckContainsFocus()
{
if (lastNotificationWasGotFocus == false)
{
lastNotificationWasGotFocus = true;
OnContainsFocus();
}
}
private void CheckLostFocus()
{
if (ContainsFocus == false)
{
lastNotificationWasGotFocus = false;
OnLostFocus();
}
}
private void OnContainsFocus()
{
Console.WriteLine("I have the power of focus!");
}
private void OnLostFocus()
{
Console.WriteLine("I lost my power...");
}
One way to solve this is to use a Timer. It's definitely brute force, but it gets the job done:
private Timer m_checkContainsFocusTimer = new Timer();
private bool m_containsFocus = true;
m_checkContainsFocusTimer.Interval = 1000; // every second is good enough
m_checkContainsFocusTimer.Tick += new EventHandler(CheckContainsFocusTimer_Tick);
m_checkContainsFocusTimer.Start();
private void CheckContainsFocusTimer_Tick(object sender, EventArgs e)
{
if (!m_containsFocus && ContainsFocus)
OnAppGotFocus();
m_containsFocus = ContainsFocus;
}
But is there an easier way?
Handling the GotFocus and LostFocus events should do it.
Another thing to note... the SDK says this about the ContainsFocus property:
You can use this property to determine
whether a control or any of the
controls contained within it has the
input focus. To determine whether the
control has focus, regardless of
whether any of its child controls have
focus, use the Focused property.
EDIT:
When handling the GotFocus event, you may still have to check the Focused/ContainsFocus property depending on how the hierarchy of your controls is set up.
ContainsFocus will be true if the control or any of its children have focus.
Focus will only be true if the specific control itself has focus, regardless of its children.