-2

I am creating my textbox within the runtime like this:

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

I searached on the internet and found that there should be a Multiline-Property in Textbox, but sadly I could not find it. I tried also to add multiple lines, but this isn't working either.

How can I do it within my code? I use Windowsforms and .NET Framework 4.7.2

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Joo
  • 41
  • 5
  • Are you using WinForms or WPF/ – Jeroen van Langen Sep 18 '21 at 15:54
  • Oh sorry, I use WinForms – Joo Sep 18 '21 at 15:57
  • What does *but sadly I could not find it* mean? If the Property exists, it's not really difficult to find it. Write `control`, enter a `dot` and Intellisense will kick in. Double check you Project Properties, maybe you have used the WPF template instead of Windows Forms by accident. – Jimi Sep 18 '21 at 17:08

1 Answers1

1
control.Multiline = true;

That will make your textbox multiline. Here is a fully working example:

var textBox = new TextBox
{
  Multiline = true
};

textBox.Text = "FirstLine" + Environment.NewLine + "SecondLine";
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
Mats
  • 14,902
  • 33
  • 78
  • 110
  • It cannot find the .Multiline property, that's my issue. Is there another way/a fix? – Joo Sep 18 '21 at 16:06
  • Are you sure you are working in a windows forms project? Where did you write the code you provided in your question? – Mats Sep 18 '21 at 16:11
  • https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.textbox.multiline?view=net-5.0 - well, it does exist! Do you get a compiler error ? Show a screenshot – Caius Jard Sep 18 '21 at 20:14
  • All I get is Error CS1061 "Control" does not contain a definition for "multiline", and no accessible multiline extension method could be found that accepts a first argument of type "Control" (possibly missing a using directive or assembly reference). – Joo Sep 18 '21 at 22:23
  • My Code: `case "BIGTEXTBOX": control = new TextBox(); control.Name = "txt" + name; control.Multiline = true; control.KeyPress += frm.textBox_KeyPress; mdl.alertOnNull = true; mdl.LabelAnchorOnTop = true; break;` – Joo Sep 18 '21 at 22:24
  • @Joo Please post the entire method or class in your question. – Mats Sep 19 '21 at 05:05
  • I figured it out, thanks :D – Joo Oct 18 '22 at 22:14