//-----------------------------------------------------------
                  /// <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 閱讀(275) 評(píng)論(0)  編輯  收藏 所屬分類: .Net
          主站蜘蛛池模板: 股票| 定日县| 荆州市| 中江县| 收藏| 蒲城县| 临颍县| 武鸣县| 纳雍县| 叶城县| 龙游县| 南漳县| 兴城市| 凉山| 鄂托克旗| 孝义市| 鸡泽县| 翁牛特旗| 句容市| 镇坪县| 衡阳市| 泰和县| 合水县| 桓台县| 嘉义县| 威海市| 广德县| 名山县| 虹口区| 旺苍县| 买车| 兴和县| 上高县| 佛山市| 安康市| 包头市| 阿拉善左旗| 康马县| 福泉市| 阳西县| 钟祥市|