当前位置:网站首页>C listbox usage

C listbox usage

2022-06-09 14:31:00 Xiongsiyu

stay Winform in ,ListBox It's good to display logs , Pictured :

usage :

/// <summary>
///  Add normal log 
/// </summary>
/// <param name="content"> Content </param>
public void AddOrdinaryLog(string content)
{
    if (this.InvokeRequired)
    {
        // Switch to UI Threads 
        this.Invoke(new MethodInvoker(delegate
        {
            AddOrdinaryLog_UI(content);
        }));
    }
    else
    {
        AddOrdinaryLog_UI(content);
    }
}


private void AddOrdinaryLog_UI(string content)
{
    // Read the current ListBox List length 
    int len = ListBox_OrdinaryLogList.Items.Count;
    // Insert a new line 
    ListBox_OrdinaryLogList.Items.Insert(len, content);
    // The list length is greater than 30, Then delete No 1 Row data 
    if (len > 30)
        ListBox_OrdinaryLogList.Items.RemoveAt(0);
    // After inserting new data , Move the scroll bar to the bottom 
    int visibleItems = ListBox_OrdinaryLogList.ClientSize.Height / ListBox_OrdinaryLogList.ItemHeight;
    ListBox_OrdinaryLogList.TopIndex = Math.Max(ListBox_OrdinaryLogList.Items.Count - visibleItems + 1, 0);
}

Here I add a thread judgment , No need to copy directly  AddOrdinaryLog_UI This method can .

end

原网站

版权声明
本文为[Xiongsiyu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091320584584.html