Databound ListControl on TabPage

G

Guest

I have a ListControl on a TabPage. It doesn't matter which ListControl as
I've tried ListBox, ComboBox, and as shown below a custom derived control (in
fact the RadioButtonList control described in a July 2004 MSDN article). Here
is a sequence of happenings that is giving me grief:

1) I select an item that is not the first item in the ListControl and which
is not the currently selected item

2) I tab out of the ListControl

3) The property to which the ListControl's SelectedValue is bound is updated

4) The bound object raises a PropertyChanged event, initating a sequence of
ListChanged events

5) Ultimately, the ListControl.SelectedIndex property is set to 0 and the
top item in the ListControl shows as the selected item


If I change a property on another control on the TabPage then the
ListControl changes to show the correct selected item (the one selected in
step 1 above). Thereafter, the ListControl will show the correct selected
item as it is changed.


Here's a stack trace at the moment the SelectedIndex is being set to 0. The
line at the bottom of the stack trace shows the property bound to the
SelectedValue property of the ListControl being set to 2 which corresponds to
a SelectedIndex value of 1. The top line in the trace shows the SelectedIndex
value being incorrectly set to 0.

UI.dll!UI.ListControl.SelectedIndex.set(int value = 0)

System.Windows.Forms.dll!System.Windows.Forms.ListControl.DataManager_PositionChanged(object sender, System.EventArgs e)
[Native to Managed Transition]
[Managed to Native Transition]

System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnPositionChanged(System.EventArgs e)

System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.ChangeRecordState(int
newPosition, bool validating, bool endCurrentEdit, bool firePositionChange,
bool pullData)

System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.Position.set(int value)

System.Windows.Forms.dll!System.Windows.Forms.BindingSource.ParentCurrencyManager_CurrentItemChanged(object sender, System.EventArgs e)
[Native to Managed Transition]
[Managed to Native Transition]

System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnCurrentChanged(System.EventArgs e)

System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.ChangeRecordState(int
newPosition, bool validating, bool endCurrentEdit, bool firePositionChange =
true, bool pullData)

System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.List_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)

System.Windows.Forms.dll!System.Windows.Forms.BindingSource.OnListChanged(System.ComponentModel.ListChangedEventArgs e)

System.Windows.Forms.dll!System.Windows.Forms.BindingSource.SetList(System.Collections.IList list, bool metaDataChanged, bool applySortAndFilter = false)

System.Windows.Forms.dll!System.Windows.Forms.BindingSource.ParentCurrencyManager_CurrentItemChanged(object sender, System.EventArgs e)
[Native to Managed Transition]
[Managed to Native Transition]

System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnCurrentItemChanged(System.EventArgs e)

System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.List_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)

System.Windows.Forms.dll!System.Windows.Forms.BindingSource.OnListChanged(System.ComponentModel.ListChangedEventArgs e)

System.Windows.Forms.dll!System.Windows.Forms.BindingSource.InnerList_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
[Native to Managed Transition]
[Managed to Native Transition]

Data.dll!Data.EntityCollection.OnListChanged(System.ComponentModel.ListChangedEventArgs args = {System.ComponentModel.ListChangedEventArgs})

Data.dll!Data.EntityCollection.OnListChanged(System.ComponentModel.ListChangedType eventType = ItemChanged, int newIndex = 0)
Data.dll!Data.EntityCollection.EntityModifiedEvent(object sender =
{Data1.Entity1}, System.EventArgs args = {System.EventArgs})
Data.dll!Data.Entity.OnEntityModified(System.EventArgs e =
{System.EventArgs})
Data.dll!Data.Entity.EndUpdateFromProxy()
Data1.dll!Data1.Entity1.Entity2Proxy_ListChanged(object sender =
{Data1.Entity2Collection}, System.ComponentModel.ListChangedEventArgs e =
{System.ComponentModel.ListChangedEventArgs})

Data.dll!Data.EntityCollection.OnListChanged(System.ComponentModel.ListChangedEventArgs args = {System.ComponentModel.ListChangedEventArgs})
Data.dll!Data.EntityCollection.Entity_PropertyChanged(object sender =
{Data1.Entity2}, System.ComponentModel.PropertyChangedEventArgs e =
{System.ComponentModel.PropertyChangedEventArgs})

Data.dll!Data.Entity.OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e = {System.ComponentModel.PropertyChangedEventArgs})
Data.dll!Data.Entity.RaisePropertyChanged(string propertyName = "Entity3")
Data1.dll!Data1.Entity2.Entity3_PropertyChanged(object sender =
{Data1.Entity3}, System.ComponentModel.PropertyChangedEventArgs e =
{System.ComponentModel.PropertyChangedEventArgs})
[Native to Managed Transition]
[Managed to Native Transition]

Data.dll!Data.Entity.OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e = {System.ComponentModel.PropertyChangedEventArgs})
Data.dll!Data.Entity.RaisePropertyChanged(string propertyName =
"SelectedValueProperty")
Data1.dll!Data1.Entity3.SetValue(string propertyName = "Property1", int
value = 2, System.Collections.Generic.List<Lookups.Lookup> lookups = Count =
4)
Data1.dll!Data1.Entity3.Property1Key.set(int value = 2)

I've not included a description of the relationships of the various objects
and their associated bindings, but will do so if that is needed.

Do you recognize this problem and know a solution?

Thank you!
Mike
 
L

Linda Liu [MSFT]

Hi Mike,

I am sorry that I may not understand your problem exactly.

Based on my understanding, you have a ListControl(no matter it is ListBox,
ComboBox or a custom control derived from ListControl) on a TabPage and
have set the DataSource, ValueMember and DisplayMember properties of the
ListControl. You also bind a property to the SelectedValue of the
ListControl. When you select an item in the ListControl other than the
first item and the currently selected item and then move the focus out of
the ListControl using Tab key, the bound property get the current correct
value but the SelectedIndex property of the ListControl is set to 0.

If I'm off base, please feel free to let me know.

I performed a test based on my understanding, but I did't reproduce the
problem you described. The following is the walkthrough of my test.

1. Set up a Windows application project. Add a TabControl on the form. Add
a ListBox, a Label and a Button on the first tabpage of the tabcontrol.

2. Add a DataSet into the project and add a DataTable in the DataSet. Add
two columns in the DataTable(ID Int32, Text String).

3. Add a property named Field and an event name FieldPropertyChanged in the
form. Bind the property Field to the SelectedValue of the ListControl.
Handle the FieldPropertyChanged event to display the current value of the
property Field in the Label. Handle the Click event of the Button to show
the value of the SelectedValue property of the ListControl.

The following is the code in the form.

public partial class Form1 : Form
{
private int field = -1;
public int Field
{
get { return field; }
set
{
int temp = field;
field = value;
if (value != temp)
{
this.OnFieldPropertyChanged();
}
}
}
public event EventHandler FieldPropertyChanged;
private void OnFieldPropertyChanged()
{
if (FieldPropertyChanged != null)
FieldPropertyChanged(this, new EventArgs());
}

public Form1()
{
InitializeComponent();
this.FieldPropertyChanged += new
EventHandler(Form1_FieldPropertyChanged);
}

void Form1_FieldPropertyChanged(object sender, EventArgs e)
{
this.label1.Text = this.Field.ToString();
}

private void Form1_Load(object sender, EventArgs e)
{
DataSet1 ds = new DataSet1();
DataRow row = ds.DataTable1.NewRow();
row[0] = 1;
row[1] = "aaaa";
ds.DataTable1.Rows.Add(row);

row = ds.DataTable1.NewRow();
row[0] = 2;
row[1] = "bbb";
ds.DataTable1.Rows.Add(row);

row = ds.DataTable1.NewRow();
row[0] = 3;
row[1] = "ccc";
ds.DataTable1.Rows.Add(row);

row = ds.DataTable1.NewRow();
row[0] = 4;
row[1] = "ddd";
ds.DataTable1.Rows.Add(row);

ds.AcceptChanges();

this.listBox1.DataSource = ds;
this.listBox1.ValueMember = "DataTable1.ID";
this.listBox1.DisplayMember = "DataTable1.Text";

this.DataBindings.Add("Field", this.listBox1, "SelectedValue");

}

private void button1_Click(object sender, EventArgs e)
{
if (this.listBox1.SelectedValue != null)
{
MessageBox.Show(this.listBox1.SelectedValue.ToString());
}

}
}

When I select the second item in the listbox, the label reads '2'. When I
move the focus out of the listbox using Tab key, the selected item doesn't
change. If I click the button, a message box shows up saying '2'.

Is there any difference between your project and mine? You may show me the
key lines of code or send me your sample project that could just reproduce
the problem. To get my actual email address, remove 'online' from my
displayed email address.

I look forward to your reply.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu [MSFT]

Hi Mike,

How about the problem now?

If the problem is not resolved and you need our further assistance, please
feel free to tell me.

Thanks for using our MSDN Managed Newsgroup Support Service!


Sincerely,
Linda Liu
Microsoft Online Community Support
 
G

Guest

Thanks, Linda. Unfortunately my situation is going to be a fair amount of
work to try to duplicate outside of the environment I'm in. I will try to do
so if I have a chance, but until then I'll work on a workaround.

Mike
 
L

Linda Liu [MSFT]

Hi Mike,

Thank you for your update.

It seems that it is impossible for you to make a sample project that could
reproduce the problem now. We would like to close this issue temporarily.
If you want our assistance on this particular issue in the future, please
feel free to re-open it.

Thank you for using our MSDN Managed Newsgroup Support Service!


Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top