//-----------------------------------------------------------
                  /// <summary>
                  
          /// DropDown時発生します。
                  
          /// </summary>
                  //-----------------------------------------------------------
                  protected override void OnDropDown(EventArgs e)
                  {
                      this.BackColor = backColor;
                      AdjustDropDownWidth();
                      base.OnDropDown(e);
                  }

                  //-----------------------------------------------------------
                  /// <summary>
                  
          /// AdjustDropDownWidth
                  
          /// </summary>
                  //-----------------------------------------------------------
                  private void AdjustDropDownWidth()
                  {
                      Graphics g = this.CreateGraphics();
                      Font font = this.Font;
                      string str = "";
                      int width = this.Width;
                      int vScrollWidth = 0;
                      int newWidth = 0;

                      try
                      {
                          vScrollWidth = (this.Items.Count > this.MaxDropDownItems) ?
                              SystemInformation.VerticalScrollBarWidth : 0;

                          foreach (object s in this.Items)
                          {
                              if (s != null)
                              {
                                  if (this.DataSource == null)
                                      str = s.ToString();
                                  else if (s is DataRow)
                                      str = ((DataRow)s)[this.DisplayMember].ToString();
                                  else if (s is DataRowView)
                                      str = ((DataRowView)s)[this.DisplayMember].ToString();
                                  else
                                      str = " ";

                                  newWidth = (int)g.MeasureString(str.ToString().Trim(), font).Width + vScrollWidth;
                                  if (width < newWidth)
                                      width = newWidth;
                              }
                          }
                          this.DropDownWidth = width;
                      }
                      catch (Exception e)
                      {
                          throw e;
                      }
                      finally
                      {
                          if (g != null)
                              g.Dispose();
                      }
                  }
          posted on 2016-04-07 15:19 Ying-er 閱讀(274) 評論(0)  編輯  收藏 所屬分類: .Net
          主站蜘蛛池模板: 红河县| 湾仔区| 新民市| 扶沟县| 巴楚县| 临邑县| 那坡县| 乐平市| 枝江市| 麦盖提县| 南雄市| 卓资县| 双桥区| 潮安县| 元氏县| 大港区| 铁力市| 连州市| 汕头市| 普定县| 大足县| 岳阳县| 乌兰察布市| 钦州市| 丹阳市| 疏附县| 偏关县| 错那县| 崇明县| 上虞市| 镇巴县| 新蔡县| 达日县| 当阳市| 图木舒克市| 富源县| 江川县| 郎溪县| 龙岩市| 沈丘县| 洪洞县|