winforms 点击鼠标时如何显示文本?

gg58donl  于 2023-03-19  发布在  其他
关注(0)|答案(2)|浏览(152)

我添加了鼠标单击事件并编写了一个方法

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
    int x = e.X;
    int y = e.Y;
    TextBox TextWithNumber = new TextBox();
    TextWithNumber.Width = 100;
    TextWithNumber.Height = 100;
    TextWithNumber.Location = new Point(x, y);
    TextWithNumber.Text = "TEXT";
    TextWithNumber.BackColor = Color.Green;
    TextWithNumber.ForeColor = Color.Red;
}

但是短信没有出现。请告诉我,我做错了什么?
编辑:正确答案:行控件.Add(带数字的文本);应添加到方法的末尾。

h22fl7wq

h22fl7wq1#

您需要将新文本框添加到表单中:

Controls.Add(TextWithNumber);
utugiqy6

utugiqy62#

您应该指定Parent-您放置TextBox的控件:

...

TextBox TextWithNumber = new TextBox() {
  Parent    = this,            // Put Text Box on "this" - i.e. on the form
  Width     = 100,
  Height    = 100,
  Location  = new Point(e.X, e.Y),
  Text      = "TEXT",
  BackColor = Color.Green,
  ForeColor = Color.Red,
};

...

相关问题