-1

I am new to learning C#, currently learning to use Windows Form / Classes using Class Diagram.

What I am trying to do is access the Windows Form components from one of the class I've created, such as the InitializeComponent()

I've tried

public partial class Class1: Form1
{
   public Class1()
   {InitializeComponent();}

which doesnt work unless I go and change the designer setting to make it public. This is all good and well and works for other windows form components I've created, but when I run the programme at the end I just get a blank form with nothing in it.

Using Class Diagram, I think it should be shown as attached photo with all the methods shown under the "method" section of a Class Diagram

enter image description here

Any ideas?

Cheers

NZ_DJ
  • 341
  • 2
  • 13
  • Do you have any controls in `Form1`? Are you not able to see those controls in `Class1`? How are you loading `Class1`? Constructor of `Form1` might already be calling `InitializeComponent`. you don't need to call it again in `Class1` Constructor. – Chetan Sep 18 '19 at 11:07
  • The Winforms designer does this the Right Way by default, implementation details of a class should be private. If you need to expose something to other code then you add a public property. Or implement an interface, you forgot to add ISubject to the Class1 declaration. You can break the OOP rulez if you want to, use the designer to change the Modifiers property of a control from Private to Public. Making InitializeComponent() public would be fundamentally wrong, it can only be called correctly from the constructor. – Hans Passant Sep 18 '19 at 11:17

1 Answers1

0

Using WinForms the method InitializeComponent() is a default method not ment to be accessed from outside.

Default it is called from the controls constructor, and usually all that is needed is add code before or after the call to InitializeComponent() in the constructor - not call it from outside.

Visual Studio generates this automatic documentation for the method, hinting that it has special content:

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
Frode Evensen
  • 516
  • 1
  • 10
  • 22