📜  如何在C#中设置文本框的位置?

📅  最后修改于: 2021-05-30 01:22:32             🧑  作者: Mango

在Windows窗体中,TextBox扮演着重要角色。借助TextBox,用户可以在应用程序中输入数据,它可以是单行或多行。在TextBox中,可以通过Location属性设置TextBox控件的左上角相对于其容器的左上角的坐标。在Windows窗体中,可以用两种不同的方式设置此属性:

1.设计时:这是设置TextBox的Location属性的最简单方法,如以下步骤所示:

  • 第1步:创建一个Windows窗体,如下图所示:
    Visual Studio->文件->新建->项目-> WindowsFormApp
  • 步骤2:从工具箱中拖动TextBox控件,并将其放在Windows窗体上。您可以根据需要将TextBox放置在Windows窗体上的任何位置。如下图所示:
  • 步骤3:拖放之后,您将转到TextBox控件的属性以设置TextBox的Location属性。如下图所示:

    输出:

2.运行时:比上述方法有些棘手。在此方法中,可以借助给定的语法以编程方式设置TextBox的Location属性:

public System.Drawing.Point Location { get; set; }

这里,Point用于表示控件的左上角相对于其容器的左上角。以下步骤用于设置TextBox的Location属性:

  • 步骤1:使用TextBox类提供的TextBox()构造函数创建一个文本框。
    // Creating textbox
    TextBox Mytextbox = new TextBox();
    
  • 步骤2:创建TextBox之后,设置TextBox类提供的TextBox的Location属性。
    // Set Location property
    Mytextbox1.Location = new Point(187, 51);
    
  • 第3步:最后使用Add()方法将此文本框控件添加到中。
    // Add this textbox to form
    this.Controls.Add(Mytextbox1);
    

    例子:

    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 Mylablel1 = new Label();
            Mylablel1.Location = new Point(96, 54);
            Mylablel1.Text = "Enter Name";
            Mylablel1.AutoSize = true;
            Mylablel1.BackColor = Color.LightGray;
      
            // Add this label to form
            this.Controls.Add(Mylablel1);
      
            // Creating and setting the properties of TextBox1
            TextBox Mytextbox1 = new TextBox();
            Mytextbox1.Location = new Point(187, 51);
            Mytextbox1.BackColor = Color.LightGray;
            Mytextbox1.AutoSize = true;
            Mytextbox1.Name = "text_box1";
            Mytextbox1.Margin = new Padding(5, 5, 5, 5);
      
            // Add this textbox to form
            this.Controls.Add(Mytextbox1);
      
            // Creating and setting the properties of Lable1
            Label Mylablel2 = new Label();
            Mylablel2.Location = new Point(96, 102);
            Mylablel2.Text = "Enter Area";
            Mylablel2.AutoSize = true;
            Mylablel2.BackColor = Color.LightGray;
      
            // Add this label to form
            this.Controls.Add(Mylablel2);
      
            // Creating and setting the properties of TextBox2
            TextBox Mytextbox2 = new TextBox();
            Mytextbox2.Location = new Point(187, 99);
            Mytextbox2.BackColor = Color.LightGray;
            Mytextbox2.AutoSize = true;
            Mytextbox2.Name = "text_box2";
      
            // Add this textbox to form
            this.Controls.Add(Mytextbox2);
        }
    }
    }
    

    输出: