title: C# Form/Control Width Not Updating All the Time
body: <p>I've made a small tool bar that sits in a transparent form, it loads a variable sized menu from a text file and can be changed on the fly. Each button is a type of Label, the bar is just a list of buttons and adds/removes them in the correct spots. Width of the form is only a little bigger than the menu bar so that sub menu isn't cut off</p>
  <p>Everything is working sweet except, when I reload everything part of the toolbar is lost. I've attempted to change the width so many ways, I've cleared and removed the controls from the form, refreshing the form/menu, updating it etc however nothing seems to make it work as intended EXCEPT if I call the reload function twice in a row, it works. I can't see why calling it once doesn't work but calling it twice works.</p>
  <p>I'm fine with calling reload twice in a row as it would only be called a couple times a week.</p>
  <p>Question: what on earth is causing this?</p>
  <p><a href="https://i.stack.imgur.com/ZpaFW.png" rel="nofollow noreferrer">photo of issues</a> first photo shows what it should look like, second is after removing a menu button and reloading, third is after adding a button and reloading</p>
  <pre><code>//calling this.reload() doesn't work
  //calling this.reload();this.reload() works
      void reload(Object o = null, EventArgs e = null)
      {
          this._menuBar.clear();
          this.loadFromFile();
      }
  void loadFromFile(Object o = null, EventArgs e = null)
      {
          try
          {
              if (File.Exists(&quot;kpi.txt&quot;))
              {
                  string cline = &quot;&quot;, cmenu = &quot;&quot;, lhs = &quot;&quot;;
                  menuList mb = null;
  
                  StreamReader sr = new StreamReader(&quot;kpi.txt&quot;);
                  while (!sr.EndOfStream)
                  {
                      cline = sr.ReadLine();  //get current line
                      if (cline.Length &gt; 0 &amp;&amp; cline[0] != ';')
                      {
                          //check if main menu/command
                          if (cline[0] == '[')
                          {
                              cmenu = Regex.Match(cline, @&quot;(?&lt;=^\[)[a-zA-Z -\@_{-~\^\r\n]+(?=\])&quot;).Value;
                              if (cmenu != &quot;&quot;)
                              {
                                  mb = this._menuBar.addMenuButton(cmenu);
                                  mb.data[&quot;options&quot;] = Regex.Match(cline, @&quot;\/\w+$&quot;).Value;
                                  var match = Regex.Match(cline, @&quot;(?&lt;=&lt;)([^&gt;\[\]\r\n]+)(?=&gt;)&quot;);
                                  mb.data[&quot;count&quot;] = (match.Success ? match.Value : &quot;0&quot;);
                                  mb.data[&quot;copy&quot;] = &quot;&quot;;
                                  applyMenuOptions(mb, false);
                              }
                          }
                          //just a standard line
                          else
                          {
                              cline = cline.Trim();
                              lhs = Regex.Match(cline, @&quot;^[^\;\&lt;\[\]\r\n]+(?=$|\&lt;|\;)&quot;).Value;
                              if (mb.getSubMenuItem(lhs) == null)
                              {
                                  var newButton = mb.addSubMenu(lhs);
                                  if (newButton != null)
                                  {
                                      newButton.parent = mb;
                                      newButton.data[&quot;options&quot;] = mb.data[&quot;options&quot;];
                                      newButton.data[&quot;copy&quot;] = Regex.Match(cline, @&quot;((?&lt;=\;)[^\[\]\&lt;\r\n]+(?=&lt;|$))&quot;).Value;
                                      var matches = Regex.Match(cline, @&quot;(?&lt;=&lt;)([^&gt;\[\]\r\n]+)(?=&gt;)&quot;);
                                      int intout = 0;
                                      if (int.TryParse(matches.Value, out intout))
                                      {//no description
                                          newButton.data[&quot;description&quot;] = &quot;&quot;;
                                          newButton.data[&quot;count&quot;] = intout.ToString();
                                      }
                                      else
                                      {
                                          newButton.data[&quot;description&quot;] = matches.Value;
                                          newButton.data[&quot;count&quot;] = (matches.NextMatch().Success ? matches.NextMatch().Value : &quot;0&quot;);
                                      }
                                      applyMenuOptions(newButton);
                                      newButton.addMiddleClick(this.addcopy);
                                      if (newButton.data[&quot;options&quot;].Contains(&quot;i&quot;))
                                      {
                                          newButton.addRightClick(this.appendInfo);
                                          newButton.addRightClick(this.increment);
                                      }
                                  }
                              }
                          }
                      }
                  }
                  sr.Close();
                  this._menuBar.squish();
                  this.Width = this._menuBar.Width+50;
              }
              else
              {
                  menuList mb = this._menuBar.addMenuButton(&quot;menu&quot;);
                  mb.data[&quot;options&quot;] = &quot;\\m&quot;;
                  mb.data[&quot;count&quot;] = &quot;0&quot;;
                  mb.data[&quot;copy&quot;] = &quot;&quot;;
                  mb.data[&quot;description&quot;] = &quot;&quot;;
                  applyMenuOptions(mb, false);
                  saveDictonary();
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(&quot;Failed to load data &quot; + ex);
              //ILog log = LogManager.GetLogger(typeof(Program));
              //log.Info(ex);
          }
      }
  
      public menuList addMenuButton(string s, int w = 0, int h = 0, int x = -1, int y = -1)
      {
          menuList mb = new menuList(this._form, s);
  
          if (this.menuItems.Exists(z =&gt; z.Text == s)) return null;
  
          mb.Width = (w==0?settings.intOf(&quot;ButtonWidth&quot;):w);
          mb.Height = (h==0?settings.IntOf(&quot;ButtonHeight&quot;):h);
          if (x == -1 || y == -1)
              mb.Location = new Point(this.menuItems.Count &gt; 0 ? this.menuItems.Last().Location.X + this.menuItems.Last().Width : padding);
          else mb.Location = new Point(x, y);
  
          mb.BringToFront();
          mb.Show();
          this.menuItems.Add(mb);
       //   this.Refresh();
  
          return mb;
      }
  
      internal void clear()
      {
          foreach(var i in this.menuItems)
          {
              this._form.Controls.Remove(i);
              i.clear();
             i.Dispose();
          }
          this.menuItems.Clear();
          this._form.Controls.Remove(this);
  
  
          this.menuItems = new List&lt;menuList&gt;();
          this._form.Controls.Add(this);
      }
  
      internal void squish()
      {
          try
          {
              this.Width = (this.menuItems.Count * this.menuItems.First().Width) + (2 * padding);
          }
          catch(Exception ex) { MessageBox.Show(&quot;&quot;+ex); }
      }
  </code></pre>