เน้นรายการกล่องรายการตามค่าจากคลาส

เป็นไปได้ไหมที่จะวนซ้ำรายการต่างๆ ในกล่องรายการและไฮไลต์หรือระบุความไม่พร้อมใช้งานของรายการโดยการตรวจสอบคลาสเพื่อหาค่า

โดยพื้นฐานแล้ว ได้รับคลาส Game และภายในข้อมูลที่เก็บไว้ว่า Game พร้อมใช้งานหรือไม่ ดังนั้นฉันต้องตรวจสอบคลาสนี้เมื่อวนซ้ำผ่าน ListBox Items และระบุในกล่องรายการว่า GameAvailable = false

มาถึงจุดนี้แล้วไม่รู้จะทำยังไงต่อไป:

private void HighlightUnavailable()
    {
        foreach(string item in listbox_consoles.Items)
        {
            foreach (Products.Game game in GameService.AllGames())
            {
                if (item == game.GameName.ToString())
                {
                    if (game.GameAvailable)
                    {

                    }
                }
            }
        }
    }

person Pawel    schedule 05.05.2020    source แหล่งที่มา
comment
ทำไมไม่ ลบ รายการที่ไม่ต้องการ (ด้วย GameAvailable == false)   -  person Dmitry Bychenko    schedule 05.05.2020
comment
@DmitryBychenko เป็นแบบฟอร์มที่ผู้ใช้สามารถแสดงเกมที่มีอยู่ทั้งหมด แต่ต้องมีการระบุในรายการผลิตภัณฑ์ที่มีอยู่   -  person Pawel    schedule 05.05.2020
comment
นี่คืออะไร? WinForms, WPF, .. ฯลฯ?   -  person    schedule 05.05.2020
comment
@JQSOFT ขอโทษ ฉันควรจะพูดถึงในตอนแรก - มันคือ WinForms   -  person Pawel    schedule 06.05.2020


คำตอบ (1)


ใช่ เป็นไปได้ในลักษณะดังนี้:

  • ผูกกล่องรายการเข้ากับ GameService.AllGames() ซึ่งส่งคืนฉันเชื่อว่าเป็นรายการหรืออาร์เรย์ของวัตถุ Game

  • ตั้งค่า ListBox DrawMode เป็น DrawMode.OwnerDrawFixed และจัดการ ListBox.DrawItem เพื่อวาดไอเท็มตามคุณสมบัติ GameAvailable

สมมติว่าชื่อตัวควบคุมคือ Form1 และ listBox1 ให้เพิ่มในตัวสร้าง Form1:

public Form1()
{
    InitializeComponent();
    //...

    listBox1.DrawMode = DrawMode.OwnerDrawFixed;
    listBox1.DrawItem += (s, e) => OnListBoxDrawItem(s, e); 
    listBox1.DataSource = GameService.AllGames();
}

สมมติว่าคุณต้องการแสดงเกมที่มีอยู่ด้วยสีเขียวและส่วนที่เหลือด้วยสีพื้นหน้าสีแดง

private void OnListBoxDrawItem(object sender, DrawItemEventArgs e)
{
    //Comment if you don't need to show the selected item(s)...
    e.DrawBackground();

    if (e.Index == -1) return;

    var game = listBox1.Items[e.Index] as Game;
    var foreColor = game.GameAvailable ? Color.Green : Color.Red;

    //Pass the listBox1.BackColor instead of the e.BackColor 
    //if you don't need to show the selection...
    TextRenderer.DrawText(e.Graphics, game.GameName, e.Font,
        e.Bounds, foreColor, e.BackColor,
        TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}

... หรือมีสีพื้นหลังต่างกัน:

private void OnListBoxDrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index == -1) return;

    var game = listBox1.Items[e.Index] as Game;          

    var backColor = e.State.HasFlag(DrawItemState.Selected)
        ? e.BackColor
        : game.GameAvailable
        ? Color.LightGreen
        : listBox1.BackColor;

    //Or this if you don't need to show the selection ...
    //var backColor = game.GameAvailable
    //  ? Color.LightGreen
    //  : listBox1.BackColor;

    using (var br = new SolidBrush(backColor))
        e.Graphics.FillRectangle(br, e.Bounds);

    TextRenderer.DrawText(e.Graphics, game.GameName, e.Font,
        e.Bounds, Color.Black, backColor,
        TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}

... หรือสองสาม ใช่ และไม่ No รูปภาพจากแหล่งข้อมูลของคุณ:

Bitmap YesImage, NoImage;

public Form1()
{
    InitializeComponent();
    //...

    YesImage = Properties.Resources.YesImage;
    NoImage = Properties.Resources.NoImage;

    listBox1.DrawMode = DrawMode.OwnerDrawFixed;
    listBox1.DrawItem += (s, e) => OnListBoxDrawItem(s, e);
    listBox1.DataSource = GameService.AllGames();
    this.FormClosed += (s, e) => { YesImage.Dispose(); NoImage.Dispose(); };
}

private void OnListBoxDrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index == -1) return;

    var game = listBox1.Items[e.Index] as Game;
    var backColor = e.State.HasFlag(DrawItemState.Selected)
        ? e.BackColor
        : listBox1.BackColor;
    var bmp = game.GameAvailable ? YesImage : NoImage;
    var rectImage = new Rectangle(
        3, e.Bounds.Y + ((e.Bounds.Height - bmp.Height) / 2),
        bmp.Width, bmp.Height
        );
    var rectTxt = new Rectangle(
        rectImage.Right + 3, e.Bounds.Y,
        e.Bounds.Right - rectImage.Right - 3,
        e.Bounds.Height
        );

    using (var br = new SolidBrush(backColor))
        e.Graphics.FillRectangle(br, e.Bounds);

    e.Graphics.DrawImage(bmp, rectImage);

    TextRenderer.DrawText(e.Graphics, game.GameName, e.Font,
            rectTxt, Color.Black, backColor,
            TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}

SOQ61607771

person Community    schedule 07.05.2020
comment
ขอบคุณ มันใช้งานได้ดี! ไปกับภาพที่ใช่/ไม่ใช่และฉันคิดว่ามันดูดีที่สุด - person Pawel; 12.05.2020
comment
ฉันขอถามได้ไหมว่าคุณได้ไอคอนสีแดงและเขียวมาจากไหน อันที่ฉันมีดูไม่ดีเท่าอันในตัวอย่างของคุณ ขอบคุณ! - person Pawel; 14.05.2020