dimanche 19 juillet 2020

Winform (C#) drawing control on a different location than it should?

Let me preface this by saying I'm not good at coding, never have been, but I make do. Please be nice if this turns out to be a really stupid question.

I'm making a Dungeons and Dragons character creator, where you'll be able to add your own gamesystems, Classes, Races and other addons. To add a new Race for example, you'll have a form where all the gamesystems will be listed as checkboxes to check in which gameystems the new Race will usable. I've got it working so far that you can add, edit and delete gamesystems, and am now working on the Add Race page where you'll have that list of checkboxes.

When the program is started, it reads the excel file with all the gamesystems and adds them to the list with checkboxes, divided by two to make two nice columns. When you add a new system during runtime, it'll update the checkboxlist and add a new checkbox for the new system.

'''
public static void updateGames(string abb) {

        var workbook = ExcelFile.Load(filepath + "Games/Crucible.xls");
        var sheet = workbook.Worksheets[0];
        int index = 0;

        foreach (Panel p in panels)
        {
            if ((p.Name.Contains("Add") || p.Name.Contains("Edit")) && !p.Name.Contains("Game"))
            {
                foreach (Control c in p.Controls)
                {
                    if (c.Name == "panelChecks")
                    {
                        foreach(var row in sheet.Rows)
                        {
                            if(row.Cells[1].Value.ToString() == abb)
                            {
                                index = row.Index;
                                break;
                            }
                        }
                        if (games.Count % 2 == 0)
                        {
                            var checkGame = new CheckBox()
                            {
                                Name = "check" + abb,
                                Text = sheet.Rows[index].Cells[0].Value.ToString(),
                                Top = (checkboxAmount - 1) * 10,
                                Left = 200,
                                Width = 200
                            };
                            c.Controls.Add(checkGame);
                            checkboxAmount++;
                            break;
                        }
                        else
                        {
                            var checkGame = new CheckBox()
                            {
                                Name = "check" + abb,
                                Text = sheet.Rows[index].Cells[0].Value.ToString(),
                                Top = checkboxAmount * 10,
                                Width = 200
                            };
                            c.Controls.Add(checkGame);
                            checkboxAmount++;
                            break;
                        }
                    }
                }
            }
        }
    }

'''

The first couple times you add a new checkbox, it works perfectly. The new control gets added precisely at the spot it's supposed to. However, after a couple times, it'll start adding them to random Top positions. The Top position is the most easy math imaginable, it's simply [a number] times 10. No way a computer should get that wrong, right? Yet, after some time, it seems to forget how to do that and starts to lie to my face when I ask it at when Top it's drawing the new checkbox.

I've got two images where I had this precise outcome. I asked it to tell me the amount of checkboxes registered, which was 16. I also asked it to tell me what the Top of the new checkbox should be, according to its math, which was 160, and the Top at which it would be drawing said checkbox, which was also 160. Top 160 would have been directly underneath 't', yet it drew the checkbox way below that. Gamesystem 'yyy' is also visible, which already had been placed at a very weird place also.

Example wherein gamesystem 'ggg' should be added directly underneath 't'

Example outcome wherein gamesystem 'ggg' is placed way below where it should be

Please help me make sense of this. Why is it telling me it will be drawing a checkbox at that Top, but then goes and draws it somewhere else entirely? There's nothing else influencing that number. The math always stays [checkboxamount]*10, it should not go wrong halfway.




Aucun commentaire:

Enregistrer un commentaire