Search code examples
c#winformslistviewtimer

How do I put subitems in columns right next to a specific column in Listview?


I'm very new to C# and wanted to try some problems. I created a ListView to store the operations of two random numbers, all of which happen in a certain amount of time.

Here's how I constructed the ListView:
First column is called "Start time" to record the time at which the timer started. Second column does the same but for the "End time". Now when it runs, the Listview will programmatically create multiple columns to store the results of the operations.
However, when the program runs, the results will jump in the second column which is reserved to record the "End time".
How do I make sure that the second column is empty until the end of the timer and that results will be put in starting from third column to the end?

Here's the code:

static int k = 10;
ListViewItem lvi1 = new ListViewItem();

private void Bstart_Click(object sender, EventArgs e)
{
    timer1.Start();
    lvi1 = new ListViewItem(DateTime.Now.ToString("HH:mm:ss"));
    listView2.Items.Add(lvi1);
}

private void Timer1_Tick(object sender, EventArgs e)
{
    Random ran1 = new Random();
    Random ran2 = new Random();

    double nu1 = ran1.Next(0, 100);
    double nu2 = ran2.Next(2, 270);

    string op;
    var operation = ran1.Next(1, 5);
    double res;

    if (k > 1)
    {
        label1.Text = k.ToString() + " seconds remaining";
    }
    else if (k == 1)
    {
        label1.Text = k.ToString() + " seconds remaining";
    }
    else
    {
        timer1.Stop();
        lvi1.SubItems[1].Text = DateTime.Now.ToString("HH:mm:ss");
        label1.Text = "You have 10 seconds";
        //MessageBox.Show("You are out of time!", "Too Bad", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
        k = 10;
    }

    k--;

    switch (operation)
    {
        case 1:
            res = nu1 + nu2;
            op = "+";
            break;
        case 2:
            res = nu1 - nu2;
            op = "-";
            break;
        case 3:
            res = nu1 * nu2;
            op = "*";
            break;
        case 4:
            res = nu1 / nu2;
            op = "/";
            break;
        default:
            res = 0;
            op = "?";
            break;
    }

    textBox1.Text = Convert.ToString(nu1 + " " + op + " " + nu2 + " = " + res);
    listView2.Columns.Add("OP", 45, HorizontalAlignment.Center);
    lvi1.SubItems.Add(res.ToString());
}

private void Bstop_Click(object sender, EventArgs e)
{
    timer1.Stop();
}

Solution

  • You can add a placeholder item to fill the second column with string.Empty or - until you want to actually fill it:

    private void Bstart_Click(object sender, EventArgs e)
    {
        timer1.Start();
        lvi1 = new ListViewItem(DateTime.Now.ToString("HH:mm:ss"));
        listView2.Items.Add(lvi1);
    
        // Add an empty SubItem or any placeholder here:
        // lvi1.SubItems.Add(string.Empty);
        lvi1.SubItems.Add("-");
    }
    

    Also like I already mentioned in a comment, your if(k > 1) and else if (k == 1) have exactly the same content. You can merge those:

    if (k >= 1)
    {
        label1.Text = k.ToString() + " seconds remaining";
    }
    else
    {
        timer1.Stop();
        // ...