【发布时间】:2009-11-01 16:34:15
【问题描述】:
我在 c# 中创建了一个 winform,我在页面上有 3 个控件和另一个控件,并以编程方式在其中一个原始控件上创建。它没有渲染,所以我更改了其他控件的背景颜色(一开始它们都是明亮的颜色),当我运行应用程序时,更改没有发生,我已经逐步完成并且看不到我是什么做错了。有人可以帮忙吗?
更新: * 我已经设置了位置、大小和名称,但没有任何区别。 * 奇怪的是,当我把它放在没有参数的构造函数中时,它工作正常。这似乎与第二个构造函数中的代码有关(它实际上在两个构造函数中运行代码都很好)。
谢谢莎拉 :)
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();
}
}
}
【问题讨论】:
-
没有任何代码,几乎不可能回答这个问题。请尝试发布一个简短但完整的程序来演示该问题。
-
我会回应 Jon 的评论,但如果出现严重错误,可能值得尝试清理解决方案(如果需要,手动清理)
-
确保您的更改在 *.designer.cs 文件中得到更新,没有任何代码我们无法真正帮助您。
-
好的,我会尝试获取一些代码,我是第一次做 MVP,所以这对我来说有点混乱-对不起
-
为什么在 public WordToGuessControl(Level level) 的末尾有 InitializeComponent() 调用:this()?那很不好。 this() 调用将调用 InitializeComponent();无需再次调用它(因为它会重置所有内容)。这可能不能解决你的问题,但它跳出来给我。
标签: c# winforms user-controls