Tags:

ถามหน่อยครับ ถ้าเกิดผมอยากออกแบบ listbox แบบนี้ จะต้องทำยังไงหรอครับ

images by free.in.th

คืออยากให้ใน 1 รายการสามารถเก็บข้อความได้สัก 2 บรรทัด แล้วสามารถกดเครื่องหมาย x ลบรายการได้อ่ะครับ แบบนี้ใน Visual Studio ทำได้หรือเปล่าหรอครับ ไม่ทราบว่ามีเทคนิคอย่างไร หรือว่าต้องใช้โปรแกรมอื่น

Get latest news from Blognone
By: Architec
ContributorWindows PhoneAndroidWindows
on 28 April 2012 - 19:40 #413747

ถ้าทำลงใน ListBox ใน WinForm นี่คงจะเป็นไปไม่ได้เลย คงต้องหาตัวอื่นแทน

แต่ใน WPF ทำได้ครับ เรียกว่า Stack Panel (ทำแบบเดียวกันเด๊ะ -*-)

By: varavut
ContributorWindows PhoneAndroidBlackberry
on 28 April 2012 - 21:37 #413785

ถ้าใช้ WPF น่าจะสร้าง ListBox ItemTemplate ได้ครับ ซึ่งเราสามารถ customize ได้ตามใจชอบเลย อยากได้อะไรก็ใส่ลงไปได้ http://www.wpftutorial.net/ListBoxDataTemplate.html

By: soginal
AndroidIn Love
on 29 April 2012 - 04:10 #413864
soginal's picture

ถ้าให้ง่ายก็ลองหา control ที่เขาแจกในเน็ตมาใช้(น่าจะมี...มั้ง)

หรือถ้าชอบท่ายาก ก็กำหนด drawmode เป็น OwnerDrawFixed(เมื่อทุก item มีขนาดเท่ากัน)
หรือ OwnerDrawVariable(เมื่อ item มีขนาดไม่เท่ากัน และทำการกำหนดขนาดแต่ละ item ด้วย event MeasureItem)

หลังจากนั้นก็ทำการวาดแต่ละ item ใน event DrawItem โดย event นี้จะมี parameter e.Graphics ซึ่งนำมาใช้วาดรูปได้

ยกตัวอย่างนะครับ


string text = listBox1.Items[e.Index].ToString();
if (e.Index == 0)
e.Graphics.FillRectangle(new SolidBrush(Color.AliceBlue), e.Bounds);
else
e.Graphics.FillRectangle(new SolidBrush(Color.Beige), e.Bounds);

e.Graphics.DrawString(text,SystemFonts.CaptionFont, SystemBrushes.WindowText, e.Bounds.Left, e.Bounds.Top);

ส่วนปุ่มกดลบเนี่ย คำนวนพิกัดการกดเอาเองครับ

อีกวิธีนึงก็ สร้าง UserControl ครับ

By: livencm
Windows
on 30 April 2012 - 01:08 #414086

ขอบคุณทุกๆท่านมากๆคร๊าบบบบบ...