strange behaviour of click method
-
hello, I have a click method for a button and try to iterate through all comboboxes on the form. When I try to access the control through the Controls collection the program jumps out of the method (in the line Control cc = this.Controls[i];). My code is below. Can anyone tell me what I'm doing wrong here?
public partial class NewTopicCtxCfgElemFrm : Form
{
private bool _freeConfig = false;public NewTopicCtxCfgElemFrm() { InitializeComponent(); } public void Initialize(Topic topic) { \_freeConfig = business.TopicFactory.GetTopicTypeIsFreeConfigurable(topic.ID); label1.Text = (\_freeConfig) ? "Please select at least one of all." : "Please select all."; List list = business.TopicFactory.GetObjTypeForTopic(topic.ID); int k = 0; foreach (var item in list) { int y = GetNewLocationTop(k); // add a label and a combobox to the form Label lbl = new Label(); lbl.Text = item.ObjTypeName; lbl.Location = new Point(10, y); lbl.Width = 200; this.Controls.Add(lbl); ComboBox cbx = new ComboBox(); cbx.Location = new Point(220, y); cbx.Width = 200; cbx.Tag = item; this.Controls.Add(cbx); // set the datasource List objects = business.ObjLinkFactory.GetObjForObjType(item.ObjTypeID); cbx.DataSource = objects; cbx.SelectedIndex = -1; // increment indexer k++; } // add an save-button to the form Button btn = new Button(); btn.Text = "Save"; btn.Location = new Point(10, GetNewLocationTop(k)); btn.Width = 200; this.Controls.Add(btn); btn.Click += new EventHandler(btn\_Click); } void btn\_Click(object sender, EventArgs e) { try { bool checkRequiredObjTypesOk = !\_freeConfig; // check if all required obj types are selected for (int i = 0; i < this.Controls.Count; i++) { Control cc = this.Controls\[i\]; ComboBox cb = cc as ComboBox; if (cb != null) { if (!\_freeConfig) { if (cb.SelectedIndex == -1) { checkRequiredObjTypesOk = false; } } else { if (cb.SelectedIndex > -1) { checkRequiredObjTypesOk = true; } } } } } catch (Excep
joost.versteegen wrote:
the program jumps out of the method (in the lin
No, it doesn't. It jumps out on the line after that, as your control is not a combo. Using "as" does not throw an exception, the variable is
null
and ignored.Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
You could try a complete rebuild of your project and then put a breakpoint at the beginning of the
btn_Click
method and run it through your debugger. You should also review what that method is trying to do. As it stands it seems to be settingcheckRequiredObjTypesOk
randomly totrue
orfalse
, but never uses that value for any purpose.That part was not finished yet.
-
joost.versteegen wrote:
the program jumps out of the method (in the lin
No, it doesn't. It jumps out on the line after that, as your control is not a combo. Using "as" does not throw an exception, the variable is
null
and ignored.Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
No, it does realy jump out of the line i specified. The debugger sometimes does not reach the "if (cb != null)" test!
-
I really don't think that would be it, but, when you compile your exe it'll produce some .PDB files. These are the 'program database' files and are used by the debugger to map the execution point and variables etc. into the source code. If it gets out of sync then you can see some very weird behaviour. When execution leaves your button handler, does the application carry on?
Regards, Rob Philpott.
How could i put the pdb file back in sync? I already tried a complete rebuild but that didn't help.
-
That part was not finished yet.
OK. However I have just tested your code (modified to fit my sample) and it works fine using both
for
andforeach
. You need to get to work with your debugger to see what is happening. I suspect some out of date code is getting linked in by mistake somewhere. -
How could i put the pdb file back in sync? I already tried a complete rebuild but that didn't help.
Do a "Clean Project" first, then recompile it.
A guide to posting questions on CodeProject
How to debug small programs
Dave Kreskowiak -
No, it does realy jump out of the line i specified. The debugger sometimes does not reach the "if (cb != null)" test!
Add in a Debug.WriteLine to show the names of the controls found. It will only loop the children, not any sub-children btw! The code will SKIP anything that is not directly on the form (IE, any combo that's on a panel).
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
Add in a Debug.WriteLine to show the names of the controls found. It will only loop the children, not any sub-children btw! The code will SKIP anything that is not directly on the form (IE, any combo that's on a panel).
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
took your advise and added a number of debug.writeln statements...the code did exactly what it was supposed to do...but the debugger keeps doing strange things. Thanks anyway!!
-
took your advise and added a number of debug.writeln statements...the code did exactly what it was supposed to do...but the debugger keeps doing strange things. Thanks anyway!!
-
hello, I have a click method for a button and try to iterate through all comboboxes on the form. When I try to access the control through the Controls collection the program jumps out of the method (in the line Control cc = this.Controls[i];). My code is below. Can anyone tell me what I'm doing wrong here?
public partial class NewTopicCtxCfgElemFrm : Form
{
private bool _freeConfig = false;public NewTopicCtxCfgElemFrm() { InitializeComponent(); } public void Initialize(Topic topic) { \_freeConfig = business.TopicFactory.GetTopicTypeIsFreeConfigurable(topic.ID); label1.Text = (\_freeConfig) ? "Please select at least one of all." : "Please select all."; List list = business.TopicFactory.GetObjTypeForTopic(topic.ID); int k = 0; foreach (var item in list) { int y = GetNewLocationTop(k); // add a label and a combobox to the form Label lbl = new Label(); lbl.Text = item.ObjTypeName; lbl.Location = new Point(10, y); lbl.Width = 200; this.Controls.Add(lbl); ComboBox cbx = new ComboBox(); cbx.Location = new Point(220, y); cbx.Width = 200; cbx.Tag = item; this.Controls.Add(cbx); // set the datasource List objects = business.ObjLinkFactory.GetObjForObjType(item.ObjTypeID); cbx.DataSource = objects; cbx.SelectedIndex = -1; // increment indexer k++; } // add an save-button to the form Button btn = new Button(); btn.Text = "Save"; btn.Location = new Point(10, GetNewLocationTop(k)); btn.Width = 200; this.Controls.Add(btn); btn.Click += new EventHandler(btn\_Click); } void btn\_Click(object sender, EventArgs e) { try { bool checkRequiredObjTypesOk = !\_freeConfig; // check if all required obj types are selected for (int i = 0; i < this.Controls.Count; i++) { Control cc = this.Controls\[i\]; ComboBox cb = cc as ComboBox; if (cb != null) { if (!\_freeConfig) { if (cb.SelectedIndex == -1) { checkRequiredObjTypesOk = false; } } else { if (cb.SelectedIndex > -1) { checkRequiredObjTypesOk = true; } } } } } catch (Excep
The debugger is sometimes a little buggy. Set a break point in one of the next lines to be hi. Generally, it will stop then there even if it uses to leave the method to nowhere.