user control and raising events from controls placed in user control - c#

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);
}

Related

one click calls both Click and LinkClicked event handlers

LinkLabel label = new LinkLabel();
// imagine there is a code to initialize the label
label.Click += (sender, args) => callback1();
label.LinkClicked += (sender, args) => callback2();
If I click the label anywhere but not its link then callback1() is called which is correct.
If I click the label's link then both callback1() and callback2() are called.
How do I make it call callback2() only?
Two solutions I can think of. First one is a very silly one but looks pretty effective. You don't like Click when the mouse hovers over a link. Which has a side-effect, the mouse cursor changes. So you could filter by checking the cursor shape:
private void linkLabel1_Click(object sender, EventArgs e) {
if (Cursor.Current == Cursors.Default) {
Debug.WriteLine("Click!");
// etc...
}
}
A bit more principled and handy if you have a lot of link labels is to not raise the Click event at all if the mouse is over a link. Add a new class to your project and paste this code:
using System;
using System.Windows.Forms;
class LinkLabelEx : LinkLabel {
protected override void OnClick(EventArgs e) {
var loc = this.PointToClient(Cursor.Position);
if (this.PointInLink(loc.X, loc.Y) == null) base.OnClick(e);
}
}
I don't see a way to do that.
As you can see in the reference source, LinkLabel is derived from Label. The Click event is raised by the Label base class. You can see in the code that the base methods like OnMouseDown and OnMouseUp are always called before the LinkLabel handles those events.
So the Label base implementation will always raise a Click event before the LinkClicked implementation raises the LinkLabel event. There is no property or flag to prevent that.
You hopefully can achieve what you want in a different way.

How can I use mousemove event in c#

The question is this:
when the mouse cursor moved on the button some thing should be happen but I don't know what exactly have to write
When you select the button in the VS-designer you will have access to the properties and events (lightning Icon in the property window).
In the events-listing are all events that the button can fire. May be for your purpose the events: ´MouseEnter´ and ´MouseLeave´ would be a good choice. Just double click the event and Visual Studio will generate the appropriate method. Like this:
private void button1_MouseEnter(object sender, EventArgs e)
{
// my code
this.button1.BackColor = Color.Red;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
// my code
this.button1.BackColor = Color.Green;
}
In my example I just change the backcolour of the button when the mouse is on the button and change it again when it leaves the button.
Practically you could run any code inside the generated method.
You can create eventHandler like this :
myButton.MouseMove += new MouseEventHandler(doSomething);
Where myButton is the button from which you want to trigger the event when mouse moves over it. and doSomething() is the method defined as like the following:
public void doSomething(object sender, MouseEventArgs e)
{
// do what ever you want
}

Problems with Combobox MouseDown Events

I have two Problems with the Mouse Events of ComboBoxes. What I want to achieve is a "Touch-and-Release"-Solution, that means after the User pressed/touched the Combobox for 500ms something should happen. I have Class where I fireup all my Events to my Controls. For the Comboboxs I do it like this:
((ComboBox)obj).PreviewMouseDown -= CTRLMouseButtonEventHandler_Down;
((ComboBox)obj).PreviewMouseDown += CTRLMouseButtonEventHandler_Down;
((ComboBox)obj).PreviewMouseUp -= CTRLMouseButtonEventHandler_Up;
((ComboBox)obj).PreviewMouseUp += CTRLMouseButtonEventHandler_Up;
My Up/Down-Events look like this:
private void CTRLMouseButtonEventHandler_Down(object sender, MouseEventArgs e)
{
_currentControl = (Control)sender;
_touchHoldTimer = new System.Windows.Threading.DispatcherTimer();
_touchHoldTimer.Interval = TimeSpan.FromMilliseconds(500);
_touchHoldTimer.Tick += TouchHoldTimer_Tick;
_touchHoldTimer.Start();
}
private void CTRLMouseButtonEventHandler_Up(object sender, MouseEventArgs e)
{
_touchHoldTimer.Stop();
}
The first Problem is, when my Focus is on another Control and click into the Combobox and hold, nothing happens. I first have to click into the Combobox and then click-and-hold and it works.
My second Problem is, that the PreviewMouseDown is also fired when the Scrollbar or ToggleButton of the Combobox is pressed. I tried something like this:
((ComboBox)obj).AddHandler(TextBox.PreviewMouseDownEvent, new RoutedEventHandler(CTRLMouseButtonEventHandler_Down2));
((ComboBox)obj).AddHandler(TextBox.PreviewMouseUpEvent, new RoutedEventHandler(CTRLMouseButtonEventHandler_Up2));
But it didn' work. Can somebody point me in the reight direction please?
Add another event. Set Event for hovering mouse over Combobox to give it selection. It would be same as clicking on it.
EDIT:
other possible events:
DropDownOpened
ContextMenuOpening
https://msdn.microsoft.com/en-us/library/system.windows.controls.combobox_events(v=vs.110).aspx
2.
KeyDown event is not firing when pressing enter in an UserControl
Just replace enter with space.

Windows Forms - get Text value from object of type button

I have a Windows form named Form1 and panel within this form named panel1. I use the panel only to place buttons there so that I can group them and work with them separately from the other buttons in my Form1. For the purpose of my program I need to handle every button click made from the buttons inside panel1. For this purpose I use the same code snippet:
public Form1()
{
InitializeComponent();
// Set a click event handler for the button in the panel
foreach (var button in panel1.Controls.OfType<Button>())
{
button.Click += HandleClick;
}
}
What I need to do is to have a way to identify which button exactly has been clicked. For this purpose I played a little bit with my handler method:
private void HandleClick(object o, EventArgs e)
{
MessageBox.Show("HI" + o.ToString());
}
which gave me some hope because I get this:
It's the second part - Text: button4 which is actually enough information to continue with my work. But I can't find a way to get this piece of information without some complicated string manipulations. So is there a way to get this or other unique information about the button been clicked given the way I have written my code?
private void HandleClick(object sender, EventArgs e)
{
var btn = sender as Button;
if (btn != null)
{
MessageBox.Show(btn.Text);
}
}
One option is to cast the object to a Button, but rather than doing the casting you can change how the event handler is assigned so that you don't need to cast in the first place:
foreach (var button in panel1.Controls.OfType<Button>())
{
button.Click += (_,args)=> HandleClick(button, args);
}
Then just change the signature of HandleClick to:
private void HandleClick(Button button, EventArgs e);
You need to cast sender to the Button class so you can access its properties:
Button b = (Button)sender;
MessageBox.Show(b.Text);

After F1 was pressed help is not displayed

I do not understand why i did not get the help message after pressing the F1 key. When on windows form i got for instance one button and it has the focus the message is displayed as expected (after pressing F1) but when i got an empty form this is not happening. I suppose that an empty form will have by default focus set on it. ( i read that this event will be raised after pressing F1 for the control which got the focus)
Is this the right behavior, or i am missing something about the "HelpRequested" event on an empty form ? Is this the right way to raise the event based on focus or it could be configured to be raised also on another event (something like onMouseOver) ? May i create my own event and raise it ? (i do not want to add a special button only for help, for example press this button and display the help, help should be displayed only after F1 was pressed).
This is the code:
private void Form1_Load(object sender, EventArgs e)
{
Form1.ActiveForm.HelpRequested += new HelpEventHandler(helpReq);
//button1.HelpRequested += new HelpEventHandler(helpReq);
}
private void helpReq(object sender, HelpEventArgs hlpevent)
{
MessageBox.Show(((Control)sender).Text);
}
using Form1.ActiveForm, is not recommended. Change it to this
there is no Text property associated with Control. Do you mean Tag?
after performing the help event, you should set the HelpEventArgs.Handled to true
instead of using events, you can just override OnHelpRequested in your form.
I would do someething like this:
protected override void OnHelpRequested(object sender, HelpEventArgs e)
{
MessageBox.Show((Control) sender).Tag);
e.Handled = true;
base.OnHelpRequested(sender, e);
}

Categories

Resources