3

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();
    }
}
}
Sara
  • 612
  • 5
  • 21
  • 1
    Without any code, it's pretty much impossible to answer this. Please try to post a short but complete program which demonstrates the problem. – Jon Skeet Nov 01 '09 at 16:37
  • I'll echo Jon's comment, but it may be worth trying to clean the solution (manually if required) in case something has gone horribly wrong – Lee Nov 01 '09 at 16:48
  • make sure your changes are being updated in the *.designer.cs file, without any code we can't really help you. – Stan R. Nov 01 '09 at 16:55
  • okay, I'll try get some code to put up I'm doing MVP for the first time so it's all a bit confusing for me -sorry – Sara Nov 01 '09 at 16:58
  • I've added the code, if you need more then please let me know. I figured out on teh game form there were backgroudn colours that were overriding the control settings, maybe this is the problem? I'm not sure how to fix this though? – Sara Nov 01 '09 at 17:11
  • You'd have to add code that changes the controls properties, after the InitializeComponent methods are called. If you set properties before hand they will be overwritten by any generated code, if that property has a value set by the code generator. – Richard Anthony Hein Nov 01 '09 at 18:02
  • 1
    Why is there a InitializeComponent() call at the end of public WordToGuessControl(Level level) : this()? That is bad. The this() call will call InitializeComponent(); no need to call it again (as it will reset everything). That may not solve your problem, but it jumped out to me. – Richard Morgan Nov 02 '09 at 22:26

4 Answers4

3

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
  • 612
  • 5
  • 21
1

You are creating a wordToGuessControl but there is no code adding it to the Controls collection of a Parent. Maybe GamePresenter() is doing that but we can't see.

In WordToGuessControl you are adding the new Controls to a Parent but you are not setting Position or Size, so they will all stack on top of each other.

H H
  • 263,252
  • 30
  • 330
  • 514
1

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

Dan Byström
  • 9,067
  • 5
  • 38
  • 68
0

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
  • 6,364
  • 1
  • 32
  • 49