【问题标题】:Make textbox multiline programmatically以编程方式制作文本框多行
【发布时间】:2021-11-13 02:03:02
【问题描述】:

我正在像这样在运行时中创建我的文本框:

TextBox control = new TextBox();                        
control.Name = "txt" + "somename";

我在互联网上搜索,发现 Textbox 中应该有一个 Multiline-Property,但遗憾的是我找不到它。 我也尝试添加多行,但这也不起作用。

如何在我的代码中执行此操作? 我使用 Windowsforms 和 .NET Framework 4.7.2

【问题讨论】:

  • 你使用的是 WinForms 还是 WPF/
  • 抱歉,我用的是 WinForms
  • 但遗憾的是我找不到它是什么意思?如果该属性存在,那么找到它并不难。写control,输入dot,Intellisense 就会启动。仔细检查你的项目属性,也许你不小心使用了 WPF 模板而不是 Windows 窗体。

标签: c# textbox programmatically-created programmatically


【解决方案1】:
control.Multiline = true;

这将使您的文本框多行。这是一个完整的示例:

var textBox = new TextBox
{
  Multiline = true
};

textBox.Text = "FirstLine" + Environment.NewLine + "SecondLine";

【讨论】:

  • 找不到 .Multiline 属性,这是我的问题。还有其他方法/解决方法吗?
  • 您确定您正在从事 Windows 窗体项目吗?您在哪里编写了您在问题中提供的代码?
  • docs.microsoft.com/en-us/dotnet/api/… - 确实存在!你得到一个编译器错误吗?显示屏幕截图
  • 我得到的只是错误 CS1061“Control”不包含“multiline”的定义,并且找不到接受“Control”类型的第一个参数的可访问的多行扩展方法(可能缺少使用指令或程序集引用)。
  • 我的代码:case "BIGTEXTBOX": control = new TextBox(); control.Name = "txt" + name; control.Multiline = true; control.KeyPress += frm.textBox_KeyPress; mdl.alertOnNull = true; mdl.LabelAnchorOnTop = true; break;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-20
  • 2013-11-11
  • 2018-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多