-1

The VS IDE has automatically created a designer class which is fine. I want to parse a text file and initialize the .text value of every component based on a user setting on runtime. It seems impossible to do it inside the designer.cs file as it is regenerated and my code gets overwritten but on the other hand this seems to be the proper thing to do since there should be just one InitializeComponent method. Any ideas?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
tcop
  • 381
  • 1
  • 2
  • 11
  • I've added the WinForms tag for you because this seem to be a WinForms question. If it isn't, please update the question with the correct UI framework. – ProgrammingLlama Oct 28 '18 at 11:46
  • Just do that in the `Load` event of your form: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.load?view=netframework-4.7.2 – Kevin Gosse Oct 28 '18 at 12:11
  • Right, you must do it in the constructor, after the InitializeComponent() call. – Hans Passant Oct 28 '18 at 12:28
  • @HansPassant: But then the text property will be initialized twice. Once with the automated designer.cs declaration and a second time from the constructor. I think it should get only one value. – tcop Oct 28 '18 at 13:38
  • That is not correct. The designer can only initialize the Text property with the value you chose at design-time. That is not useful if you need to show a value that you read from user-data. So the proper default is an empty string, the designer doesn't generate an assignment for that in InitializeComponent(). – Hans Passant Oct 28 '18 at 13:49

1 Answers1

1

How i like to do it is create function:

private void SecondInitialize()
{
    //Custom controls initializition
}

and call it after InitializeComponent()

public YourForm()
{
    InitializeComponent();
    SecondInitialize();
}
Aleksa Ristic
  • 2,394
  • 3
  • 23
  • 54