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.
Related
I want to add an event to a programmatically generated button like this:
Button activityButton = new Button();
activityButton.Click += new EventHandler(onChangeActivityFilter);
I'm getting the following exception in the 2nd line:
Cannot implicit convert type System.EventHandler to System.Windows.RoutedEventhandler
The onChangeActivityFilter methode looks like this:
private void onChangeActivityFilter(object sender, EventArgs e)
{
}
I'd like to know what I'm doing wrong.
You need to create a instance of RoutedEventHandler:
activityButton.Click += new RoutedEventhandler(onChangeActivityFilter);
And also change the method signature:
private void onChangeActivityFilter(object sender, RoutedEventArgs e)
{
}
RoutedEvents where introduced with WPF.
You can also use lambda functions
activityButton.Click += (sender, e) =>
{
MessageBox.Show("the button was clicked");
};
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);
}
On form1, I have registerButton that create new registerForm with an acceptButton on it. Both dynamically created:
private void registerButton_Click(object sender, EventArgs e)
{
registerButton.Enabled = false;
Form registrationForm = new Form();
registrationForm.Text = "Register new account";
registrationForm.Visible = true;
Button createButton = new Button();
createButton.Text = "Accept";
registrationForm.Controls.Add(createButton);
createButton.Click+= new EventHandler(createButton_Click);
}
How can I close registerForm after clicking acceptButton without closing the form1?
You've lost the reference to the registration form instance. But you can always get it back from the sender argument that's passed to the Click event handler. Like this:
private void registrationButton_Click(object sender, EventArgs e) {
var btn = (Control)sender;
btn.FindForm().Close();
}
For your code (which I don't recommend to use) fix will be
createButton.Click += (s,e) => registrationForm.Close();
When you attach this lambda as event handler, you have opportunity to capture registrationForm instance in a closure. Thus form instance will be available when click event will happen, and you will be able to close this form.
Better approach: instead of adding button dynamically to form, place this button statically in designer and attach click event handler which will close the form:
private void acceptButton_Click(object sender, EventArgs e)
{
Close();
}
Usage of registration form will be simple as:
private void registerButton_Click(object sender, EventArgs e)
{
Form registrationForm = new Form();
registrationForm.Show();
}
Add this.Close() at the click event of acceptButton.
You can attach an event to it dynamically. While creating the button, do acceptButton.Click += new System.EventHandler(accepButton_click); and create matching function or press Tab twice after doing the +=.
I need to generate a dynamic list of buttons, I already did, with an event handler attached to it.
However the event handler is not being executed.
private void GetOptions(EcoBonusRequest request)
{
var ecobonuswworkflow = WorkflowFactory.CreateEcobonusWorkflow();
ecobonuswworkflow.SetCurrentStep(request.CurrentStatus);
var currentoptions = ecobonuswworkflow.GetCurrentOptions();
foreach(var option in currentoptions)
{
var btn = new Button() {Text = option.OptionName};
btn.Click +=new EventHandler(btn_Click);
Buttons.Controls.Add(btn);
}
}
void btn_Click(object sender, EventArgs e)
{
var btn = (Button) sender;
string command = btn.Text;
EcoBonusRequest request = this.GetDBRequest(RequestBaseId.Value);
EcoBonusRequestBL.AddWorkflowHistoryItem(request, command,CurrentUser, command);
}
The controls that you add dynamically in your page must be added in Page_init event, and they must have unique Ids. If you are adding textboxes or some other controls where user can input or change value, than on every post back when these controls are re-added they must have same IDs.
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");
}