//-----------------------------------------------------------
                  /// <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 閱讀(271) 評論(0)  編輯  收藏 所屬分類: .Net
          主站蜘蛛池模板: 太仓市| 阿图什市| 罗江县| 榆中县| 乐安县| 贡觉县| 长治县| 新绛县| 临城县| 克拉玛依市| 普洱| 昌平区| 普定县| 麻城市| 乌审旗| 成安县| 泌阳县| 连江县| 宣化县| 长乐市| 甘洛县| 体育| 集贤县| 望谟县| 黄龙县| 肥乡县| 武乡县| 仙居县| 监利县| 广河县| 安多县| 大厂| 双柏县| 沙田区| 绥阳县| 兰考县| 东宁县| 永昌县| 荣昌县| 临沭县| 喀喇沁旗|