Code Executes, but form1.cs[Design] still gives error

jim golz 21 Reputation points
2022-11-09T18:35:38.57+00:00

Hello,

I am working on developing an interface and HMI and it's way over my head.

My code executes successfully and does everything correctly, but I keep running into this issue where whenever things are not explicitly defined inside the form1.cs[Design] code, even though it executes correctly, form1.cs[Design] still gives a build error. The main problem is that this error causes the form1.cs[Design] form becomes un-editable in the C# Window (e.g. I cannot place new buttons, etc.).

Form1.CS does the normal Initialize:
namespace GUI
{
public partial class GUIMainForm : Form
{
public GUIMainForm()
{
InitializeComponent();
}
}
}

While executing Initialize Component, the error occurs here:
string[] trigshtlist = GetStringList("TrigShtList.txt");
object[] trigshtOBJlist = new object[trigshtlist.Length];
int i = 0;

        foreach (string s in trigshtlist)  
        {  
            GUI.triggerSheet trigSht = new GUI.triggerSheet();  
            trigSht.trigSheetNum = trigshtlist[i];  
            trigSht.GameNumber = i.ToString();  
            trigshtOBJlist[i] = trigSht;  
            i++;  
        }  

When the program runs this code, it calls a function called "GetStringList", which simply reads a text file and creates the string array based on that text file. This is what's causing the error. When it executes, it works fine. but form1.cs[Design] will not accept this information being provided via a method/text file.

If I added 100 lines of code manually describing this array, it would work fine. It did work fine when I manually created a few "TriggerSheets" prior to writing the GetStringList method. The problem is that these should be driven by some sort of database that the program is reading, and I need to know why this is getting flagged as an error even though the code does exactly what it is supposed to be doing.

Is there a better way to do this? is there a way to ignore this error so that I can continue to use form1.cs[Design] to modify the form?

Developer technologies | Windows Forms
0 comments No comments
{count} votes

Accepted answer
  1. Lex Li 6,037 Reputation points
    2022-11-10T05:23:18.807+00:00

    You should avoid adding anything manually to InitializeComponent() because you don't know how you might break the visual designer. Move those extra lines to the Load event of the Form and usually they work the same way, but more reliable.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.