您好我正在创建一个需要为其添加服务器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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:添加另一个示例以适应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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在"输入"表单中,添加属性:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
20768 次 |
最近记录: |