Windows Develop Bookmark and Share   
 index > Windows Forms General > could use some help on an event firing problem
 

could use some help on an event firing problem

hi all. ive got a winform im working on with a tab control. on some tab in the tabcontrol i have a datagridview and 2 textboxes on the same tab. right now, i have it in tab.Enter event the datagridview connects to the database and gets some records. after it is loaded with rows, i databind a column to a textbox on that tab. now when that textbox value changes (using the TextChanged event) i need it to change the value on the second textbox and pop a warning message (MessageBox).
whats happening is when i load up the app and click on this particular tab, i get the
MessageBox popup before the tab loads. and after close it, the tab loads up. so the databinding is triggering the TextChanged event and calling the MessageBox but its earlier than i want. i would like the popup to occur after the tab has loaded rather than before. is there a better way to handle this? maybe use a different event other than tab.Enter to do the databind?
nattylife  Friday, November 21, 2008 4:33 PM

Post code for a better answer. The TextChanged event is firing because the TextBox is being initialized to a particular value from your data. Check the sender object to see who fired the event.

Rudedog =|^D

Rudedog2  Friday, November 21, 2008 5:18 PM
The standard approach is to set a little helper flag that indicates that the event fired due to code executing, not the user typing. Something like this:

private bool mUpdating;

private void textBox1_TextChanged(object sender, EventArgs e) {
if (mUpdating) return;
// Normal update stuff
//...
}
private void updateFromDbase() {
mUpdating = true;
try {
// Database and binding stuff
//..
}
finally {
mUpdating = false;
}
}

nobugz  Friday, November 21, 2008 5:59 PM

Hi nattylife,

I think there is a better way to overcome the problem you are facing. As Rudedog2 pointed out, the TextChanged event of the TextBox will be fired once the DataBinding process was completed. The MessBox.Show() function in the TextChanged event handler will block the TabPage control loading any other controls until you click Ok to close the model dialog.

To solve the problem, we can delay adding event handler for TextChanged event of TextBox controls. Say it exactly, we can add TextChanged event handler in the BingdingComplete event handler of the DataBinding object.

I wrote the following code to help you understand my ideas.

Code Snippet

public partial class Form2 : Form

{

DataTable dt = new DataTable();

public Form2()

{

InitializeComponent();

dt.Columns.Add("AA", typeof(string));

dt.Columns.Add("BB", typeof(string));

dt.Rows.Add("aa1", "bb1");

dt.Rows.Add("aa2", "bb2");

this.tabPage2.Enter += new EventHandler(tabPage2_Enter);

}

void tabPage2_Enter(object sender, EventArgs e)

{

if (this.textBox1.DataBindings.Count == 0)

{

this.dataGridView1.DataSource = dt;

this.textBox1.DataBindings.Add("Text", dt, "AA",true);

this.textBox1.DataBindings["Text"].BindingComplete+=new BindingCompleteEventHandler(TextBox1_BindingComplete);

}

}

void TextBox1_BindingComplete(object sender, BindingCompleteEventArgs e)

{

this.textBox1.TextChanged -= new System.EventHandler(this.textBox1_TextChanged);

this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

}

private void textBox1_TextChanged(object sender, EventArgs e)

{

MessageBox.Show("dd");

}

If you have further problems, please feel free to let me know.

Best regards,

Bruce Zhou

Bruce.Zhou  Tuesday, November 25, 2008 7:30 AM

Post code for a better answer. The TextChanged event is firing because the TextBox is being initialized to a particular value from your data. Check the sender object to see who fired the event.

Rudedog =|^D

Rudedog2  Friday, November 21, 2008 5:18 PM
The standard approach is to set a little helper flag that indicates that the event fired due to code executing, not the user typing. Something like this:

private bool mUpdating;

private void textBox1_TextChanged(object sender, EventArgs e) {
if (mUpdating) return;
// Normal update stuff
//...
}
private void updateFromDbase() {
mUpdating = true;
try {
// Database and binding stuff
//..
}
finally {
mUpdating = false;
}
}

nobugz  Friday, November 21, 2008 5:59 PM
nobugz wrote:
The standard approach is to set a little helper flag that indicates that the event fired due to code executing, not the user typing. Something like this:

private bool mUpdating;

private void textBox1_TextChanged(object sender, EventArgs e) {
if (mUpdating) return;
// Normal update stuff
//...
}
private void updateFromDbase() {
mUpdating = true;
try {
// Database and binding stuff
//..
}
finally {
mUpdating = false;
}
}



this was actually the approach i was thinking, but i wanted to make sure there wasnt an event handler around that i should have used instead of the Enter one. thanks
nattylife  Friday, November 21, 2008 6:08 PM

Hi nattylife,

I think there is a better way to overcome the problem you are facing. As Rudedog2 pointed out, the TextChanged event of the TextBox will be fired once the DataBinding process was completed. The MessBox.Show() function in the TextChanged event handler will block the TabPage control loading any other controls until you click Ok to close the model dialog.

To solve the problem, we can delay adding event handler for TextChanged event of TextBox controls. Say it exactly, we can add TextChanged event handler in the BingdingComplete event handler of the DataBinding object.

I wrote the following code to help you understand my ideas.

Code Snippet

public partial class Form2 : Form

{

DataTable dt = new DataTable();

public Form2()

{

InitializeComponent();

dt.Columns.Add("AA", typeof(string));

dt.Columns.Add("BB", typeof(string));

dt.Rows.Add("aa1", "bb1");

dt.Rows.Add("aa2", "bb2");

this.tabPage2.Enter += new EventHandler(tabPage2_Enter);

}

void tabPage2_Enter(object sender, EventArgs e)

{

if (this.textBox1.DataBindings.Count == 0)

{

this.dataGridView1.DataSource = dt;

this.textBox1.DataBindings.Add("Text", dt, "AA",true);

this.textBox1.DataBindings["Text"].BindingComplete+=new BindingCompleteEventHandler(TextBox1_BindingComplete);

}

}

void TextBox1_BindingComplete(object sender, BindingCompleteEventArgs e)

{

this.textBox1.TextChanged -= new System.EventHandler(this.textBox1_TextChanged);

this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

}

private void textBox1_TextChanged(object sender, EventArgs e)

{

MessageBox.Show("dd");

}

If you have further problems, please feel free to let me know.

Best regards,

Bruce Zhou

Bruce.Zhou  Tuesday, November 25, 2008 7:30 AM

You can use google to search for other answers

Custom Search

More Threads

• Hiding desktop icons without disabling desktop in c#
• WebClient and Timeouts...
• Richtextbox error !
• Problem with FromStream in PictureBox
• how to export rtb document into image
• Public Enum
• How can i control the mouse and keyboard?
• Heavy operation and Windows.Forms.Timer
• Images for use in application development
• UI hanging