C# ListBox控件中item換行 C#重繪ListBox項

C# ListBox控件中item換行 C#重繪ListBox項

 

WindowsForm項目開發中,Listbox控件item數據項,只能一條數據顯示在一行,有的時候內容很長,體驗就非常之差。簡直要歇菜了。哈哈。。。不開玩笑了。下面講下怎麼實現吧!

新建一個Winform項目,命名爲ListAutoline,拖一個按鈕(用來填充數據用的,觸發吧),一個ListBox控件 . 如圖;

弄完之後呢  將listbox屬性項:  把這兩個屬性設置成如圖所示;Enabled可設置可不設置;看個人了; DramMode主要是控制列表框繪製,系統用戶繪製每項;可以理解爲啓用人工繪製的意思啦;

弄玩上面之後,咱們來看一下怎麼繪製這個玩意吧。

先手動做幾條數據吧 搞個Datatable去裝數據;代碼如下

 Font nfont;
        Color colorn;
        int leftMargines;
        int topMargines;

        int distance;
        Font tfont;
        DataTable ListData;

        /// <summary>
        /// 初始化數據
        /// </summary>
        private void FillDataTable()
        {
            ListData = new DataTable("List");
            //ListData.Columns.Add("Caption", System.Type.GetType("System.String"));
            ListData.Columns.Add("Text", System.Type.GetType("System.String"));
            ListData.Rows.Add(
                new object[] 
                { 
                    //"datagram ", 
                    "工站:過誰丟發貨未付哈爾發我份那份辣口味發文分櫱UI違法哈維佛阿文hi服務。" 
                });
            ListData.Rows.Add(
                new object[] 
                { 
                    //"digital signature ",
                    "過誰丟發貨未付哈爾發我份那份辣口味發文分櫱UI違法哈維佛阿文hi服務請檢查連接P上傳"
                });
            ListData.Rows.Add(
                new object[]
                { 
                    //"Digital Signature Standard ",
                    "過誰丟發貨未付哈爾發我份那份辣口味發文分櫱UI違法哈維佛阿文hi服務 "
                });
            ListData.Rows.Add(
                new object[] 
                { 
                   // "Distinguished Encoding Rules  ",
                    "過誰丟發貨未付哈爾發我份那份辣口味發文分櫱UI違法哈維佛阿文hi服務28;CellId[3]:FXP0290DGWFE90228;"
                });
            ListData.Rows.Add(
                new object[] 
                { 
                    //"electronic codebook  ", 
                    "P過誰丟發貨未付哈爾發我份那份辣口味發文分櫱UI違法哈維佛阿文hi服務,請檢查 " 
                });
        }

然後在Button事件下面這樣寫; 就是調用那個FillDataTable();再加個ID啥的;

  private void button1_Click(object sender, EventArgs e)
        {
            
            this.FillDataTable();
            this.listBox1.Enabled = false;
            this.listBox1.Items.Clear();

            for (int i = 0; i <= ListData.Rows.Count - 1; i++)
            {
                this.listBox1.Items.Add(i);
                //this.listBox1.Items.Add(i + ListData.Rows[i]["Text"].ToString());
            }
            this.listBox1.Enabled = true;
        }

做完以上操作之後,選中Listbox  查看事件;

新增兩個事件; 還有

新建這兩個事件;開始重新繪製Listbox;

代碼如下:

  private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
        {

            Graphics gfx = e.Graphics;
            int number = e.Index + 1;

            //string xcaption = ListData.Rows[e.Index]["Caption"].ToString();
            string dtext = ListData.Rows[e.Index]["Text"].ToString();

            SizeF f1 = gfx.MeasureString(number.ToString() + ".", nfont);
           // SizeF f11 = gfx.MeasureString(xcaption, nfont);

            Rectangle rect = new Rectangle(leftMargines + (int)f1.Width + 8, topMargines + (int)f1.Height + distance, this.listBox1.ClientSize.Width - leftMargines - (int)f1.Width - 8, e.ItemHeight);
            StringFormat stf = new StringFormat();
            stf.FormatFlags = StringFormatFlags.FitBlackBox;
            SizeF f2 = gfx.MeasureString(dtext, tfont, rect.Width, stf);
            int Temp = e.ItemWidth;

            //if (f2.Width < (leftMargines + f1.Width + 8 + f11.Width)) 
            //    Temp = leftMargines + (int)f1.Width + 8 + (int)f11.Width + 4;
            if (f2.Width < (leftMargines + f1.Width + 8))
                Temp = leftMargines + (int)f1.Width + 8 + 4;//行間距


            e.ItemHeight = topMargines + (int)f1.Height + distance + (int)f2.Height + 8 + 8;//上下高度
        }

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            int number = e.Index + 1;

			//string xcaption = ListData.Rows[e.Index]["Caption"].ToString();
            string dtext = ListData.Rows[e.Index]["Text"].ToString();

            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                Graphics gfx = e.Graphics;

                Pen pen = new Pen(Brushes.SteelBlue, 0.4f);

                gfx.DrawRectangle(pen, e.Bounds.X + 4, e.Bounds.Y + 4, e.Bounds.Width - 8, e.Bounds.Height - 8);
                gfx.FillRectangle(Brushes.LightSteelBlue, e.Bounds.X + 5, e.Bounds.Y + 5, e.Bounds.Width - 9, e.Bounds.Height - 9);


                gfx.DrawString(number.ToString() + ".", nfont, Brushes.Black, e.Bounds.X + leftMargines, e.Bounds.Y + topMargines);
                SizeF f1 = gfx.MeasureString(number.ToString() + ".", nfont);

                //gfx.DrawString(xcaption, nfont, Brushes.SteelBlue, e.Bounds.X + leftMargines + f1.Width + 8, e.Bounds.Y + topMargines);
                //SizeF f11 = gfx.MeasureString(xcaption, nfont);

                Rectangle rect = new Rectangle(e.Bounds.X + leftMargines + (int)f1.Width + 8, e.Bounds.Y + topMargines + (int)f1.Height + distance, this.listBox1.ClientSize.Width - leftMargines - (int)f1.Width - 8, this.ClientSize.Height);
                StringFormat stf = new StringFormat();
                stf.FormatFlags = StringFormatFlags.FitBlackBox;

                gfx.DrawString(dtext, tfont, Brushes.Black, rect, stf);


            }
            else
            {
                Graphics gfx = e.Graphics;
                gfx.FillRectangle(Brushes.White, e.Bounds.X + 4, e.Bounds.Y + 4, e.Bounds.Width - 7, e.Bounds.Height - 7);
                gfx.FillRectangle(Brushes.SteelBlue, e.Bounds.X + 4, e.Bounds.Y + e.Bounds.Height - 8, e.Bounds.Width - 8, 1);
                Pen pen = new Pen(Brushes.SteelBlue, 0.4f);

                gfx.DrawString(number.ToString() + ".", nfont, Brushes.Black, e.Bounds.X + leftMargines, e.Bounds.Y + topMargines);
                SizeF f1 = gfx.MeasureString(number.ToString() + ".", nfont);

                //gfx.DrawString(xcaption, nfont, Brushes.SteelBlue, e.Bounds.X + leftMargines + f1.Width + 8, e.Bounds.Y + topMargines);
                //SizeF f11 = gfx.MeasureString(xcaption, nfont);

                Rectangle rect = new Rectangle(e.Bounds.X + leftMargines + (int)f1.Width + 8, e.Bounds.Y + topMargines + (int)f1.Height + distance, this.listBox1.ClientSize.Width - leftMargines - (int)f1.Width - 8, this.ClientSize.Height);
                StringFormat stf = new StringFormat();
                stf.FormatFlags = StringFormatFlags.FitBlackBox;
                gfx.DrawString(dtext, tfont, Brushes.Black, rect, stf);
            }
			e.DrawFocusRectangle();
		
        }

然後在窗體Load事件中寫入以下代碼:

 private void ListAutoLine_Load(object sender, EventArgs e)
        {
            nfont = new Font("Microsoft Sans Serif", 10, FontStyle.Bold);
            tfont = new Font("Microsoft Sans Serif", 10, FontStyle.Regular);
            colorn = Color.Black;
            leftMargines = 8;
            topMargines = 10;
            distance = 12;
        }

新建窗體SizeChange事件:

 private void ListAutoLine_SizeChanged(object sender, EventArgs e)
        {
            listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
            listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
        }

好了,以上代碼就實現了ListBox自動換了;寫個事件測試測試;

  private void listBox1_Click(object sender, EventArgs e)
        {
            
            MessageBox.Show("當前選中-[" + listBox1.SelectedItem.ToString().Trim() + "]-項");
            MessageBox.Show(getListText(listBox1.SelectedItem.ToString().Trim()));
        }

 

基本上是沒有什麼問題的啦;

效果圖爲:

就這樣子了;

以上就是本期全部內容了;如有不當之處,還請指正,感謝;歡迎留言討論;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章