Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. strange behaviour of click method

strange behaviour of click method

Scheduled Pinned Locked Moved C#
businessquestion
17 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    joost versteegen
    wrote on last edited by
    #1

    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
    
    R L B 4 Replies Last reply
    0
    • J joost versteegen

      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
      
      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      Curious - what do you mean it jumps out? On which iteration of the loop - the first?

      Regards, Rob Philpott.

      J 1 Reply Last reply
      0
      • R Rob Philpott

        Curious - what do you mean it jumps out? On which iteration of the loop - the first?

        Regards, Rob Philpott.

        J Offline
        J Offline
        joost versteegen
        wrote on last edited by
        #3

        It differs, most of time on the first loop, sometimes in the second loop. I do not get any exception. I tried restarting visual studio, did not help. Also tried fornext(), but same result.

        R 1 Reply Last reply
        0
        • J joost versteegen

          It differs, most of time on the first loop, sometimes in the second loop. I do not get any exception. I tried restarting visual studio, did not help. Also tried fornext(), but same result.

          R Offline
          R Offline
          Rob Philpott
          wrote on last edited by
          #4

          Well, I'm as confused as you. I can't see anything wrong with the code, and particularly not anything which would cause it to return from the function on that line. The only very long shot I can think of is the pdbs are out of date of something and that's causing the debugger to lie to you.

          Regards, Rob Philpott.

          J 1 Reply Last reply
          0
          • R Rob Philpott

            Well, I'm as confused as you. I can't see anything wrong with the code, and particularly not anything which would cause it to return from the function on that line. The only very long shot I can think of is the pdbs are out of date of something and that's causing the debugger to lie to you.

            Regards, Rob Philpott.

            J Offline
            J Offline
            joost versteegen
            wrote on last edited by
            #5

            pdbs??? could you elaborate please?

            R 1 Reply Last reply
            0
            • J joost versteegen

              pdbs??? could you elaborate please?

              R Offline
              R Offline
              Rob Philpott
              wrote on last edited by
              #6

              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.

              J 1 Reply Last reply
              0
              • J joost versteegen

                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
                
                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                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 setting checkRequiredObjTypesOk randomly to true or false, but never uses that value for any purpose.

                J 1 Reply Last reply
                0
                • J joost versteegen

                  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
                  
                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  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[^]

                  J 1 Reply Last reply
                  0
                  • L Lost User

                    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 setting checkRequiredObjTypesOk randomly to true or false, but never uses that value for any purpose.

                    J Offline
                    J Offline
                    joost versteegen
                    wrote on last edited by
                    #9

                    That part was not finished yet.

                    L 1 Reply Last reply
                    0
                    • L Lost User

                      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[^]

                      J Offline
                      J Offline
                      joost versteegen
                      wrote on last edited by
                      #10

                      No, it does realy jump out of the line i specified. The debugger sometimes does not reach the "if (cb != null)" test!

                      L 1 Reply Last reply
                      0
                      • R Rob Philpott

                        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.

                        J Offline
                        J Offline
                        joost versteegen
                        wrote on last edited by
                        #11

                        How could i put the pdb file back in sync? I already tried a complete rebuild but that didn't help.

                        D 1 Reply Last reply
                        0
                        • J joost versteegen

                          That part was not finished yet.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          OK. However I have just tested your code (modified to fit my sample) and it works fine using both for and foreach. 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.

                          1 Reply Last reply
                          0
                          • J joost versteegen

                            How could i put the pdb file back in sync? I already tried a complete rebuild but that didn't help.

                            D Offline
                            D Offline
                            Dave Kreskowiak
                            wrote on last edited by
                            #13

                            Do a "Clean Project" first, then recompile it.

                            A guide to posting questions on CodeProject

                            How to debug small programs
                            Dave Kreskowiak

                            1 Reply Last reply
                            0
                            • J joost versteegen

                              No, it does realy jump out of the line i specified. The debugger sometimes does not reach the "if (cb != null)" test!

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              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[^]

                              J 1 Reply Last reply
                              0
                              • L Lost User

                                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[^]

                                J Offline
                                J Offline
                                joost versteegen
                                wrote on last edited by
                                #15

                                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!!

                                L 1 Reply Last reply
                                0
                                • J joost versteegen

                                  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!!

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #16

                                  Did you deploy a version to the GAC?

                                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                                  1 Reply Last reply
                                  0
                                  • J joost versteegen

                                    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
                                    
                                    B Offline
                                    B Offline
                                    Bernhard Hiller
                                    wrote on last edited by
                                    #17

                                    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.

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups