In my C# Windows form have one textbox and one datatimePicker.This textbox has Leave Event and
when textbox leave event occured,I want to fill value in datetimepicker. This datetimepicker also has ValueChanged Event.
Now my problem is when fill value in datetimepicker from textbox leave event,same time datetimepicker ValueChanged Event also occured.
So textbox leave event occur again.
In this problem, I don't want to occur datetimepicker Valuechanged Event from textbox Leave Event.
This is sample for my code.
private void textbox_Leave(object sender, EventArgs e)
{
datetimepicker.Value = System.DateTime.Now.ToString("dd/MM/yyyy").Substring(0, 10);
}
private void datetimepicker_ValueChanged(object sender, EventArgs e)
{
//do something
}
Thanks for help. Sorry for my English.
You may use
or a boolean variable, that indicates that the value to DateTimePicker is set from outside, and if appears its ValueChangedEvent, just don't care.
//PSEUDO CODE
bool setDateFromOutside = false; //GLOBAL VARIABLE
public void TextBox_Leave(...){
setDateFromOutside = true; //SET TRUE
dateTimePicker.Value = newDate; //SOME VALUE
setDateFromOutside = false; //SET TRUE
}
public void DateTimePicker_ValueChanged(...){
if(setDateFromOutside)
return;
}
or before setting the value to DateTimePicker unsubscribe from its ValueChangedEvent, and subscribe again after.
Now I resolve my problem by this.
private void textbox_Leave(object sender, EventArgs e)
{
datetimepicker.ValueChanged -= datetimepicker_ValueChanged;
datetimepicker.Value = System.DateTime.Now.ToString("dd/MM/yyyy").Substring(0, 10);
datetimepicker.ValueChanged += datetimepicker_ValueChanged;
}
private void datetimepicker_ValueChanged(object sender, EventArgs e)
{
//do something
}
Related
I have a Windows Form, DataGridView and two buttons.
When I will press the button1 it changes a value of RowHeadersVisible to true.
When I will press the button2 it changes a value of RowHeadersVisible to false.
public Form1()
{
InitializeComponent();
dataGridView1.RowHeadersVisible = false;
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.RowHeadersVisible = true;
}
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.RowHeadersVisible = false;
}
I cannot find any kind of events about "RowHeadersVisible" value changing in "DataGridView" class. As I mentioned "CellFormatting" event works for this action but it appears often, almost for all kind of action made in datagridview1.
I think we might create a custom event handler in order to make different decisions.
When "RowHeadersVisible" changes the value to false I need to call another function inside "CustomEvent".
private void CustomEvent(object sender, EventArgs e)
{
SomeFunction();
}
On the other hand "DataGridTableStyle" class has the event "RowHeadersVisibleChanged".
So How to solve this problem?
.NET 4.5, you should get help.
Dissolve it in the following way.
Represents the table drawn by the System.Windows.Forms.DataGrid control at run time.
// Instantiate the EventHandler.
public void AttachRowHeaderVisibleChanged()
{
myDataGridTableStyle.RowHeadersVisibleChanged += new EventHandler (MyDelegateRowHeadersVisibleChanged);
}
// raise the event when RowHeadersVisible property is changed.
private void MyDelegateRowHeadersVisibleChanged(object sender, EventArgs e)
{
string myString = "'RowHeadersVisibleChanged' event raised, Row Headers are";
if (myDataGridTableStyle.RowHeadersVisible)
myString += " visible";
else
myString += " not visible";
MessageBox.Show(myString, "RowHeader information");
}
// raise the event when a button is clicked.
private void myButton_Click(object sender, System.EventArgs e)
{
if (myDataGridTableStyle.RowHeadersVisible)
myDataGridTableStyle.RowHeadersVisible = false;
else
myDataGridTableStyle.RowHeadersVisible = true;
}
In the first column of my datagridview, I have checkboxes and I want to fire an event each time the status of the checkbox is changed. I thought of using the cellcontentclick event, casting the sender object to datagridviewcell and checking by its column index. But I found out that the sender object is a datagridview object. So, how to perform the desired operation?
To handle CheckBoxCell value changed you have to use this event CellValueChanged. Sender in events will always be the control that raised the event. To have more information about what happend you need to take a look into EventArgs.
Back to handling CheckBoxCell do this:
private void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
var dgv = sender as DataGridView;
var check = dgv[e.ColumnIndex, e.RowIndex].Value as bool?;
if (check.HasValue)
{
if (check)
{
//checked
}
else
{
//unchecked
}
}
}
Hope this helps :)
There are many methods
One Method is :
You can take a hidden field or viewstate on page in which you can store row id when click occured by javascript and then in code behind get that hiddenfield value.
Other one :
You can use CommandName & CommandArgument and in code behind use datagridview_ItemCommand
private void dgvStandingOrder_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvStandingOrder.Columns[e.ColumnIndex].Name == "IsSelected" && dgvStandingOrder.CurrentCell is DataGridViewCheckBoxCell)
{
bool isChecked = (bool)dgvStandingOrder[e.ColumnIndex, e.RowIndex].EditedFormattedValue;
if (isChecked == false)
{
dgvStandingOrder.Rows[e.RowIndex].Cells["Status"].Value = "";
}
dgvStandingOrder.EndEdit();
}
}
private void dgvStandingOrder_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dgvStandingOrder.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void dgvStandingOrder_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dgvStandingOrder.CurrentCell is DataGridViewCheckBoxCell)
{
dgvStandingOrder.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
I have a textbox and in some cases in Enter event I need to set the focus to a different textbox.
I tried that code:
private void TextBox1_Enter(object sender, EventArgs e)
{
if(_skipTextBox1) TextBox2.Focus();
}
But this code doesn't work. After that I found on MSDN:
Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers.
So how can I do it other way?
Postpone executing the Focus() method until after the event is finished executing. Elegantly done by using the Control.BeginInvoke() method. Like this:
private void textBox2_Enter(object sender, EventArgs e) {
this.BeginInvoke((MethodInvoker)delegate { textBox3.Focus(); });
}
You could handle the KeyPress event instead:
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
TextBox2.Focus();
}
}
textBox.Select();
or
textBox.Focus();
or
set TabIndex = 0 from properties of that textBox.
both methods are use to set focus on textBox in C#, .NET
I'm a beginner with C# and I'm developing a basic application.
I want to check if the value of a textbox is a number with the following code :
private void check_value(object sender)
{
TextBox tb = (TextBox)sender ;
if (!Utility.isNumeric(tb.Text)){
MessageBox.Show(tb.Text.Length.ToString());
tb.Focus();
}
}
private void Amount_1_LostFocus(object sender, RoutedEventArgs e)
{
check_value(sender);
}
When I enter a letter in the textbox there is an infinite loop and it seems that the tb.Focus() actually cause the LostFocus event to be call recursively.
I don't understand why the call to the Focus method of an object triggers the LostFocus event of the same object.
Opening the modal MessageBox is responsible for loosing the focus. Try hook to Validating event.
As i said before in the link provided by Xaqron it's said that it's forbidden to use the Focus method in the LostFocus event.
And as I'm developing a WPF application there is no Validating event and CausesValidation property, so the others ways to validate the content is to use the TextChanged event or use binding validation.
Thank you for your answers.
Of course, in a perfectly valid program, you should not change Focus in the LostFocus event. This also applies to the Enter, GotFocus, Leave, Validating and Validated events, which Ms makes clear in the documentation https://learn.microsoft.com/pl-pl/dotnet/api/system.windows.forms.control.lostfocus.
However, in very unusual cases, you can use the timer to trigger changes to the Focus, bypassing this problem.
private TextBox tb = null;
private System.Windows.Forms.Timer MyTimer;
private void initialize()
{
MyTimer.Tick += new System.EventHandler(MyTimer_Tick);
MyTimer.Enable = false;
MyTimer.Interval = 100;
}
private void check_value(object sender)
{
tb = (TextBox)sender ;
if (!Utility.isNumeric(tb.Text)){
MessageBox.Show(tb.Text.Length.ToString());
MyTimer.Enable = true;
}
}
private void Amount_1_LostFocus(object sender, RoutedEventArgs e)
{
check_value(sender);
}
private void MyTimer_Tick(object sender, EventArgs e)
{
MyTimer.Enabled = false;
if (tb!=null) tb.Focus();
}
I want to call a generic handler function for all textBox on GotFocus and LostFocus.
For GotFocus I could create:
private void GotFocus()
{
TextBox textBox = ((TextBox)FocusManager.GetFocusedElement());
textBox.Text = "";
}
and call it like this:
private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
//Instead of this in every TextBox
//TextBox textBox = (TextBox)sender;
//textBox.Text = "";
GotFocus();
}
But for LostFocus I can't do the same to get some symetry handler ? Am I obliged to manage the memorization of the control in GotFocus in a private member so as to be able to use it later in LostFocus ?
Aren't there any way to do this more globally by hooking the .NET system and create this missing GetPreviousFocusedElement function ?
I like the Law of Symetry, that's how Physicians discover new laws and I think this principle could also apply in IT.
The parameter object sender contains a reference to the control.
private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
(sender as TextBox).Text = "";
}
private void textBox1_LostFocus(object sender, RoutedEventArgs e)
{
(sender as TextBox).Text = "";
}
or whatever you want the LostFocus method to do.