简体   繁体   中英

c# loop through list view items and display each in a text box a row at a time with a click of a button

I need to displaying items of the 4th column in a textbox row by row after clicking 'next' button. When the user clicks next, the next row should be displayed until the last row.

Below is the code I have so far, that only displays the last row when button 'next' is clicked. I cannot seem to get the loop right.

public partial class RecordingView : Form
{
    public RecordingView()
    {
        InitializeComponent();
    }

    public void UpdateListView(ListView listView1)
    {
        foreach (ListViewItem item in listView1.Items)
        {
            this.listView1.Items.Add((ListViewItem)item.Clone());
        }
    }

    private void RecordingView_Load(object sender, EventArgs e)
    {
        //Add column header
        listView1.Columns.Add("#", 60);
        listView1.Columns.Add("Start time", 100);
        listView1.Columns.Add("End time", 100);
        listView1.Columns.Add("Duration", 100);
        listView1.Columns.Add("Text", 350);
    }

    private void btnNext_Click(object sender, EventArgs e)
    {

        for (int i = 0; i < listView1.Items.Count; i++)
        {
            string line = listView1.Items[i].SubItems[4].Text; //picks list item

            rtbCurrentLine.Text = line;
        }

    }
}

Below is my user interface

user interface

kindly help me figure out where I have gone wrong.

I suggest this, declare a global variable that will get your index of next or previous click:

int intIndex = -1;

Then on your btnNext click event handler, increment it per click:

private void btnNext_Click(object sender, EventArgs e)
{   
        if (intIndex <= listView1.Items.Count)
        {
          intIndex++;
          string line = listView1.Items[intIndex].SubItems[4].Text;
          rtbCurrentLine.Text = line;
        }  
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM