-1

Greetings I have a combobox which filters all the items in the by clearing the panel controls and repopulating it according to the filter, but for whatever reason it loads them improperly and after a couple of filter changes only shows some and not all. It reads it from a litst passing it to another which has all the filtered ones yet it still cant do it.

Any help is greatly appreciated.

The code segment

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (GegStde == null)
                return;

            foreach (Gegenstand geg in panel1.Controls.OfType<Gegenstand>().Where(x => x.select))
            {
                geg.Reset();
            }

            panel1.Controls.Clear();

            List<Gegenstand> TempGegStd = new List<Gegenstand>();

            if (Filter.SelectedIndex == 0)
                TempGegStd = new List<Gegenstand>(GegStde);
            else if (Filter.SelectedIndex == 1)
                TempGegStd = new List<Gegenstand>(GegStde.Where(x => x.GetDesc.Contains("Spezies")).ToList());
            else if (Filter.SelectedIndex == 2)
                TempGegStd = new List<Gegenstand>(GegStde.Where(x => !x.GetDesc.Contains("Spezies")).ToList());

            foreach (Gegenstand art in TempGegStd)
            {

                panel1.Controls.Add(art);

                if (panel1.Controls.Count > 1)
                    art.Location = new Point(art.Location.X, panel1.Controls[panel1.Controls.Count - 2].Location.Y + panel1.Controls[panel1.Controls.Count - 2].Height + 5);
            }
        }
7
  • Firstly, you should be using a TableLayoutPanel or FlowLayoutPanel as a container, rather than trying to calculate the Location for each child control. You might replace your existing Panel or put the TLP or FLP in that Panel, depending on which is more appropriate. Commented Jun 25 at 4:46
  • As for your issue, you need to actually debug your code. If you don't know how to debug, stop what you're doing and learn that first. You need to set a breakpoint and step through the code line by line to see exactly what it does and compare that to what you expect it to do. You may well be able to fix it yourself once you see the difference but, if you can't, you tell us exactly what you find. You MUST debug your code before posting a question here. Not doing so is one of the specific reasons for voting to close a question. Commented Jun 25 at 4:54
  • I did debug of course, and everything was pointing to that code snippet, I even went single step mode and yes it only went through that specific code snippet, but if a tlp or flp will fix that, I will just replace it, rather than trying to figure this out Commented Jun 25 at 8:55
  • I don't know whether a change of container will fix your issue or not, but you should do it regardless. As for saying of course you debugged, it's certainly not obvious from your question because there's nothing in there that you wouldn't know by simply watching the UI like a regular user. If you actually used the debugger then you should be able to see whether the correct number of controls area created and added and what their Location is and whether that is what you expect. Commented Jun 25 at 9:01
  • There's also a bit of craziness going on with your code, creating loads of lists that you don't need. Firstly, you create a new List where you declare the TempGegStd but you never use it. You then replace it with different List that you populate from yet another List. That part of the code can be cleaned up a lot. You probably don't need to create any List at all. What type is GegStde? I'm guessing that it is also List<Gegenstand> or similar. Commented Jun 25 at 9:06

0

Browse other questions tagged or ask your own question.