Cannot get KeyPressEventHandler in usercontrol - c#

From the site that I saw on how to create a user control of keypad Click here for the link
. I successfully added the control in the tools. and drag one in the Form UI. when i start clicking the number. the RaiseButton is triggered:
private void btn1_click(object sender,eventargs e)
{
RaiseButton('1');
}
and on the other usercontrol.
There is this method
public event KeyPressHandler IsPressed;
public void RaiseButton(char Tosend)
{
KeyPressEventHandler handle = IsPressed;
handle(this,new KeypressEventArgs(Tosend));
}
The Class KeyPressHandler Ispressed value is null
so it cannot write to textbox. Now my question is, did I miss something that makes keypresseventhandler null?
Please advise thanks

The IsPressed event is null because nothing is registered for it. Once you register for that event it will no longer be null. Check for the event being null first. Also, you don't need to assign it to a variable.
public void RaiseButton(char Tosend)
{
if (IsPressed != null)
{
IsPressed(this,txtbox.text);
}
}

I solved it now. I was so stupid the event that I used in the form is not the same event that I used in the user control. The public event KeyPressEventHandler IsPressed event in the user control must be use also in your form. To be able to register this one, On your form where your you want to use the control. Click the user Control and look in the event property or just click the thunder symbol in your property. Look for the event IsPressed and now your good to go. double click and put the code that you want to do for the event. #wlemond thanks for giving me the example.
Cheers :)

Related

Button Click event is getting called for holding event

I have used command binding for the click event of the button. Now i also have a holding event on the button. So whenever i do holding on the button, click is also getting called along with holding event handler. I have tried setting
e.handled = true;
but that doesn't work. Any suggestions on why both the events are getting detected. If i use Tapped event instead of command binding, everything work fine. But my requirement is to use command binding for click event.
Edit: Below are some code behind
Code:
xaml :
Button Command={Binding ButtonClicked} Holding="Button_Holding"
xaml.cs
private void Button_Holding(object sender, HoldingRoutedEventArgs e)
{
}
ViewModel has the ButtonClicked Command
I am not an Expert in this stuff but i guess that Holding is a bubbling event and ButtonClick is a direct event(If somebody knows that for sure i would love to read a comment)
So your e.handled = true; doesn't apply to the ButtonClicked event.
I would say that you have to let your button clicked command know that the hold event was fired first and then ignore that event via the CanExecute method, or whatever you named it, or at least do something like
public void YourButtonClickedMethod()
{
if(SomeObject.IsHoldAllreadyExecuted)
{
SomeObject.IsHoldAllreadyExecuted = false; //set it to false for the next run
}
else
{
//DO the stuff you want to
}
}

wpf click button in usercontrol calls event in parent window

This must have been asked before, but I am fairly new and don't quite know how to express myself...
1) I have a UserControl that basically acts as a toolbar. I re-use the toolbar in each window, hence the need for a uc.
2) The toolbar is filled with buttons
3) the usercontrol doesn't act on the button (no code), but it should pass the event back to the parent window so the code in the parent window fires up.
How can I do this? Is this a routed event? any sample code in vb.net would be appreciated!
On you user control, you need events that you can fire when the buttons are clicked. Then in your form, you handle the events just like you do for every other control. IE:
public event Button1_ClickedEventHandler Button1_Clicked;
public delegate void Button1_ClickedEventHandler(object sender);
private void Button1_Click(object sender, EventArgs e)
{
if (Button1_Clicked != null) {
Button1_Clicked(this);
}
}
You can call the event whatever you want and pass whatever you want. Here you will notice I am NOT sending the button but THIS, which in this case should be the User Control.

How to check whether control is button or not?

How can I check that whether mouse pointer is pointing a button or some other control?
I want to perform a particular task when mouse hover/move a button.
I know I can set event on individual button. But isn't it possible to check the pointed/hover control is button?
The sender argument in an event method should have the information you need...
private void MyEventHandler(object sender, EventArgs args) {
if(sender is Button) {
//Do some stuff
}
}
I'm not sure if you mean: can I do this without event handlers for MouseHover in individual controls. If so, the answer is no.
But you can attach each contol's MouseHover event to just one event handler that could look like the one in Chris's answer. For convenience you could even do that programmatically by looping through the controls in the form's load event. (assuming this is winforms)

wpf combo change event triggered when form loads

I am having a problem, when window loads, the 'selection_change' event associated with 'combo box' control is getting triggered when the window loads first time. Why its occuring and How to restrict it please?
Regards
With that code, the SelectionChanged event won't get raised. Create a new project, paste it and try it for yourself.
My guess is pretty much the same as Sekhar_ Pro's, you're populating your ComboBox from code behind, and something in there causes the SelectedItem to change.
Investigate the cmbUsers.SelectedItem in the cmbUsers_SelectionChanged event handler to see if it has some value or is null in the debugger. Also, look in the Call Stack to find what caused this event to be raised.
Example code
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
cmbUsers.Items.Add(new ComboBoxItem { Content = "Test" });
cmbUsers.SelectedIndex = 0;
}
private void cmbUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (cmbUsers.SelectedItem != null)
{
MessageBox.Show(cmbUsers.SelectedItem.ToString());
}
}
}
The Call Stack looks like this for me in the event handler
This is not a normal Behavior, some where you must be doing something like setting SelectedItem, etc which in turn is triggering the event. Check thorough your form's life-cycle events and see if you are doing something like this, may be in Load or Activate event or somewhere in the Constructors.

C# TreeView SetFocus firing leave event twice

I have this simple code, where when the user leaves the TextBox control, TreeView gets focused:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.treeView1.Nodes.Add("A");
this.treeView1.Nodes[0].Nodes.Add("A.A");
this.treeView1.Nodes.Add("B");
this.treeView1.Nodes[0].Nodes.Add("B.A");
}
private void textBox1_Leave(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Leave..");
this.treeView1.Focus();
}
}
If we execute this code the Leave event is fired twice:
Leave..
Leave..
But if we set focus to other control, only one Leave event is fired.
Is that a problem of the TreeView? Do you know any workaround? Should we report this to Microsoft?
Thanks,
RG
this.treeView1.Focus();
Do not use the Focus() method in an event handler that's called because of a focusing event, like Leave. If you need to prevent a focus change then use the Validating event instead. Setting e.Cancel = true stops it.
But do note that this isn't very logical to do so for a TreeView, there isn't anything the user can do to alter the state of the control. You'll trap the user. Maybe that was the intention, do make sure the user can still close the window. If not then you might need the FormClosing event to force e.Cancel back to false.
Given that there is no code there to wire up the event I'm guessing you did it from the designer which means a line of code such as
textBox1.Leave += new EventHandler(textBox1_Leave);
will have been added to the Form1.designer.cs, check this file to ensure the line doesn't exist more than once as for each time this line is run you will get an event trigger, so if you run the line 3 times the Leave event will fire 3 times when you leave the textbox!
HTH
OneShot

Categories