//-----------------------------------------------------------
                  /// <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
          主站蜘蛛池模板: 青神县| 西和县| 彝良县| 民县| 张家口市| 祁门县| 三原县| 封丘县| 健康| 兴安县| 噶尔县| 耿马| 清徐县| 孟连| 沙田区| 松桃| 博客| 永靖县| 石棉县| 高青县| 芦溪县| 漠河县| 凤城市| 弋阳县| 郧西县| 辽宁省| 密云县| 千阳县| 那坡县| 军事| 米易县| 铅山县| 安新县| 巧家县| 南开区| 永康市| 西畴县| 台南市| 温州市| 盐源县| 百色市|