Winform does not pass control changes

I created winform in C #, I have 3 controls on the pages and another control and it is created programmatically on one of the original controls. This is not rendering, so I changed the background colors of the other controls (all of them are bright colors for starters), and when I launch the application, there were no changes, I went through and cannot see what I'm doing wrong. Can anyone help?

Update: * I set the position, size and name, but it does not matter. * Strange, when I put it in the constructor with no arguments, it works fine. This seems to be due to the fact that the code is in the second constructor (it really runs the code in both constructors perfectly).

Thanks Sara :)

GameForm.cs

namespace Hangman
{
public partial class GameForm : Form, IGameView
{
    public GameForm()
    {
        InitializeComponent();
    }

    public GameForm(Game game) : this()
    {
        wordToGuessControl = new WordToGuessControl(game.Level);
        new GamePresenter(this, wordToGuessControl, hangmanControl);
    }
}

WordToGuessControl.cs

namespace Hangman.UserControls
{
    public partial class WordToGuessControl : UserControl, IWordToGuessView
    {

    private string _word;
    public WordToGuessControl()
    {
        InitializeComponent();
    }

    public WordToGuessControl(Level level) : this()
    {
        _word = GenerateWord(level);

        foreach (var character in _word)
        {
            var characterInWord = new RenderLetterControl();
            characterInWord.Location = new Point(0, 0);
            characterInWord.Name = character.ToString();
            characterInWord.Size = new Size(50,50);

            characterInWord.Text = "_";
            Controls.Add(characterInWord);

        } 
    }

    private string GenerateWord(Level level)
    {
        return new WordGenerator().GenerateWord(level);
    }
}

RenderLetterControl.cs

namespace Hangman.UserControls
{
public partial class RenderLetterControl : Label
{
    public RenderLetterControl()
    {
     InitializeComponent();
     Text = "_";
    }

    public RenderLetterControl(char character): this()
    {
        string characterGuessed = character.ToString();
    }
}
}
+3
4

WordToGuessControl , , . .

:)

+3

wordToGuessControl, , Controls . , GamePresenter() , .

wordToGuessControl , , .

+1

InitializeComponent();

+1
source

It looks like you are adding RenderLetterControl controls to your WordToGuessControl, but you never set its size or location and you don't call Control.Show to make it visible.

0
source

Source: https://habr.com/ru/post/1721622/


All Articles