Hi!!!
I have made a window application in which when a string is entered in textbox , the related links are displayed in listview.
Since ,there can be any number of links related to that string. So,I want to display only 5-7 links at a time in listview and on clicking on "NEXT" button it should display other links.But at a time there should be only 5-7 links in listview .Like in google search we have some links on one page and as we click on next we can see other links.Similar functionality I want in window application.
Can anyone help me in this.............. :pretty:

Try something like this.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.paging
{
  public partial class Form1 : Form
  {
    private const int RECORDS_PER_PAGE = 10;
    private List<string> lstAllLinks;
    private List<string> currentSearchResults;

    private int curPage;
    private int maxPage;

    public Form1()
    {
      InitializeComponent();
      lstAllLinks = new List<string>();
      currentSearchResults = new List<string>();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      for (int i1 = 0; i1 < 500; i1++)
      {
        lstAllLinks.Add(i1.ToString() + " - " + @"http://" + Guid.NewGuid().ToString() + ".com");
      }
      DoSearch();
    }

    private void DoSearch()
    {
      string term = textBox1.Text.Trim();
      currentSearchResults.Clear();
      if (string.IsNullOrEmpty(term)) //all results
      {
        currentSearchResults.AddRange(lstAllLinks.ToArray());
      }
      else
      {
        for (int i1 = 0; i1 < lstAllLinks.Count; i1++)
        {
          if (lstAllLinks[i1].IndexOf(term, StringComparison.CurrentCultureIgnoreCase) >= 0)
            currentSearchResults.Add(lstAllLinks[i1]);
        }
      }
      curPage = 1;
      maxPage = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(((double)currentSearchResults.Count / (double)RECORDS_PER_PAGE))));
      textBoxTotalPage.Text = maxPage.ToString();
      DisplayResults();
    }

    private void DisplayResults()
    {
      textBoxCurrentPage.Text = curPage.ToString();
      
      int recStart = 0 + (RECORDS_PER_PAGE * (curPage - 1));
      int recEnd = recStart + RECORDS_PER_PAGE;
      listView1.BeginUpdate();
      listView1.Items.Clear();
      for (int i1 = recStart; i1 < recEnd; i1++)
      {
        if (currentSearchResults.Count > i1)
          listView1.Items.Add(currentSearchResults[i1]);
        else
          break;
      }
      listView1.EndUpdate();
      buttonNextPage.Enabled = (curPage < maxPage);
      buttonPrevPage.Enabled = (curPage > 1);
    }

    private void buttonSearch_Click(object sender, EventArgs e)
    {
      DoSearch();
    }

    private void buttonNextPage_Click(object sender, EventArgs e)
    {
      curPage++;
      DisplayResults();
    }

    private void buttonPrevPage_Click(object sender, EventArgs e)
    {
      curPage--;
      DisplayResults();
    }

  }
}

I have attached my sample project.

Hey sknake :) Great code, i just had one query. Is it better to manually loop through the array to find matches? I use predicates with the List.Find method. Is there any kind of performance boost for either approach?

Either way is fine. I was just keeping it simple for the sake of readability.

Please mark this thread as solved if you have found an answer to your question and good luck!

Well thanks for your reply sknake....
Actually I tried reverse of this i.e. first searching a string and then display the search results 5 at a time .
I am searching the string "abc" and it should show 5 a time and click "next" to others.
My code is attached here which i have tried, Kindly help me in this.

I remeber this search code from somewhere ... ;)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CompareString
{
  public partial class Form1 : Form
  {
    //
    private const int RECORDS_PER_PAGE = 5;
    //
    private SearchResult[] currentSearchResults;
    private int curPage;
    private int maxPage;
    //
    public Form1()
    {
      InitializeComponent();
      currentSearchResults = default(SearchResult[]);
    }

    private void buttonSearch_Click(object sender, EventArgs e)
    {
      if (string.IsNullOrEmpty(textBox1.Text.Trim()))
      {
        MessageBox.Show("Enter your search term");
        textBox1.Focus();
        return;
      }
      else
      {
        DoSearch();
      }
    }

    private void DoSearch()
    {
      if (string.IsNullOrEmpty(textBox1.Text))
      {
        currentSearchResults = default(SearchResult[]);
      }
      else
      {
        FormSearcher fs = new FormSearcher();
        fs.ExcludeTypes.Add(this.GetType());
        currentSearchResults = fs.FindString(textBox1.Text);
      }

      curPage = 1;
      maxPage = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(((double)currentSearchResults.Length / (double)RECORDS_PER_PAGE))));
      textBoxTotalPage.Text = maxPage.ToString();
      DisplayResults();
    }

    private void DisplayResults()
    {
      textBoxCurrentPage.Text = curPage.ToString();
      int recStart = 0 + (RECORDS_PER_PAGE * (curPage - 1));
      int recEnd = recStart + RECORDS_PER_PAGE;
      listView1.BeginUpdate();
      listView1.Items.Clear();
      if (currentSearchResults != null)
      {
        for (int i1 = recStart; i1 < recEnd; i1++)
        {
          if (currentSearchResults.Length > i1)
          {
            ListViewItem lvi = listView1.Items.Add(currentSearchResults[i1].FormType.Name);
            lvi.SubItems.Add(currentSearchResults[i1].Caption);
            lvi.Tag = currentSearchResults[i1];
          }
          else
            break;
        }
      }
      listView1.EndUpdate();
      buttonNext.Enabled = (curPage < maxPage);
      buttonPrev.Enabled = (curPage > 1);
    }

    private void listView1_DoubleClick(object sender, EventArgs e)
    {
      if (listView1.SelectedItems.Count != 1)
        return;

      SearchResult sr = (SearchResult)listView1.SelectedItems[0].Tag;
      using (Form f = (Form)Activator.CreateInstance(sr.FormType))
      {
        f.StartPosition = FormStartPosition.CenterParent;
        f.ShowDialog();
      }
    }

    private void buttonNext_Click(object sender, EventArgs e)
    {
        curPage++;
        DisplayResults();
    }

    private void buttonPrev_Click(object sender, EventArgs e)
    {
        curPage--;
        DisplayResults();
    }
      
    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
  }
}

thanks a lot..........

hello Sknake......I need your help..........And this is one and the last problem related to this thread.........
I know its wrong to extend this thread , but I know you are only one who can help me out.
In my application I have made forms on which there are two panels each having a picturebox.
Here, when user enters the string in textbox for searching and the Text(caption) of the matched string are displayed.
But I want that it should display the image of picturebox1 of each matched form in the listview along with the caption. And when we click on image ,it should display the picturebox2 of that form(if possible) otherwise show the complete corresponding form.

Kindly help me ,please.............

Mark this thread as solved ... create a new ... and we'll go from there. Your requirements have changed. I normally say don't send me a private message but I have been busy lately and not checking all the threads so let me know when you start your new thread.

Mark this thread as solved ... create a new ... and we'll go from there. Your requirements have changed. I normally say don't send me a private message but I have been busy lately and not checking all the threads so let me know when you start your new thread.

Hi Sknake i started a new thread "Display the Results".
Kindly help me in that thread.............

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.