views:

98

answers:

3

Hi All :)

I've created a winform in c#, I have 3 controls on the pages and another control and is created programmatically on one of the original controls. It's not rendering, so I Changed the background colours of the other controls (they are all bright colours to begin with) and when I run the app the changes have not taken place, I've steped through and can't see what I am doing wrong. Can anyone help?

Updates: * I have set the position, size and name but it does not make a difference. * Weirdly when I put it in the constructor with no arguments it works fine. It seems to be something to do with the code being in the second constructor (It does actually run the code in both constructors fine).

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();
    }
}
}
A: 

It looks like you are adding RenderLetterControl controls to your WordToGuessControl, but you're never setting its size or location nor are you calling Control.Show to make it visible.

Eric
+1  A: 

In the second constructor you have forgotten to call InitializeComponent();

danbystrom
I tried that, no luck :( and also the first constructor is called ( this() )
Sara
Oh, sorry. I didn't see that! :-(
danbystrom
+2  A: 

The WordToGuessControl was being created in the designer thus overriding the settings I had set in the code. I deleted the settings in the designer and dynamically created the control.

Thanks for all your help :)

Sara