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 RenderLetterContro开发者_Go百科l : Label
{
public RenderLetterControl()
{
InitializeComponent();
Text = "_";
}
public RenderLetterControl(char character): this()
{
string characterGuessed = character.ToString();
}
}
}
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 :)
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.
In the second constructor you have forgotten to call InitializeComponent();
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.
精彩评论