N
N
Nazarevsky2021-05-16 16:38:24
C++ / C#
Nazarevsky, 2021-05-16 16:38:24

How to connect two files in Windows Forms?

Hello!
I am doing one project in Windows forms. There are 2 documents, one is Form1.cs itself, and the second is with all classes (Classes.cs).
There is a class in Classes.cs that has a method that should draw some kind of square on the picturebox. But the joke is that although I placed the picturebox on the form, it is not displayed in Classes.cs in any way, so I can’t write a method.
Tell me how to make it so that I can call picturebox from Classes.cs?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Voland69, 2021-05-16
@Nazarevsky

In accordance with the principles of OOP and software development in general, dragging a dependency on the user (Form1.cs) into the used component (Classes.cs) is generally a bad idea.
What can be done:

  1. In Classes.cs, make a method with a parameter (ref PictureBox picturebox) and call it from Form1.cs - here you give the child component a link to the control, not very good. Classes.cs gets nailed to WF.
  2. In Classes.cs, make a method that returns the desired image (Bitmap, Image, etc) and call it from Form1.cs - here you are already doing only drawing the picture in Classes.cs, and Form1.cs decides what to do with this picture.

H
HemulGM, 2021-05-16
@HemulGM

First, learn the C# language. At least the basics.
Then - use one namespace and include the required one in using

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question