Event handler arguments - c#

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.

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

How to make a button event handler Method in C#?

I am new to C# and I am making a small tic-tac-toe game using WPF. However, I would like to make my code compact and would like to create a method which will take button click event as argument and modify the button. For the time being, this is what I am trying to do:
Button.IsEnabled = false;
Button.Content = "X";
I want to make a event which will do this job when called inside a button click event and hence I will not need to copy this code for every button.
IN the xaml, add the event handler to the button:
<Button Click="Button_Click" />
(add the same handler to all buttons)
in the code behind, cast the sender to a button:
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (button != null)
{
button.IsEnabled = false;
button.Content = "X";
}
}
Attach the event handler and use the sender:
Button.Click += this.Button_Click;
private void Button_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
if (button != null)
{
button.IsEnabled = false;
button.Content = X;
}
}

How to add event handler for dynamically created controls at runtime?

I am working on C# windows application. my application get controls(button,text box,rich text box and combo box etc)from custom control library and placed them into form dynamically at run time. how i create event handler for that controls using delegate? and how to add business logic in particular custom control click event?
For Example:
i have user1, user2, user3, when user1 log in i want to show only "save" button. when user2 then only show "add and delete" buttons and user 3 only show "add and update" buttons.text boxes and button created as per user log in information taken from DB tables.in this scenario how i handle different event(adding,saving,updating,deleting) for button save,add,delete and update for different users when form is dynamically created controls(save,add,delete and update button object is from same button class)
With anonymous method:
Button button1 = new Button();
button1.Click += delegate
{
// Do something
};
With an anonymous method with explicit parameters:
Button button1 = new Button();
button1.Click += delegate (object sender, EventArgs e)
{
// Do something
};
With lambda syntax for an anonymous method:
Button button1 = new Button();
button1.Click += (object sender, EventArgs e) =>
{
// Do something
};
With method:
Button button1 = new Button();
button1.Click += button1_Click;
private void button1_Click(object sender, EventArgs e)
{
// Do something
}
Further information you can find in the MSDN Documentation.
var t = new TextBox();
t.MouseDoubleClick+=new System.Windows.Input.MouseButtonEventHandler(t_MouseDoubleClick);
private void t_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
throw new NotImplementedException();
}
It's adding double click eventhandler to new TextBox
I believe you could do something like this:
if (userCanAdd)
container.Controls.Add(GetAddButton());
if (userCanUpdate)
container.Controls.Add(GetUpdateButton());
if (userCanDelete)
container.Controls.Add(GetDeleteButton());
private Button GetAddButton() {
var addButton = new Button();
// init properties here
addButton.Click += (s,e) => { /* add logic here */ };
// addButton.Click += (s,e) => Add();
// addButton.Click += OnAddButtonClick;
return addButton;
}
private void OnAddButtonClick (object sender, EventArgs e) {
// add logic here
}
// The other methods are similar to the GetAddButton method.

Send an EventHandler to a RoutedEventHandleer

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.

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