robertpnl

Hi,

I have a very strange situation with databinding and with an UserControl.

I have a UserControl with two textboxes: textbox1 and textbox2.

Two properties are available for get/set these textboxes: TxtBox1 and TxtBox2.

The gets/sets are simpel: write/read the .text property of the textbox.

Also, when user enter some text, a event with TxtBox1Changed and TxtBox2Changed will raised for databinding.

In the main program, with the usercontrol and a BindingSource, I set databinding on this way

[code]

userControl1.DataBindings.Add("TxtBox1", bindingSource1, "text1");

userControl1.DataBindings.Add("TxtBox2", bindingSource1, "text2");

[/code]

When i'm testing this with one DataBinding (comment second databinding), all works fine. Do I this with the two databinding as displayed here, then I have a very strange situation.

When I enter some text in textbox1, tab to textbox2 and enter also some text, click on an another control op form, so UserControl will loose the focus, textbox2 text will cleared. Then again, I enter soms text in textbox2, and again loose the focus of UC, the text2 will not removed. And everythings works fine.

During debug, I see that the mainprogram will call Set of textbox2 before the Get. Only once will this happen, when the application runs.

What's wrong with this all Is there sometime that i'm missing when using multiple properties of UC and databinding Thanks.



Re: Windows Forms Data Controls and Databinding UserControl with multiple propertys: strange result

robertpnl

Ok, i have a look better to this, but this will happen when I raise the event TxtBox1PropertyChanged (when the focus will loose). Then, the TxtBox2-set property will be called by some native code and change the textbox2 value to empty.

So my solution is now that during raising the event of property change, not any set-property can be change the value. I do this with a boolean that I set on true before raising the event, and set on false after raising.





Re: Windows Forms Data Controls and Databinding UserControl with multiple propertys: strange result

Rong-Chun Zhang - MSFT

Hi robertpnl,

I can reproduce your problem. Could you show some code on how you implement the UserControl Below is the code I used :

Code Snippet

namespace UserControls

{

public partial class BindingableUC : UserControl, INotifyPropertyChanged

{

public BindingableUC()

{

InitializeComponent();

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

this.textBox2.TextChanged += new EventHandler(textBox2_TextChanged);

}

void textBox1_TextChanged(object sender, EventArgs e)

{

OnPPropertyChanged("TxtBox1");

}

void textBox2_TextChanged(object sender, EventArgs e)

{

OnPPropertyChanged("TxtBox2");

}

public string TxtBox1

{

get { return this.textBox1.Text;}

set { this.textBox1.Text = value; }

}

public string TxtBox2

{

get { return this.textBox2.Text; }

set { this.textBox2.Text = value; }

}

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPPropertyChanged(string PropertyName)

{

if (PropertyChanged != null)

{

PropertyChanged(this,new PropertyChangedEventArgs(PropertyName));

}

}

#endregion

}

}

Hope this helps.

Regards






Re: Windows Forms Data Controls and Databinding UserControl with multiple propertys: strange result

robertpnl

Code Snippet

namespace UserControls

{

public partial class BindingableUC : UserControl, INotifyPropertyChanged

{

private bool _suspend = false;

public BindingableUC()

{

InitializeComponent();

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

this.textBox2.TextChanged += new EventHandler(textBox2_TextChanged);

}

void textBox1_TextChanged(object sender, EventArgs e)

{

_suspend = true;

OnPPropertyChanged("TxtBox1");

_suspend = false;

}

void textBox2_TextChanged(object sender, EventArgs e)

{

_suspend = true;

OnPPropertyChanged("TxtBox2");

_suspend = false;

}

public string TxtBox1

{

get { return this.textBox1.Text;}

set

{

if (_suspend) return;

this.textBox1.Text = value;

}

}

public string TxtBox2

{

get { return this.textBox2.Text; }

set

{

if (_suspend) return;

this.textBox2.Text = value;

}

}

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPPropertyChanged(string PropertyName)

{

if (PropertyChanged != null)

{

PropertyChanged(this,new PropertyChangedEventArgs(PropertyName));

}

}

#endregion

}

}

Hi Rong-Chun Zhang,

Thank you for your reply.

The code here, I add a boolean with the name _suspend. It wil set on true during raising an event. This wll denied to change antoher properties when even is raising.





Re: Windows Forms Data Controls and Databinding UserControl with multiple propertys: strange result

Rong-Chun Zhang - MSFT

Hi robertpnl,

Thanks you for posting. The code I post(without any flag) work well on my machine, Could you show us the code of your UserControl.

Thanks.






Re: Windows Forms Data Controls and Databinding UserControl with multiple propertys: strange result

Rong-Chun Zhang - MSFT

Hi robertpnl,

We are changing the issue type to “Comment” because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question” by editing your initial post and changing the radio button at the top of the post editor window. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

Thank you!