创建使用表单在C#的输入框 [英] Creating an Inputbox in C# using forms

查看:149
本文介绍了创建使用表单在C#的输入框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我目前正在创建具有需要服务器的IP地址添加到它,因为在C#中没有InputBox函数我试图完成这一使用表单的应用程序,但我很新的语言,所以不100%作为我应该做的。

Hello I'm currently creating an application which has the need to add server IP addresses to it, as there is no InputBox function in C# I'm trying to complete this using forms, but am very new to the language so not 100% as to what I should do.

目前,我有我的主要形式和将作为我的输入框而想隐藏负载的形式。然后,当用户点击添加IP地址在主窗体上我想开拓二级形式返回的IP地址输入到二级窗体上的文本框。

At the moment I have my main form and a form which will act as my inputbox which wants to hide on load. Then when the user clicks on the add IP Address on the main form I wish to open up the secondary form and return the IP address entered into a text box on the secondary form.

所以,我怎么会去这样做?或者是还有什么更好的方法来达到类似的效果?

So how would I go about doing this? Or is there any better ways to achieve similar results?

推荐答案

在您的主要形式,添加事件处理该事件的按钮的点击添加IP地址。在事件处理程序,请执行以下代码类似:

In your main form, add an event handler for the event Click of button Add Ip Address. In the event handler, do something similar as the code below:

private string m_ipAddress;
private void OnAddIPAddressClicked(object sender, EventArgs e)
{
    using(SetIPAddressForm form = new SetIPAddressForm())
    {
        if (form.ShowDialog() == DialogResult.OK)
        {
            //Create a property in SetIPAddressForm to return the input of user.
            m_ipAddress = form.IPAddress;
        }
    }
}

修改:添加另外一个例子,以配合的 manemawanna 的意见

Edit: Add another example to fit with manemawanna comment.

private void btnAddServer_Click(object sender, EventArgs e)
{
    string ipAdd;
    using(Input form = new Input())
    {
        if (form.ShowDialog() == DialogResult.OK)
        {
            //Create a property in SetIPAddressForm to return the input of user.
            ipAdd = form.IPAddress;
        }
    }
}

在您输入的形式,添加属性:

In your Input form, add a property:

public class Input : Form
{
    public string IPAddress
    {
        get { return txtInput.Text; }
    }

    private void btnInput_Click(object sender, EventArgs e)
    {
        //Do some validation for the text in txtInput to be sure the ip is well-formated.

        if(ip_well_formated)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}

这篇关于创建使用表单在C#的输入框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆