//-----------------------------------------------------------
                  /// <summary>
                  
          /// DropDown時(shí)発生します。
                  
          /// </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 閱讀(271) 評(píng)論(0)  編輯  收藏 所屬分類: .Net
          主站蜘蛛池模板: 苍山县| 郸城县| 江永县| 和硕县| 邵阳县| 高清| 色达县| 天峻县| 普陀区| 阿城市| 七台河市| 磐石市| 常州市| 吕梁市| 江城| 称多县| 隆安县| 宁城县| 兰坪| 东至县| 杭锦后旗| 高陵县| 苍山县| 南充市| 潞西市| 满城县| 聊城市| 邵阳县| 临沧市| 湘西| 新绛县| 乾安县| 东乌珠穆沁旗| 同德县| 明星| 白朗县| 宜阳县| 双峰县| 林州市| 漳州市| 隆化县|