Hi,

firstly, sorry about the title of this thread, I couldn't really think of one to explain what I am trying to get at.

In my app I have an XML file that is loaded on formload and looped through to extract the required data into a listbox. That bit is straight forward and works a charm.

However the said XML file is broken down into many individual pages because of a size issue.

Now, also on my form I have 2 buttons, btnNext & btnPrev, so as to be able to navigate through the remaining pages of the XML.

I was wondering if there is a simple way to use the button click events to call either the Next or Previous page without actually having to rewrite the code into each buttons click event?

I understand that I would need to construct a loop to count through avialable pages and display the correct Next or Previous page in relation to the current page that is being used but am kind of at a hurdle and need a few ideas as to the best approach.

Kind regards..,

MT ;)

Recommended Answers

All 5 Replies

>I was wondering if there is a simple way to use the button click events to call either the Next or Previous page without actually having to rewrite the code into each buttons click event?

Attach a common handler (method) to the click event of buttons.

private void Form1_Load(object sender, EventArgs e)
        {
            button1.Tag = "Next"; 
            button2.Tag = "Prev";

            button1.Click += new EventHandler(MyHandler);
            button2.Click+=new EventHandler(MyHandler);
        }

        void MyHandler(object sender, EventArgs e)
        {
            MessageBox.Show((sender as Button).Tag.ToString());
        }

Put the Load Page code in to a functions then call this from formload, previous page and next page.

Put the Load Page code in to a functions then call this from formload, previous page and next page.

Sorry for not responding sooner only for some reason this time Gmail started to class the messages from Daniweb as spam.

Anyway, thanks for replying, I have done what you suggested and it works brilliantly, however, I am still struggling with one small piece.

The form initially loads with page1, now what I need to do is increase the page number by 1 each time the btnNext is clicked.

I have fiddled around a bit but have only got as for as starting with page1, then page11, then page111 and so on - can you see a pattern? ;)

Obviously it needs to go page1, page2 etc...

Here is my button code:-

void BtnTbarNextClick(object sender, EventArgs e)
		{
			// Read the getPublicPhotos XML file and search by tag 'photos'

XmlDocument xDocP = new XmlDocument();
xDocP.Load(System.IO.Path.Combine(Application.StartupPath, "getPublicPhotos.xml"));	
XmlNodeList photos = xDocP.GetElementsByTagName("photos");

// Create loop to catch the values of 'page' & 'pages'
// Make the page number increase by 1 on each press of the button

for(int pNo = 0; pNo < photos.Count; ++ pNo)
{
	string pageNo = photos[pNo].Attributes["page"].Value;
	string Tpages = photos[pNo].Attributes["pages"].Value;
	
	if (Convert.ToInt32(pageNo) <= Convert.ToInt32(Tpages)){
	
WebClient Client = new WebClient ();
string gPP = "http://api.flickr.com/services/rest/?method=flickr.people.getPublicPhotos&api_key="+ KEY + "&user_id=" + ID + "&per_page=5&page=";
Client.DownloadFile(gPP, (System.IO.Path.Combine(Application.StartupPath, "getPublicPhotos.xml")));
theMainBit();
}
}
}

Kind regards..,

p.s. the actual file is held in the

string gPP

.

MT ;)

Unless I am miss-reading your code you don't seem to have anything that records the current page.
Your loop in the BtnTbarNextClick event seems very confusing.

What I think you need is something like this.

// keep current page and any other data at form level
        int CurrentPage = 1;

        private void Form1_Load(object sender, EventArgs e)
        {
            // Code to get page info (store at form level)
            LoadPage(CurrentPage);
        }

        private void LoadPage(int PageNo)
        {
            // code to get single page here.
        }

        private void BtnNext_Click(object sender, EventArgs e)
        {
            CurrentPage++;
            LoadPage(CurrentPage);
        }

        private void BtnPrev_Click(object sender, EventArgs e)
        {
            CurrentPage--;
            LoadPage(CurrentPage);
        }

Unless I am miss-reading your code you don't seem to have anything that records the current page.
Your loop in the BtnTbarNextClick event seems very confusing.

What I think you need is something like this.

// keep current page and any other data at form level
        int CurrentPage = 1;

        private void Form1_Load(object sender, EventArgs e)
        {
            // Code to get page info (store at form level)
            LoadPage(CurrentPage);
        }

        private void LoadPage(int PageNo)
        {
            // code to get single page here.
        }

        private void BtnNext_Click(object sender, EventArgs e)
        {
            CurrentPage++;
            LoadPage(CurrentPage);
        }

        private void BtnPrev_Click(object sender, EventArgs e)
        {
            CurrentPage--;
            LoadPage(CurrentPage);
        }

Great, thanks.

I think I was overdoing it on the coding front, I myself got stuck in a loop ;)

All sorted now.

Kind regards..,

MT :)

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.