Issue with LostFocus event of a TextBox - c#

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

Related

How to catch if cell Value got changed in DataGridView as sender

I haven't found something that matches my problem so I ask it here. I have some code which belongs to a Textbox:
if ((sender as TextBox).Text == form1.filterType())
{
//Do something
}
This comes from the TextBox TextChanged event. So when the TextChanged Event gets fired it calls a method which has the if-construct above and recognizes that the textchanged event came from the textbox.
Now I want exactly the same just when someone writes into a cell in a DataGridView (not when it's just clicked - when the content changes).
How to do this correctly and which event fires when the content changes in a cell without leaving the cell?
I have found a solution for this:
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
TextBox tb = (TextBox)e.Control;
//"unwire" the event before hooking it up ensures the event handler gets called only once
tb.TextChanged -= new EventHandler(tb_TextChanged);
tb.TextChanged += new EventHandler(tb_TextChanged);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("changed");
}
Now it fires everytime the value in the cell gets changed - it behaves like a textbox.
Now - I still need a solution for the "if-construct" above. How to do this exactly? I've casted the cell into a textbox but now? Does this still comes from the dataGridview?
Once I had a similar problem an solved it this way (think there are better ones but it worked)
private void DataGridView1_onFocus ( Object sender, EventArgs e)
{
DataGridView1.onKeyPress+=DataGridView1_onKeyStroke;
}
private void DataGridView1_onFocusLost( Object sender, EventArgs e)
{
DataGridView1.onKeyPress-=DataGridView1_onKeyStroke;
}
private void DataGridView1_onKeyStroke( Object sender , EventArgs e)
{
//Do your thing
}

FireTextChanged event of TextBox B when TextChanged event of TextBox A occurs

I have two TextBoxes: A and B.
I want B to imitate the behavior of A. So whenever A's text changes, B's text changes too.
This is doable by setting B's text to A's text whenever A's text changes. But since B has an AutoComplete option, this AutoComplete will not work unless B's TextChange event is fired, too.
So with each TextChange in A, I want to fire a TextChange in B.
How can I achieve this?
void TextChanged_A(object sender, EventArgs e)
{
//Do Anything
//Then
TextChanged_B(B,null);
}
void TextChanged_B(object sender, EventArgs e)
{
//Do Anything
}
An easy way to do is the TextChanged Event Of TextBox 1.
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}

Prevent Event Handler

I have a Gridview. I and populating two dynamic text box for each cell inside it. User will enter the arriving time in first textbox and the arriving + 9 hours will be added and display in second textbox. I have written event handler where i am calculating exit time. the event handler is working fine but I need to event handler will fire for first Cell only. How to prevent event handler for rest of the textbox.
You didn't provide code but this is a general example.
private bool _isFirst = true;
private void CellEventHandler(object sender, EventArgs e)
{
if (!_isFirst) return;
// code
_isFirst = false;
}
You could also unbind the event handler
private void CellEventHandler(object sender, EventArgs e)
{
// your code here
textBox.Click -= CellEventHandler;
}

Why it is not Possible to call textBox1_KeyPress() event from some Button Click event.?

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
MessageBox.Show("called btn 0 click..");
KeyPressEventArgs e0 = new KeyPressEventArgs('0');
textBox1_KeyPress(sender, e0);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show("called txtbox_keypress event...");
}
}
Am sorry if this is a silly question,I have just started to learn windows forms, I still find material on the internet confusing.I want to implement calculator. So when number button is pressed it should be filled in textbox. So I thought calling textBox1_keypress() event from button click event would work??? but its not working,
I can manually write the logic in button click event to fill text in text box but if i do so, i have to do the same thing in button1_KeyPress event too. so it would be duplication of code right??...so i thought solution was to call textBox1_KeyPress() event from both button click event and button key press event...but its not working .So what should i do???..is there any other approach which should i follow.
so it would be duplication of code right??
Yes, it would be. So you can do
private void btn0_Click(object sender, EventArgs e)
{
CommonMethod(e);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
CommonMethod(e);
}
private void CommonMethod(EventArgs e)
{
//Your logic here.
}
The TextBox KeyPress event handler (textBox1_KeyPress) is called after the user presses a key. The KeyPressEventArgs parameter includes information such as what key was pressed. So calling it from your btn0_Click method isn't going to set the text for the TextBox.
Rather, you want to (probably) append whatever number the user pressed to the text already present in the TextBox. Something like
private void btn0_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
might be closer to what you're trying to accomplish.
You could put the logic in an extra function like so:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
NumberLogic(0),
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// I don't know right now if e contains the key pressed. If not replace by the correct argument
NumberLogic(Convert.ToInt32(e));
}
void NumberLogic(int numberPressed){
MessageBox.Show("Button " + numberPressed.ToString() + " pressed.");
}
}
You don't want to tie the events together like that.
A key-press is one thing, handled in one way.
A button click is something totally different and should be handled as such.
The basic reason is this,
The button doesn't know what number it is, you need to tell it that.
A key-press on the other hand, knows what number was pressed.
If you REALLY want to, for some reason, you could use SendKeys to trigger your key-press event in a round-about way, from the button.
SendKeys.SendWait("0");
I can suggest to you to use an Tag Property of the Buttons. Put in it the value of each button in Design mode or in Constructor, create one button event handler for all buttons and use Tag value:
Constructor:
button1.Tag = 1;
button2.Tag = 2;
button1.Click += buttons_Click;
button2.Click += buttons_Click;
Event hadler:
private void buttons_Click(object sender, EventArgs e)
{
textBox1.Text = ((Button)sender).Tag.ToString();
}

Trigger control's event programmatically

Assume that I have a WinFoms project. There is just one button (e.g. button1).
The question is: is it possible to trigger the ButtonClicked event via code without really clicking it?
Button controls have a PerformClick() method that you can call.
button1.PerformClick();
The .NET framework uses a pattern where for every event X there is a method protected void OnX(EventArgs e) {} that raises event X. See this Msdn article. To raise an event from outside the declaring class you will have to derive the class and add a public wrapper method. In the case of Button it would look like this:
class MyButton : System.Windows.Forms.Button
{
public void ProgrammaticClick(EventArgs e)
{
base.OnClick(e);
}
}
You can just call the event handler function directly and specify null for the sender and EventArgs.Empty for the arguments.
void ButtonClicked(object sender, EventArgs e)
{
// do stuff
}
// Somewhere else in your code:
button1.Click += new EventHandler(ButtonClicked);
// call the event handler directly:
ButtonClicked(button1, EventArgs.Empty);
Or, rather, you'd move the logic out of the ButtonClicked event into its own function, and then your event handler and the other code you have would in turn call the new function.
void StuffThatHappensOnButtonClick()
{
// do stuff
}
void ButtonClicked(object sender, EventArgs e)
{
StuffThatHappensOnButtonClick();
}
// Somewhere else in your code:
button1.Click += new EventHandler(ButtonClicked);
// Simulate the button click:
StuffThatHappensOnButtonClick();
The latter method has the advantage of letting you separate your business and UI logic. You really should never have any business logic in your control event handlers.
Yes, just call the method the way you would call any other. For example:
private void btnSayHello_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello World!");
}
private void btnTriggerHello_Click(object sender, EventArgs e)
{
btnSayHello_Click(null, null);
}
button1.PerformClick();
But if you have to do something like this maybe it's better to move the code you have under the event on a new method ?
Why don't you just put your event code into a Method. Then have the Event execute the method. This way if you need to execute the same code that the Event rises, you can, but simply just calling the "Method".
void Event_Method()
{
//Put Event code here.
MessageBox.Show("Hello!");
}
void _btnSend_Click(object sender, EventArgs e)
{
Event_Method();
}
void AnotherMethod()
{
Event_Method();
}
Make sense? Now the "Click" event AND anywhere in code you can trigger the same code as the "Click" event.
Don't trigger the event, call the method that the event calls. ;)
In most cases you would not need to do that. Simply wrap your functionality in functions related to a specific purpose (task). You call this function inside your event and anywhere else it's needed.
Overthink your approach.
I recently had this problem where I wanted to programatically click a button that had multiple event handlers assigned to it (think UserControl or derived classes).
For example:
myButton.Click += ButtonClicked1
myButton.Click += ButtonClicked2;
void ButtonClicked1(object sender, EventArgs e)
{
Console.WriteLine("ButtonClicked1");
}
void ButtonClicked2(object sender, EventArgs e)
{
Console.WriteLine("ButtonClicked1");
}
When you click the button, both functions will get called. In the instances where you want to programmatically fire an event handler for a function from a form (for example, when a user presses enter in a Text field then call the InvokeOnClick method passing through the control you. For example
this.InvokeOnClick(myButton, EventArgs.Empty);
Where this is the Form instance you are in.
use a for loop to call the button_click event
private void btnadd_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i <= 2; i++)
StuffThatHappensOnButtonClick();
}
void StuffThatHappensOnButtonClick()
{
........do stuff
}
we assume at least one time you need click the button

Categories