Send an EventHandler to a RoutedEventHandleer - c#

here is the basic model of the code I have:
private void textBlock1_Tap (object sender, System.Windows.Input.GestureEventArgs e)
{
TextBox TextBox1 = new TextBox();
TextBlock tblk = (TextBlock)sender;
ApplicationBar = new ApplicationBar();
TextBox1.LostFocus += TextBox1_LostFocus;
ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/check.png", UriKind.Relative));
appBarButton.Text = "Accept";
ApplicationBar.Buttons.Add(appBarButton);
appBarButton.Click +=
}
void TextBox1_LostFocus(object sender, RoutedEventArgs e)
{
//do things here
}
I need to subscribe to the click Event, and when it is triggered, I need TextBox1_LostFocus to be called and TextBox1 to be sent as a parameter. Basically what I want to do is make appBarButton.Click do the exact same thing as TextBox1.LostFocus.
Problem is, LostFocus is a RoutedEventHandler and TextBox1_LostFocus takes a RoutedEventArgs as a parameter while appBarButton.Click is an EventHandler.
I'm not very experienced in coding at all so any help is much appreciated!

RoutedEventArgs inherits EventArgs.
You can add the same handler to both events.
Better yet, you can move the code to a function and call it from two different event handlers.

Related

Getting access to Button parent

I'm using a Button in a class. When the button is pressed, it should call a routine with the button's corresponding text. How do I convert the sender into a String_Entry? Also, I'm quite a newbie regarding object oriented/class programming, so comments are welcome.
public class String_Entry
{
public TextBox textbox;
public Button send;
// other stuff
public String_Entry()
{
textbox = new TextBox();
send = new Button();
send.Click += new System.EventHandler(this.bSend_Click);
// put in GUI, set parameters and other stuff
}
// other stuff
private void bSend_Click(object sender, EventArgs e)
{
// Trying to get the corresponding String_Entry from the Button click event
Button cntrl = (Button)sender;
String_Entry entry = (String_Entry)(cntrl.Parent);
parse.ProcessHexLine(entry);
}
}
Your solution of encapsulating a button with a textbox and the event handler is sound. It just goes wrong in the event handler:
private void bSend_Click(object sender, EventArgs e)
{
Button cntrl = (Button)sender;
String_Entry entry = (String_Entry)(cntrl.Parent);
parse.ProcessHexLine(entry);
}
Firstly, there is no point to doing anything with sender as it'll be the same as the field send. Next cntrl.Parent will give you a reference to the Form, or other container object, that contains the button, not this instance of String_Entry. To access that, use this. So you can change the event handler to:
private void bSend_Click(object sender, EventArgs e)
{
parse.ProcessHexLine(this);
}

handling multiple button actions in one action in C#

I have 70 buttons whose names are like button1, button2, button3 and so on.
My aim is that whenever button1 is clicked, it will say "1", button2 will say as "2" and so on for the others.
The code for button1 to speak is:
SpeechSynthesizer synthesizer = new SpeechSynthesizer();
private void button1_Click(object sender, EventArgs e)
{
synthesizer.Speak("1");
}
For button2
private void button2_Click(object sender, EventArgs e)
{
synthesizer.Speak("2");
}
and so on for other 68 buttons.
Now it is difficult to implement the 70 button's actions. These button actions follow a pattern - so can anyone suggest a more efficient way I can implement these button handlers to save me writing out 70 different actions?
Try something like this
button1.Tag = "1";
button2.Tag = "2";
...
private void button_Click(object sender, EventArgs e)
{
synthesizer.Speak(((Button)sender).Tag.ToString());
}
Use same handler for all buttons. Sender of event will be the button which raised event. You can get it's name and extract text to say:
private void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
string text = button.Name.Substring("button".Length);
synthesizer.Speak(text);
}
Create a single handler for all of the buttons.
Cast the sender parameter to get the Button instance that was clicked, and figure out what to do based on its Name, Text, or Tag.
Depending on your UI, you might want to generate all of those buttons in a loop, too.
You can subscribe them to a single eventhandler.
thus:
button1.Click += buttonClicked;
button2.Click += buttonClicked;
// and so on
and the code for the buttonClicked;
private void buttonClicked(object sender, EventArgs e)
{
//This will get the Type first, the name and then the last character on the Name
synthesizer.Speak(sender.GetType().Name.Substring(sender.GetType().Name.Length - 1, 1));
}
this promotes code reuse for you :)

Issue with LostFocus event of a TextBox

I'm trying to use this function:
private void IDCustTextBox_LostFocus(object sender, System.EventArgs e)
{
if (CustName.Text == "abc")
MessageBox.Show("Error");
}
When I type abc in CustName textbox, and then leave the textbox, I dont get any message. In the textbox properties I can see that "textbox.Changed" is using the event LostFocus.
How can I get this to show the Error message above?
There is no LostFocus event for textbox in property Window,if you want to use this then you must need to add event handler, there is textbox leave event in property window, that could be used as below:
private void textBox1_Leave(object sender, EventArgs e)
{
// do your stuff
}
for adding event handler you need to write the following:
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
then you can use it as below:
private void textBox1_LostFocus(object sender, EventArgs e)
{
// do your stuff
}
You will need to let the field know that there is a handler for the event LostFocus
Since this is not part of the properties window you will have attach the handler as such
CustTextBox.LostFocus += new EventHandler(IDCustTextBox_LostFocus);

Event handler arguments

I am using the following code to add buttons to a list:
for (int i=0; i < mov.Theat.Count(); i++)
{
StackPanel st=new StackPanel();
st.Orientation=System.Windows.Controls.Orientation.Horizontal;
st.HorizontalAlignment = HorizontalAlignment.Center;
TextBlock tx = new TextBlock();
tx.Text=mov.Theat[i];
st.Children.Add(tx);
TextBlock tx2=new TextBlock();
tx2.Text=mov.Time[i];
st.Children.Add(tx2);
Button test = new Button();
test.Width=450;
test.Content = st;
test.Click += new RoutedEventHandler(Button_Click);
theatlist.Items.Add(test);
}
As for the event handler, it is shown below:
void Button_Click(object sender, RoutedEventArgs e)
{
Theat_Data TD=(App.Current as App).Theat.First(theat => theat.Name=="");
PhoneApplicationService.Current.State["Theat"] = TD;
this.GoToPage(ApplicationPages.Theat);
}
I want to pass some variable about the selected button in the even handler so how can this be done? And if this is not possible, then what options do I have to identify the button and pass some data about it to the event handler?
I assume your business object is called Thead_Data and you would like to have access to this object from within your Click-method:
During creation, attach your data object to the DataContext or the Tag property:
Button test = new Button(){DataContext=Theat[i]};
Within your event handler, cast the DataContext or the Tag-property to your business-object:
void Button_Click(object sender, RoutedEventArgs e){
Button btn=(Button)sender;
Theat_Data td=(Theat_Data)button.DataContext;
...
}
You can access the sender like this:
void Button_Click(object sender, RoutedEventArgs e)
{
var clickedButton = sender as Button;
}
I'm not sure I completely understand what you are asking, but the "sender" param of the event handler is the button. You should just be able to cast it:
Button b = (Button)sender;
You could write your own event handler and add your custom data in the event args.

Add events to controls that were added dynamically

I am working on a winforms app and I have added some controls dynamically (eg. Button). I want to add an event to that created button; how can I perform this? Also, can someone refer a C# book to me which covers all winforms topics?
// create some dynamic button
Button b = new Button();
// assign some event to it
b.Click += (sender, e) =>
{
MessageBox.Show("the button was clicked");
};
// add the button to the form
Controls.Add(b);
I totally agree with Darin's answer, and this is another syntax of adding dynamic event
private void Form1_Load(object sender, EventArgs e)
{
Button b = new Button();
b.Click += new EventHandler(ShowMessage);
Controls.Add(b);
}
private void ShowMessage(object sender,EventArgs e)
{
MessageBox.Show("Message");
}

Categories