Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
could please help me to find a solution?
I am trying to fill in the flowlayoutpanel1 with 9 items associated with an  event.  I'm using next and previous buttons to navigate through it, but when I click the previous button I face index was out of range problem with clicking one of the items as well flowlayoutpanel1 decreased to 6 items:


What I have tried:

C#
private void frmelection_Load(object sender, EventArgs e)
       {
           Cards = new List<Candidate>();
           btnprevious.Enabled = false;
           
           var cand = cancontroller.GetAllCandidates();
           if (cand.Count() <= 9)
           {
               btnnext.Enabled = false;
           }
           foreach (var i in cand)
           {
               Cards.Add(new Candidate{
                   ID=i.ID,
                   name=i.name,
                   candidatepic=i.candidatepic,
                   logoID=i.logoID
               });             
           }
   
           for (int i = 0; i < 9; i++)
           {
               //Check if Cards Has the Index
   
               if (Cards.Count - 1 < i)
                   break;
               indexer++;
               Candpanel c = new Candpanel();
               c.Controls.Add(addlblID(Cards[i].ID));
               c.Controls.Add(addlblName(Cards[i].name));
               c.Controls.Add(addpic(Cards[i]));
               flowLayoutPanel1.Controls.Add(c);
               c.DoubleClick += new EventHandler((s, ee) => this.CandpanelDoubleClick(s, e, Cards[i]));
           }
       }

       private void btnprevious_Click(object sender, EventArgs e)
       {
           FLPContainer.Controls.Clear();
           btnnext.Enabled = true;
           var index = indexer - 9;
   
           try
           {
               for (int i = index; i >= 0; i--)
               {
                   //Check if Cards Has the Index
                   if (Cards.Count - 1 < i)
                       break;
   
                   // Preventing User From Going Forward Case the List is Back Default
                   if (i == 9)
                       btnprevious.Enabled = false;
   
                   indexer++;
                   var c = new Candpanel();
                   c.Controls.Add(addlblID(Cards[i].ID));
                   c.Controls.Add(addlblName(Cards[i].name));
                   c.Controls.Add(addpic(Cards[i]));
                   c.DoubleClick += new EventHandler((s, ee) => this.CandpanelDoubleClick(s, e, Cards[i]));
                   flowLayoutPanel1.Controls.Add(c);
               }
           }
           catch
           {
               return;
           }
       }
Posted
Updated 13-Aug-20 23:39pm
Comments
Luc Pattyn 14-Aug-20 5:54am    
 
          catch
           {
               return;
           }

ignoring exceptions like that is a horrible idea. They offer the first clue when things go terribly wrong, so always make sure you see them.
Richard MacCutchan 14-Aug-20 7:34am    
Also you are using a hard coded loop limit of 9 but using Cards.Count to break out of your loop. Use only Cards.Count, as it is the only one that is guaranteed to be accurate.

Reference: IndexOutOfRangeException Class (System) | Microsoft Docs[^]
Quote:
The exception that is thrown when an attempt is made to access an element of an array or collection with an index that is outside its bounds.


With yu logic of next/previous, you need to make sure at any given point of time, the value of i is always between 0-8 (as you shared there are 9 items).

My guess, if you use a DEBUGGER in your IDE, you will find that whenever the value is outside the range of 0-8 (either -1 or 9) it would throw the error.
 
Share this answer
 
We can't help you directly with this: it requires both your data - which we don;t have - and your code running to fix it, and we can;t run that code in isolation!.

So, it's going to be up to you.
the name of your IDE and "debugger" should give you the info you need.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900