I know how to programmatically invoke the event handler of a Button:
button1.PerformClick();
I would like to do the same for the Click event handler of a TextBox. The problem is that TextBox does not have a
textBox1.PerformClick();
I suggest method extraction (why should we mix UI - windows messages and Business Logic):
//TODO: put a better name here
private void onMyTextBoxClick() {
//TODO: relevant code here
}
private void MyTextBox_Click(object sender, EventArgs e) {
onMyTextBoxClick();
}
Then you can just call onMyTextBoxClick:
...
// Same business logic as if MyTextBox is clicked
onMyTextBoxClick();
...
Edit: If you really want EventArgs aruments, just provide them:
//TODO: put a better name here
private void onMyTextBoxClick(TextBox box, EventArgs e) {
//TODO: relevant code here
}
// Default EventArgs
private void onMyTextBoxClick(TextBox box) {
onMyTextBoxClick(box, EventArgs.Empty);
}
// Both TextBox and EventArgs are default ones
private void onMyTextBoxClick() {
onMyTextBoxClick(MyTextBox, EventArgs.Empty);
}
private void MyTextBox_Click(object sender, EventArgs e) {
onMyTextBoxClick(sender as TextBox, e);
}
Usage:
// Default EventArgs
onMyTextBoxClick(myTextBox);
// Custom EventArgs
EventArgs args = ...
onMyTextBoxClick(myTextBox, args);
Related
In my C# form I have two buttons
button1.Hide()
private void button2_Click(object sender, EventArgs e)
{
button1.PerformClick();
}
The button1 is hidden at form loading, I want the logic behind button1 to be perfomed when it's hidden too.
Just let the function outside become another function, then you can call function although you hidden the button1.
private void button1(object sender, EventArgs e)
{
_button1();
}
private void button2(object sender, EventArgs e)
{
_button1();
}
//Here is the function
void _button1()
{
...
}
If your Button is hidden, it seems that you need the functionality behind not or just in special cases. Keeping functionality out of events is often a simple solution to avoid problems in the future.
private void btn_Reload_Click(object sender, EventArgs e)
{
// reload here - maybe you reload all your employees from a datasource
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
// you can use functionality here from a another button and call the
btn_Reload_Click(this, EventArgs.Empty); // DON'T DO THIS IN MY OPINION
// ....
}
Maybe this solution is better even if you need the functionality at other workflows.
private void btn_Reload_Click(object sender, EventArgs e)
{
Reload();
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
Reload();
Calculate();
}
void Reload() { }
void Calculate() { }
I am trying to call a OnCellEditEnding event from another event,
private void BillsTableRecords_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
// do stuff here
}
My issue is I don't know how to pass the DataGridCellEditEndingEventArgs into the method, i.e. the e in the below method obviously gives an error as it is referencing RoutedEventArgs not DataGridCellEditEndingEventArgs.
private void BillsRecordsCheckBox_OnChecked(object sender, RoutedEventArgs e)
{
BillsTableRecords_OnCellEditEnding(sender, e);
}
So how do obtain the value from DataGridCellEditEndingEventArgs so that I can pass the value in the method? Please note that the DataGrid cell with be selected at this point so it will contain a value.
I wouldn't recommend this approach. Event handlers are to be called by events; their signature does not really fit for a standalone call. In case you execute business code in your event handler, it is also not good design, because your event handlers are UI code, which should be separated from business code.
The best way to go here is to create a dedicated method that does what you want and call it from both event handlers:
private void DoStuff(/* add the parameters you need*/) {
//do stuff
}
private void BillsTableRecords_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DoStuff();
}
private void BillsRecordsCheckBox_OnChecked(object sender, RoutedEventArgs e)
{
DoStuff();
}
try that
private void BillsRecordsCheckBox_OnChecked(object sender, RoutedEventArgs e)
{
BillsTableRecords_OnCellEditEnding(sender, new DataGridCellEditEndingEventArg());
}
If you want to keep the arguments from the RoutedEventArgs, add them to the constructor of DataGridCellEditEndingEventArg
I've created a custom user control for a windows form that will operate similar to a button (and please don't suggest that I just use a button, because I will be storing data in this user control), but I can't figure out how to get the OnClick() event to fire. I've sifted through a handful of tutorials and looked at a few similar questions on the site, but I can't seem to get the event to fire off - so I'm either doing something wrong or everyone posted incorrect code (I hope it isn't the latter)
In my custom control.cs,
namespace MobCreator {
public partial class MOBSample : UserControl {
public MOBSample() {
InitializeComponent();
}
protected override void OnMouseUp(MouseEventArgs e) {
this.BorderStyle = BorderStyle.FixedSingle;
base.OnMouseUp(e);
}
protected override void OnMouseDown(MouseEventArgs e) {
this.BorderStyle = BorderStyle.Fixed3D;
base.OnMouseDown(e);
}
public event EventHandler ButtonClick;
private void OnButtonClick(object sender, EventArgs e) {
// invoke UserControl event here
if (this.ButtonClick != null) this.OnButtonClick(sender, e);
}
}
}
And in my form.cs,
private void MobCreatorForm_Load(object sender, EventArgs e) {
UserControl1.ButtonClick += new EventHandler(this.CustomEvent_Handler);
}
private void CustomEvent_Handler(object sender, EventArgs e) {
Console.WriteLine("Click");
}
However, when I run the program my console never outputs "Click".
Check this link on MSDN: it is a simple Event tutorial, you should be able to adapt it to your scenario.
At a first look, what you are probably missing is a Delegate for your event.
Try this
private void MobCreatorForm_Load(object sender, EventArgs e)
{
CustomEvent_Handler(null,null);
}
private void CustomEvent_Handler(object sender, EventArgs e)
{
Console.WriteLine("Click");
}
In C# , Winform, I have created a form and bunch of UI controls on it. I have changed the name of the controls through Properties windows but the following automated generated code did not update automatically. However, the InitializeComponent code is automatically updated though. My problem is now that I don't remember which box or whihc label I renamed to certain name.. Two questions : How could I have done this more efficiently to begin with? Question 2) Is there anything I could do now to make it automatically change the corresponding names? I have heard of refactoring but I don't know if I could have used it here and how? I appreciate any help.
public partial class frmMyInterface : Form
{
public frmMyInterface()
{
InitializeComponent();
}
private void frmMyInterface_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
Rename each these event handlers and then on the property window, reassign the events selecting from the dropdrown. Or delete these event handlers and double click on each event in the property window and this time it will update it for you
I am trying to figure out how to make it that when my timer ticks, it performs a bidder00_TextChanged, or something like that.
Is this even possible to do? and if it isn't, is there any other way to do it?
I tried to search Google for it but i didn't get any results, if you find anything that i missed please post it here.
I don't really have any code but here it is:
private void bidder00_TextChanged(object sender, EventArgs e)
{
if (bidder00.Text == addbidder1.Text)
{
bidBtn1.PerformClick();
}
}
That is my TextChanged Event
My timer doesn't have any code because it is going to perform the bidder00_TextChanged Event.
You could create a method Perform() and call it from within your event handlers :
private void timer1_Tick(object sender, EventArgs e)
{
Perform();
}
private void bidder00_TextChanged(object sender, EventArgs e)
{
Perform();
}
private void Perform()
{
if (bidder00.Text == addbidder1.Text)
{
bidBtn1.PerformClick();
}
}
I assume you have coupled your actual logic with your click event which is not a good idea. Separate the code out into a separate function and have both parts of the application call the same code e.g.
private void SubmitBid()
{
// code you want to execute
}
private void OnSubmitBid()
{
// confirm whether we can actually submit the bid
if (bidder00.Text == addbidder1.Text)
{
SubmitBid();
}
}
private void Timer1_OnTick(object sender, EventArgs e)
{
// trigger code from timer
OnSubmitBid();
}
private void bidder00_TextChanged(object sender, EventArgs e)
{
// trigger code from text change
OnSubmitBid();
}
private void btnBid_Click(object sender, EventArgs e)
{
// trigger code from button press
OnSubmitBid();
}
Notice all the UI controls trigger the same code. There is an extra call in there for the text control validation (i.e. OnSubmitBid()) - if this wasn't required then you would just call SubmitBid directly.