在Windows窗体中,TextBox扮演着重要角色。借助TextBox,用户可以在应用程序中输入数据,它可以是单行或多行。 TextBox是一个类,它在System.Windows.Forms命名空间下定义。在C#中,可以用两种不同的方式创建TextBox:
1.设计时:这是创建TextBox的最简单方法,如以下步骤所示:
- 第1步:创建一个Windows窗体。如下图所示:
Visual Studio->文件->新建->项目-> WindowsFormApp - 步骤2:从工具箱中拖动TextBox控件,并将其放在Windows窗体上。您可以根据需要将TextBox放置在Windows窗体上的任何位置。
- 步骤3:拖放之后,您将转到TextBox控件的属性,以根据需要修改TextBox设计。
2.运行时:比上述方法有些棘手。在这种方法中,您可以使用TextBox类创建自己的文本框。
- 步骤1:使用TextBox类提供的TextBox()构造函数创建一个文本框。
// Creating textbox TextBox Mytextbox = new TextBox();
- 步骤2:创建TextBox之后,设置TextBox类提供的TextBox的属性。
// Set location of the textbox Mytextbox.Location = new Point(187, 51); // Set background color of the textbox Mytextbox.BackColor = Color.LightGray; // Set the foreground color of the textbox Mytextbox.ForeColor = Color.DarkOliveGreen; // Set the size of the textbox Mytextbox.AutoSize = true; // Set the name of the textbox Mytextbox.Name = "text_box1";
- 第3步:最后使用Add()方法将此文本框控件添加到中。
// Add this textbox to form this.Controls.Add(Mytextbox);
例子:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace my { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Creating and setting the properties of Lable1 Label Mylablel = new Label(); Mylablel.Location = new Point(96, 54); Mylablel.Text = "Enter Name"; Mylablel.AutoSize = true; Mylablel.BackColor = Color.LightGray; // Add this label to form this.Controls.Add(Mylablel); // Creating and setting the properties of TextBox1 TextBox Mytextbox = new TextBox(); Mytextbox.Location = new Point(187, 51); Mytextbox.BackColor = Color.LightGray; Mytextbox.ForeColor = Color.DarkOliveGreen; Mytextbox.AutoSize = true; Mytextbox.Name = "text_box1"; // Add this textbox to form this.Controls.Add(Mytextbox); } } }
输出:
TextBox的重要属性
Property Description AcceptsReturn This property is used to set a value which shows whether pressing ENTER in a multiline TextBox control creates a new line of text in the control or activates the default button for the given form. AutoSize This property is used to adjust the size of the TextBox according to the content. BackColor This property is used to set the background color of the TextBox. BorderStyle This property is used to adjust the border type of the textbox. CharacterCasing This property is used to check whether the TextBox control modifies the case of characters as they are typed. Events This property is used to provide a list of event handlers that are attached to this Component. Font This property is used to adjust the font of the text displayed by the textbox control. ForeColor This property is used to adjust the foreground color of the textbox control. Location This property is used to adjust the coordinates of the upper-left corner of the textbox control relative to the upper-left corner of its form. Margin This property is used to set the margin between two textbox controls. MaxLength This property is used to set the maximum number of characters the user can type or paste into the text box control. Multiline This property is used to set a value which shows whether this is a multiline TextBox control. Name This property is used to provide a name to the TextBox control. PasswordChar This property is used to set the character used to mask characters of a password in a single-line TextBox control. ScrollBars This property is used to set which scroll bars should appear in a multiline TextBox control. Text This property is used to set the text associated with this control. TextAlign This property is used to adjust the alignment of the text in the TextBox control. TextLength This property is used to get the length of the text in the TextBox control. UseSystemPasswordChar This property is used to set a value which shows whether the text in the TextBox control should appear as the default password character. Visible This property is used to get or set a value which determine whether the control and all its child controls are displayed.