C# 弹出字符串框

发布于 2025-01-10 14:29:35 字数 115 浏览 1 评论 0原文

所以我试图制作一个简单的弹出框,您可以在其中插入多个不同的输入 (名字、姓氏等)信息,它将被保存为字符串。我尝试使用内置文本框,但无法理解它的真正工作原理。有更简单的方法吗? 如果相关的话,我正在使用 win 表单。

so I'm trying to make a simple pop-up box in which you can insert multiple different inputs
(first name, last name, etc...) info and it will be saved as a string. I tried using built-in text boxes but was unable to understand how it really works. Is there an easier way of doing so?
I'm using win forms if that's relevant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2025-01-17 14:29:35

简单地返回一个字符串是没有意义的,而是考虑创建一个类来表示您的数据并按如下重写 ToString ,以便可以选择使用数据的字符串表示形式或为每个要收集的元素提供属性。

您需要创建一个表单(在本例中为 UserInputForm),在表单上放置标签和控件以收集输入。验证可以通过此表单或调用表单来完成。

简单的例子,我们想要名字、姓氏和出生日期,添加名字和姓氏的文本框以及出生日期的 DateTimePicker。提交/确定按钮和取消按钮。对于“取消”按钮,在属性窗口中将 DialogResult 设置为“取消”。

创建一个类来表示要收集的数据,例如

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
}

在表单中收集数据。请注意表单级别变量,用于返回 Person 类型的数据,如果按下 sumbit 按钮,调用表单将使用该变量。

public partial class UserInputForm : Form
{
    public readonly Person Person = new Person();
    public UserInputForm()
    {
        InitializeComponent();
    }

    private void SubmitButton_Click(object sender, EventArgs e)
    {
        Person.FirstName = FirstNameTextBox.Text;
        Person.LastName = LastNameTextBox.Text;
        Person.BirthDate = BirthDateTimePicker.Value;
        DialogResult = DialogResult.OK;
    }
}

在表单中调用上述表单

private void GetPersonDetailsButton_Click(object sender, EventArgs e)
{
    UserInputForm f = new UserInputForm();
    try
    {
        if (f.ShowDialog() == DialogResult.OK)
        {
            MessageBox.Show($"{f.Person.FirstName} {f.Person.LastName} {f.Person.BirthDate:d}");
        }
        else
        {
            MessageBox.Show("Cancelled");
        }
    }
    finally
    {
        f.Dispose();
    }

}

然后可以在提交按钮事件或调用表单中完成验证。

表单验证的简单示例。

private void SubmitButton_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrWhiteSpace(FirstNameTextBox.Text) & !string.IsNullOrWhiteSpace(LastNameTextBox.Text) & BirthDateTimePicker.Value < DateTime.Now)
    {
        Person.FirstName = FirstNameTextBox.Text;
        Person.LastName = LastNameTextBox.Text;
        Person.BirthDate = BirthDateTimePicker.Value;
        DialogResult = DialogResult.OK;
    }
    else
    {
        // present user with a dialog to correct input
    }

}

如果您确实想要一个字符串,请更改最后一个以覆盖 ToString

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
    public override string ToString() => $"{FirstName},{LastName},{BirthDate:d}";
}

用法

if (f.ShowDialog() == DialogResult.OK)
{
    MessageBox.Show(f.Person.ToString());
}
else
{
    MessageBox.Show("Cancelled");
}

完整源代码

It does not make sense to simply return a string, instead consider creating a class to represent your data and override ToString as per below to have the option to have a string representation of the data or to have properties for each element to collect.

You need to create a form (in this case UserInputForm), place labels and controls on the form to collect input. Validation may be done in this form or by the calling form.

Simple example, we want first, last name and birth date, add text boxes for first and last name and a DateTimePicker for birth date. A submit/ok button and a cancel button. For the Cancel button set DialogResult to Cancel in the property window.

Create a class to represent the data to be collected e.g.

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
}

In the form to collect data. Note form level variable for returning data of type Person which the calling form uses if the sumbit button is pressed.

public partial class UserInputForm : Form
{
    public readonly Person Person = new Person();
    public UserInputForm()
    {
        InitializeComponent();
    }

    private void SubmitButton_Click(object sender, EventArgs e)
    {
        Person.FirstName = FirstNameTextBox.Text;
        Person.LastName = LastNameTextBox.Text;
        Person.BirthDate = BirthDateTimePicker.Value;
        DialogResult = DialogResult.OK;
    }
}

In the form to call the above form

private void GetPersonDetailsButton_Click(object sender, EventArgs e)
{
    UserInputForm f = new UserInputForm();
    try
    {
        if (f.ShowDialog() == DialogResult.OK)
        {
            MessageBox.Show(
quot;{f.Person.FirstName} {f.Person.LastName} {f.Person.BirthDate:d}");
        }
        else
        {
            MessageBox.Show("Cancelled");
        }
    }
    finally
    {
        f.Dispose();
    }

}

Then there is validation which can be done in the submit button event or in the calling form.

Simple example for in form validation.

private void SubmitButton_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrWhiteSpace(FirstNameTextBox.Text) & !string.IsNullOrWhiteSpace(LastNameTextBox.Text) & BirthDateTimePicker.Value < DateTime.Now)
    {
        Person.FirstName = FirstNameTextBox.Text;
        Person.LastName = LastNameTextBox.Text;
        Person.BirthDate = BirthDateTimePicker.Value;
        DialogResult = DialogResult.OK;
    }
    else
    {
        // present user with a dialog to correct input
    }

}

And if you really want a string alter the last to override ToString

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
    public override string ToString() => 
quot;{FirstName},{LastName},{BirthDate:d}";
}

Usage

if (f.ShowDialog() == DialogResult.OK)
{
    MessageBox.Show(f.Person.ToString());
}
else
{
    MessageBox.Show("Cancelled");
}

Full source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文