设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 数据 手机
当前位置: 首页 > 运营中心 > 网站设计 > 正文

使用表单在C#中创建一个输入框

发布时间:2021-04-01 01:53 所属栏目:12 来源:网络整理
导读:您好我正在创建一个需要为其添加服务器IP地址的应用程序,因为C#中没有InputBox函数我试图使用表单来完成此操作,但是我对该语言非常新,所以不是100%as我应该做什么. 目前我有我的主表单和一个表单,它将作为我的输入框,想要隐藏负载.然后,当用户点击主表单上

您好我正在创建一个需要为其添加服务器IP地址的应用程序,因为C#中没有InputBox函数我试图使用表单来完成此操作,但是我对该语言非常新,所以不是100%as我应该做什么.

目前我有我的主表单和一个表单,它将作为我的输入框,想要隐藏负载.然后,当用户点击主表单上的添加IP地址时,我希望打开辅助表单并将输入的IP地址返回到辅助表单上的文本框中.

那我该怎么做呢?或者有没有更好的方法来实现类似的结果?

解决方法

在主窗体中,为事件添加事件处理程序单击按钮添加IP地址.在事件处理程序中,执行与以下代码类似的操作:
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评论.

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;
        }
    }
}

在“输入”表单中,添加属性:

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();
        }
    }
}

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读