Hi,

I have some images that has to be displayed on a form from a dynamic folder using control array. The controls display 5X3, that is 15 images on one form. How do I display the next set of images using Next button, and go back to the previous set using the Previous button, if there are more than 15 files in the folder.

Thanks

Recommended Answers

All 38 Replies

As I said before, work it out on paper, you have a few ways to do it. One is messier to code but easier on the eyes, the other a bit more maths but easier to code.

Either way, its a logic issue not a coding issue, so have a go at working out how you might do it.

If nothing else, start a new console based app, and get it to print numbers 1-15 for page 1, and then move to page 2, and back, and so on. so you can concentrate purely on that aspect.

PS now you started a new thread, and the last question was solved, please mark it as such

I will take your valuable advice and try to work out on paper.

Sure, I will mark the previous thread as solved.

Thanks

As I said before, work it out on paper, you have a few ways to do it. One is messier to code but easier on the eyes, the other a bit more maths but easier to code.

Either way, its a logic issue not a coding issue, so have a go at working out how you might do it.

Let me suggest my idea. I would divide the total number of files by 15, get the result and the quotient to get the total number of pages. This is my logic. But don't know how to put it in code. Will try.

Thanks

Suggestion : although it is tempting to use a for-loop I think you better use a while-loop here.
And from 0 to 14 is 15...

Didn't click. What to be done after 14?

Well 15 to 29 is again 15 and so on.
The last part of your file list won't always be exactly 15.
That's why you can use a while-loop to test when that happens.

Well if I gave you 40 peies of paper, and told you to show me the first 15, then the second 15, and then said "next" how do you as a person work it out? now tell your computer exactly the same.

As I mentioned earlier, I would divide by the number by 15, and seperate it to the resulting number of sets, then display each set. But how do I code that? Sorry, if I'm wrong.

Well, as I said before in your other thread theres 2 ways, 1 involves a bit of maths the other involves messier code. However, the second way is bad.

So. Given you have some concept of the maths:

If you had 200 cards, and needed to display them 15 at a time.
How would you in maths terms work out given page = p, what numbers to show for page p?

p=Round(200/15)
p=14
I get 14 pages in total. After displaying the first page, if p>1 then, enable the Next button, click on the Next button to view the next set of 15 images. But how?

OK, but that wasnt the question. That tells you how many pages there are, not how you tell which ones to give in a mathematical equation. Sigh.

If you were counting on your fingers, 6 would be the first finger your hand (wether you used the same hand or both). So, if you had "15" fingers per hand, how would you work out mathematically which finger any given number is, if I said you had counted your hand 12 times, which numbers would you be on..

displaying the images is the same.

Let me ask this a slightly different way to see if it helps you think about it.

You have 200 pictures, numbered 0 to 199. (I chose those numbers because that matches the index in almost all collection data types.)

I propose your screen has a layout something like this:

A   B   C   D   E
   F   G   H   I   J
   K   L   M   N   O
<prev            next>

What picture numbers are on the first page?

What pictures are on the second page?

(If you see a pattern, skip down to the 'N' question, if not fill in a couple more pages and look for the pattern.)

What pictures are on the third page?

What pictures are on the fourth page?

What is the first picture on page 'N'?

So if we had a collection of filenames (or images if you want to try and pre-load them all) and the above formula, we could write a function displayPage(N) to display that page of images from the collection. The display function would enable (or show) the Previous button for all pages except the first and enable (or show) the Next button for all pages except the last. The handler for the buttons would either reduce or increase the page number. (Remember the last page will require a little special handling, as it is likely to not have a full set of images to display.)

So, if you had "15" fingers per hand, how would you work out mathematically which finger any given number is, if I said you had counted your hand 12 times, which numbers would you be on..

:icon_confused:


You have 200 pictures, numbered 0 to 199. (I chose those numbers because that matches the index in almost all collection data types.)

I propose your screen has a layout something like this:

A   B   C   D   E
   F   G   H   I   J
   K   L   M   N   O
<prev            next>

Exactly, this is how I want the form to look.

What picture numbers are on the first page?

0 to 14

What pictures are on the second page?

15 to 29

30 to 44
45 to 59
60 to 74

Looks like I add 15 to a variable
N+15

ok so if n is page, 1, N+15 = 16, so thats the start of page 2, not the start of page 1.....

So, still using N, what would be the beginning and end of page 1?

ok
So, still using N, what would be the beginning and end of page 1?

End of page 1 would be 15 when N=0.
Beginning of page 1 would be 0 when N=-15. :icon_rolleyes:

If the page starts with n+15, then page 1 starts with 16, page 2 with 17, page 3 with 18?

Doesn't seem quite right somehow.

Do we want to number the pages from 0 like we did the pictures, or do we want to number them from 1 like the user might expect?

Then it would appear that addition is not the correct mathematical operation to apply.

So, we've kinda proved you need a rethink.
Have another go - the answer will come, honest.

My thinking is like, if there are 50 files, how many pages do I have to divide these files to? If N is the no. of pages, then

N = (50 / 15) + (50 % 15)
That will give me 4 pages.
x = 1
N1 = x to x + 15
x = x + 15
N2 = x to x +15
x = x + 15
N3 = x to x + 15
x = x + 15
N4 = x to total number of files.

Please correct me.

x = 1
N1 = x to x + 15
x = x + 15
N2 = x to x +15
...

The 16th file will be displayed on N1 and on N2. I suppose you need this correction:

x = 0
N1 = x + 1 to x + 15
x = x + 15
N2 = x + 1 to x +15
...

Do you now see how to do the code?

Well....let me try :)

Could you give me a start please? :-/

Given the amount of hints people have given you give it a go, if something this simple baffles you what are you going to do with a big problem?

yea...you are right.....but I had been struggling with this code for some time.....as I'm a beginner in C#. :(

the programming language has nothing to do with it. You didnt even do much to work out the logic - everyone else did it for you.

Try.
I've seen enough of your code to know you can do it

This is what I did so far. I'm still stuck with the logic. :'(

public partial class MainFrm : Form
    {
        private AxSlide.AxSlide[] sldArray;
        private System.Windows.Forms.Label[] lblArray;
        private int sldNum;
        private string parPath = Properties.Settings.Default.ParentPath.ToString();
        private FileInfo[] filCol;
        private int iCnt=15;
        private int n = 0;

        public MainFrm()
        {
            InitializeComponent();
            DirectoryInfo di = new DirectoryInfo(parPath);

            DirectoryInfo[] subDirs = di.GetDirectories();

            btnNext.Enabled = false;
            btnPrev.Enabled = false;

            foreach (DirectoryInfo subDir in subDirs)
            {
                cboDept.Items.Add(subDir.Name);
            }

            cboDept.Sorted = true;
            cboDept.SelectedIndex = 0;
        }

        private void TV_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.groupBox2.Controls.Clear();

            TreeNode newSelected = e.Node;
            DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
            filCol = nodeDirInfo.GetFiles("*.sld");
            sldNum = nodeDirInfo.GetFiles("*.sld").Count();

            if (sldNum > 0)
            {
                AddControls("sld", sldNum);
                if (sldNum < 15)
                {
                    ShowSld(sldNum);
                    n = 0;
                }
                else
                {
                    ShowSld(15);  //don't know ????
                }
            }
        }

        private void ShowSld(int sldCnt)
        {

            int xPos = 10;// 140;
            int yPos = 23;// 140;
             while (n < sldCnt)
            {
                    sldArray[n].Tag = n;
                    sldArray[n].Width = 125;
                    sldArray[n].Height = 115;
                    if (xPos > 570)
                    {
                        xPos = 10;
                        yPos = yPos + sldArray[n].Height + 35;
                    }
                    sldArray[n].Left = xPos;
                    sldArray[n].Top = yPos;
                    xPos = xPos + sldArray[n].Width + 15;

                    groupBox2.Controls.Add(sldArray[n]);
                    sldArray[n].FileName = filCol[n].FullName;

                    n++;
            }

            if (sldNum > 15)
            {
                btnNext.Enabled = true;
            }
            else
            {
                btnNext.Enabled = false;
            }
        }

        private void AddControls(string anyControl, int cNumber)
        {
            switch (anyControl)
            {
                case "sld":

                    sldArray = new AxSlide.AxSlide[cNumber + 1];
                    for (int i = 0; i < cNumber + 1; i++)
                    {
                        sldArray[i] =new AxSlide.AxSlide();
                    }
                    break;
             }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            iCnt = iCnt + 15;   //urrhhhh eeeekk........
            if (iCnt < sldNum)
            {
                ShowSld(iCnt);
            }                       
        }

    }

So, you didnt really read the huge hints everyone gave you then

I did this code and it works! :ooh:

private FileInfo[] filCol;
private int n = 0;
private int iNext = 1;
private int start;
private int end;

private void TV_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.groupBox2.Controls.Clear();
            n = 0;

            TreeNode newSelected = e.Node;
            DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
            filCol = nodeDirInfo.GetFiles("*.sld");
            sldNum = nodeDirInfo.GetFiles("*.sld").Count();

            btnPrev.Enabled = false;

            if (sldNum > 15)
            {
                btnNext.Enabled = true;
            }

            if (sldNum > 0)
            {
                AddControls("sld", sldNum);
                if (sldNum < 15)
                {
                    ShowSld(sldNum);
                    n = 0;
                }
                else
                {
                    ShowSld(15);
                }
            }
        }

private void btnNext_Click(object sender, EventArgs e)
        {
            btnPrev.Enabled = true;
                start = n + 1;
                end = n + 15;
                if (end > sldNum)
                {
                    end = start + (sldNum % 15) - 1;
                    btnNext.Enabled = false;
                }
                ShowSld(end);
                iNext++;
        }

private void ShowSld(int sldCnt)
        {

            int xPos = 10;// 140;
            int yPos = 23;// 140;
            this.groupBox2.Controls.Clear();
            while (n < sldCnt)
            {
                    sldArray[n].Tag = n;
                    sldArray[n].Width = 125;
                    sldArray[n].Height = 115;
                    if (xPos > 570)
                    {
                        xPos = 10;
                        yPos = yPos + sldArray[n].Height + 35;
                    }
                    sldArray[n].Left = xPos;
                    sldArray[n].Top = yPos;
                    xPos = xPos + sldArray[n].Width + 15;

                    groupBox2.Controls.Add(sldArray[n]);
                    sldArray[n].FileName = filCol[n].FullName;

                    n++;
            }
         }

Hope it's correct.

Next step is to write code for Previous Button.

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.