Flaky results when loading win form tab control

R

Rotsey

Hi,

I am loading a tab control on a form.

The code loads textboxes and comboboxes and checkboxes, normal
data entry form that loads a table row of data.

I have a combo on the form above the tab control that filters the form
for me.

But I am finding that it is hit or miss whether the comboboxes
have the selected item set from the underlying table when filtering

Sometimes it works sometimes they are set at the first item in the list.

Textboxes seem fine.

If the tabpage is not visible would this cuase this???

Any ideas??

rotsey
 
A

AlexS

re: Sometimes it works sometimes they are set at the first item in the list.

Most probably it's bad logic. Can you post code demonstrating problem?
 
R

Rotsey

The problem seems to be that the tabs that aren't visible aren't
selecting the combo items, because when I click on a tab and then
reselect the filter the combos are fine.???

How much code would you like to see?
 
A

AlexS

Demonstrating the problem. Bare bones.


Rotsey said:
The problem seems to be that the tabs that aren't visible aren't
selecting the combo items, because when I click on a tab and then
reselect the filter the combos are fine.???

How much code would you like to see?
 
R

Rotsey

Alex

i have discovered a problem with it.
When I intiialise the combo with a datatable the datatable
has 20 rows in it. The form displays fine as in I see the items
in the combo but when it goes to set the item in the combo
the number of items in the collection is zero. Weird

If this code helps here is 2 methods. First one is
the creation of the combo and the other a method to
set the selected item.

private void CreateComboBox(ControlDef ctl, Control.ControlCollection
controls)

{

TBRComboBox cmb = new TBRComboBox();

string classname;

ILookUpObject obj;

DataSet ds;

cmb.Name = ctl.FieldName;

cmb.Top = ctl.Top;

cmb.Left = ctl.Left;

cmb.Width = ctl.Width;

if (ctl.LookUpSql != "NIL")

{

if(ctl.LookUpSql.StartsWith("Select",StringComparison.CurrentCultureIgnoreCase))

{

classname = "TBR.NET." + ExtractText.GetClassName(ctl.LookUpSql);

obj = (ILookUpObject)Activator.CreateInstance(Type.GetType(classname));


if (obj == null)

throw new ApplicationException("Cannot create object for type " +
classname);

ds = obj.GetLookupData(ExtractText.ParseSQL(ctl.LookUpSql));

if (ctl.FieldName == "PayFrequency")

Console.WriteLine("");


switch (ctl.ColumnCount)

{

case 2:

cmb.DisplayMember = "Description";

cmb.ValueMember = "ID";

cmb.DataSource = ds.Tables[0];

break;

case 1:

foreach (DataRow dr in ds.Tables[0].Rows)

{

cmb.Items.Add(dr[0]);

}

break;

default:

break;

}


if (ctl.IsRowLookup == true)

{

cmb.ForeColor = TBRSettings.Instance.Settings.FilterFieldForeColor;

cmb.BackColor = TBRSettings.Instance.Settings.FilterFieldBgColor;

cmb.SelectedIndexChanged += delegate(Object s, EventArgs e)

{

LoadData((int)cmb.SelectedValue);

};

}

}

else

{

string[] words;

List<ComboItem> list = new List<ComboItem>();

words = ctl.LookUpSql.Split(';');

cmb.Items.Clear();

if (ctl.ColumnCount == 1)

{

foreach (string word in words)

{

cmb.Items.Add(word.Replace("\"", ""));

}

}

else if(ctl.ColumnCount == 2)

{

for (int i = 0; i <words.Length; i += 2)

{

list.Add(new ComboItem(Convert.ToInt32(words), words[i+1].Replace("\"",
"")));

}

cmb.DisplayMember = "Description";

cmb.ValueMember = "ID";

cmb.DataSource = list;

}

}

}

cmb.SelectedIndex = -1;

controls.Add(cmb);

}



public void FindByValue(string value, string columnname)

{

int aIndex;

DataRowView drv;

ComboDebug.Instance.list.Add(this.Name + " ---- NumItems: " +
this.Items.Count);

for (aIndex = 0; aIndex < this.Items.Count; aIndex += 1)

{

try

{

drv = (DataRowView)this.Items[aIndex];

if (drv.Row[columnname].ToString() == value)

{

ComboDebug.Instance.list.Add(this.Name + " ---- " + columnname + " ----- " +
value);

this.SelectedIndex = aIndex;

return;

}

}

catch (Exception)

{

if(this.Items[aIndex].ToString() == value)

{

ComboDebug.Instance.list.Add(this.Name + " ---- " + columnname + " ----- " +
value);

this.SelectedIndex = aIndex;

return;

}

}

}

this.SelectedIndex = -1;

return;

}
 
A

AlexS

Do you dispose your lists between Create and Find?

Can you step through and see when collection disappears?


Rotsey said:
Alex

i have discovered a problem with it.
When I intiialise the combo with a datatable the datatable
has 20 rows in it. The form displays fine as in I see the items
in the combo but when it goes to set the item in the combo
the number of items in the collection is zero. Weird

If this code helps here is 2 methods. First one is
the creation of the combo and the other a method to
set the selected item.

private void CreateComboBox(ControlDef ctl, Control.ControlCollection
controls)

{

TBRComboBox cmb = new TBRComboBox();

string classname;

ILookUpObject obj;

DataSet ds;

cmb.Name = ctl.FieldName;

cmb.Top = ctl.Top;

cmb.Left = ctl.Left;

cmb.Width = ctl.Width;

if (ctl.LookUpSql != "NIL")

{

if(ctl.LookUpSql.StartsWith("Select",StringComparison.CurrentCultureIgnoreCase))

{

classname = "TBR.NET." + ExtractText.GetClassName(ctl.LookUpSql);

obj = (ILookUpObject)Activator.CreateInstance(Type.GetType(classname));


if (obj == null)

throw new ApplicationException("Cannot create object for type " +
classname);

ds = obj.GetLookupData(ExtractText.ParseSQL(ctl.LookUpSql));

if (ctl.FieldName == "PayFrequency")

Console.WriteLine("");


switch (ctl.ColumnCount)

{

case 2:

cmb.DisplayMember = "Description";

cmb.ValueMember = "ID";

cmb.DataSource = ds.Tables[0];

break;

case 1:

foreach (DataRow dr in ds.Tables[0].Rows)

{

cmb.Items.Add(dr[0]);

}

break;

default:

break;

}


if (ctl.IsRowLookup == true)

{

cmb.ForeColor = TBRSettings.Instance.Settings.FilterFieldForeColor;

cmb.BackColor = TBRSettings.Instance.Settings.FilterFieldBgColor;

cmb.SelectedIndexChanged += delegate(Object s, EventArgs e)

{

LoadData((int)cmb.SelectedValue);

};

}

}

else

{

string[] words;

List<ComboItem> list = new List<ComboItem>();

words = ctl.LookUpSql.Split(';');

cmb.Items.Clear();

if (ctl.ColumnCount == 1)

{

foreach (string word in words)

{

cmb.Items.Add(word.Replace("\"", ""));

}

}

else if(ctl.ColumnCount == 2)

{

for (int i = 0; i <words.Length; i += 2)

{

list.Add(new ComboItem(Convert.ToInt32(words), words[i+1].Replace("\"",
"")));

}

cmb.DisplayMember = "Description";

cmb.ValueMember = "ID";

cmb.DataSource = list;

}

}

}

cmb.SelectedIndex = -1;

controls.Add(cmb);

}



public void FindByValue(string value, string columnname)

{

int aIndex;

DataRowView drv;

ComboDebug.Instance.list.Add(this.Name + " ---- NumItems: " +
this.Items.Count);

for (aIndex = 0; aIndex < this.Items.Count; aIndex += 1)

{

try

{

drv = (DataRowView)this.Items[aIndex];

if (drv.Row[columnname].ToString() == value)

{

ComboDebug.Instance.list.Add(this.Name + " ---- " + columnname + " ----- "
+ value);

this.SelectedIndex = aIndex;

return;

}

}

catch (Exception)

{

if(this.Items[aIndex].ToString() == value)

{

ComboDebug.Instance.list.Add(this.Name + " ---- " + columnname + " ----- "
+ value);

this.SelectedIndex = aIndex;

return;

}

}

}

this.SelectedIndex = -1;

return;

}


AlexS said:
Demonstrating the problem. Bare bones.
 
R

Rotsey

Alex,

The list shows 0 items after I assign the datatable to the datasource
property.

rotsey



AlexS said:
Do you dispose your lists between Create and Find?

Can you step through and see when collection disappears?


Rotsey said:
Alex

i have discovered a problem with it.
When I intiialise the combo with a datatable the datatable
has 20 rows in it. The form displays fine as in I see the items
in the combo but when it goes to set the item in the combo
the number of items in the collection is zero. Weird

If this code helps here is 2 methods. First one is
the creation of the combo and the other a method to
set the selected item.

private void CreateComboBox(ControlDef ctl, Control.ControlCollection
controls)

{

TBRComboBox cmb = new TBRComboBox();

string classname;

ILookUpObject obj;

DataSet ds;

cmb.Name = ctl.FieldName;

cmb.Top = ctl.Top;

cmb.Left = ctl.Left;

cmb.Width = ctl.Width;

if (ctl.LookUpSql != "NIL")

{

if(ctl.LookUpSql.StartsWith("Select",StringComparison.CurrentCultureIgnoreCase))

{

classname = "TBR.NET." + ExtractText.GetClassName(ctl.LookUpSql);

obj = (ILookUpObject)Activator.CreateInstance(Type.GetType(classname));


if (obj == null)

throw new ApplicationException("Cannot create object for type " +
classname);

ds = obj.GetLookupData(ExtractText.ParseSQL(ctl.LookUpSql));

if (ctl.FieldName == "PayFrequency")

Console.WriteLine("");


switch (ctl.ColumnCount)

{

case 2:

cmb.DisplayMember = "Description";

cmb.ValueMember = "ID";

cmb.DataSource = ds.Tables[0];

break;

case 1:

foreach (DataRow dr in ds.Tables[0].Rows)

{

cmb.Items.Add(dr[0]);

}

break;

default:

break;

}


if (ctl.IsRowLookup == true)

{

cmb.ForeColor = TBRSettings.Instance.Settings.FilterFieldForeColor;

cmb.BackColor = TBRSettings.Instance.Settings.FilterFieldBgColor;

cmb.SelectedIndexChanged += delegate(Object s, EventArgs e)

{

LoadData((int)cmb.SelectedValue);

};

}

}

else

{

string[] words;

List<ComboItem> list = new List<ComboItem>();

words = ctl.LookUpSql.Split(';');

cmb.Items.Clear();

if (ctl.ColumnCount == 1)

{

foreach (string word in words)

{

cmb.Items.Add(word.Replace("\"", ""));

}

}

else if(ctl.ColumnCount == 2)

{

for (int i = 0; i <words.Length; i += 2)

{

list.Add(new ComboItem(Convert.ToInt32(words),
words[i+1].Replace("\"", "")));

}

cmb.DisplayMember = "Description";

cmb.ValueMember = "ID";

cmb.DataSource = list;

}

}

}

cmb.SelectedIndex = -1;

controls.Add(cmb);

}



public void FindByValue(string value, string columnname)

{

int aIndex;

DataRowView drv;

ComboDebug.Instance.list.Add(this.Name + " ---- NumItems: " +
this.Items.Count);

for (aIndex = 0; aIndex < this.Items.Count; aIndex += 1)

{

try

{

drv = (DataRowView)this.Items[aIndex];

if (drv.Row[columnname].ToString() == value)

{

ComboDebug.Instance.list.Add(this.Name + " ---- " + columnname + " -----
" + value);

this.SelectedIndex = aIndex;

return;

}

}

catch (Exception)

{

if(this.Items[aIndex].ToString() == value)

{

ComboDebug.Instance.list.Add(this.Name + " ---- " + columnname + " -----
" + value);

this.SelectedIndex = aIndex;

return;

}

}

}

this.SelectedIndex = -1;

return;

}


AlexS said:
Demonstrating the problem. Bare bones.


The problem seems to be that the tabs that aren't visible aren't
selecting the combo items, because when I click on a tab and then
reselect the filter the combos are fine.???

How much code would you like to see?


re: Sometimes it works sometimes they are set at the first item in the
list.

Most probably it's bad logic. Can you post code demonstrating problem?


Hi,

I am loading a tab control on a form.

The code loads textboxes and comboboxes and checkboxes, normal
data entry form that loads a table row of data.

I have a combo on the form above the tab control that filters the
form
for me.

But I am finding that it is hit or miss whether the comboboxes
have the selected item set from the underlying table when filtering

Sometimes it works sometimes they are set at the first item in the
list.

Textboxes seem fine.

If the tabpage is not visible would this cuase this???

Any ideas??

rotsey

 
A

AlexS

When the DataSource property for combo box is set, the items collection
cannot be modified

Is your table having any data? Are you changing item list after setting
Datasource? If yes, you will need to remove it.


Rotsey said:
Alex,

The list shows 0 items after I assign the datatable to the datasource
property.

rotsey



AlexS said:
Do you dispose your lists between Create and Find?

Can you step through and see when collection disappears?


Rotsey said:
Alex

i have discovered a problem with it.
When I intiialise the combo with a datatable the datatable
has 20 rows in it. The form displays fine as in I see the items
in the combo but when it goes to set the item in the combo
the number of items in the collection is zero. Weird

If this code helps here is 2 methods. First one is
the creation of the combo and the other a method to
set the selected item.

private void CreateComboBox(ControlDef ctl, Control.ControlCollection
controls)

{

TBRComboBox cmb = new TBRComboBox();

string classname;

ILookUpObject obj;

DataSet ds;

cmb.Name = ctl.FieldName;

cmb.Top = ctl.Top;

cmb.Left = ctl.Left;

cmb.Width = ctl.Width;

if (ctl.LookUpSql != "NIL")

{

if(ctl.LookUpSql.StartsWith("Select",StringComparison.CurrentCultureIgnoreCase))

{

classname = "TBR.NET." + ExtractText.GetClassName(ctl.LookUpSql);

obj = (ILookUpObject)Activator.CreateInstance(Type.GetType(classname));


if (obj == null)

throw new ApplicationException("Cannot create object for type " +
classname);

ds = obj.GetLookupData(ExtractText.ParseSQL(ctl.LookUpSql));

if (ctl.FieldName == "PayFrequency")

Console.WriteLine("");


switch (ctl.ColumnCount)

{

case 2:

cmb.DisplayMember = "Description";

cmb.ValueMember = "ID";

cmb.DataSource = ds.Tables[0];

break;

case 1:

foreach (DataRow dr in ds.Tables[0].Rows)

{

cmb.Items.Add(dr[0]);

}

break;

default:

break;

}


if (ctl.IsRowLookup == true)

{

cmb.ForeColor = TBRSettings.Instance.Settings.FilterFieldForeColor;

cmb.BackColor = TBRSettings.Instance.Settings.FilterFieldBgColor;

cmb.SelectedIndexChanged += delegate(Object s, EventArgs e)

{

LoadData((int)cmb.SelectedValue);

};

}

}

else

{

string[] words;

List<ComboItem> list = new List<ComboItem>();

words = ctl.LookUpSql.Split(';');

cmb.Items.Clear();

if (ctl.ColumnCount == 1)

{

foreach (string word in words)

{

cmb.Items.Add(word.Replace("\"", ""));

}

}

else if(ctl.ColumnCount == 2)

{

for (int i = 0; i <words.Length; i += 2)

{

list.Add(new ComboItem(Convert.ToInt32(words),
words[i+1].Replace("\"", "")));

}

cmb.DisplayMember = "Description";

cmb.ValueMember = "ID";

cmb.DataSource = list;

}

}

}

cmb.SelectedIndex = -1;

controls.Add(cmb);

}



public void FindByValue(string value, string columnname)

{

int aIndex;

DataRowView drv;

ComboDebug.Instance.list.Add(this.Name + " ---- NumItems: " +
this.Items.Count);

for (aIndex = 0; aIndex < this.Items.Count; aIndex += 1)

{

try

{

drv = (DataRowView)this.Items[aIndex];

if (drv.Row[columnname].ToString() == value)

{

ComboDebug.Instance.list.Add(this.Name + " ---- " + columnname + " -----
" + value);

this.SelectedIndex = aIndex;

return;

}

}

catch (Exception)

{

if(this.Items[aIndex].ToString() == value)

{

ComboDebug.Instance.list.Add(this.Name + " ---- " + columnname + " -----
" + value);

this.SelectedIndex = aIndex;

return;

}

}

}

this.SelectedIndex = -1;

return;

}


Demonstrating the problem. Bare bones.


The problem seems to be that the tabs that aren't visible aren't
selecting the combo items, because when I click on a tab and then
reselect the filter the combos are fine.???

How much code would you like to see?


re: Sometimes it works sometimes they are set at the first item in
the list.

Most probably it's bad logic. Can you post code demonstrating
problem?


Hi,

I am loading a tab control on a form.

The code loads textboxes and comboboxes and checkboxes, normal
data entry form that loads a table row of data.

I have a combo on the form above the tab control that filters the
form
for me.

But I am finding that it is hit or miss whether the comboboxes
have the selected item set from the underlying table when filtering

Sometimes it works sometimes they are set at the first item in the
list.

Textboxes seem fine.

If the tabpage is not visible would this cuase this???

Any ideas??

rotsey


 

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