//-----------------------------------------------------------
                  /// <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
          主站蜘蛛池模板: 建昌县| 巴彦淖尔市| 印江| 泸西县| 红桥区| 甘肃省| 罗田县| 长岛县| 拉萨市| 新化县| 文登市| 韩城市| 娱乐| 嘉义市| 顺平县| 张家界市| 临猗县| 枣阳市| 驻马店市| 石林| 澜沧| 基隆市| 阿克苏市| 台东县| 洮南市| 克东县| 布拖县| 浦北县| 涡阳县| 沁水县| 来宾市| 安义县| 富蕴县| 祁东县| 健康| 郑州市| 彰化市| 英超| 新竹市| 龙井市| 文山县|