tags:

views:

1242

answers:

4

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.

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?

+1  A: 

Add a button in main form.

Create a form with textbox for ip address. (lets say it IPAddressForm)

Add click event handler for that button.

In the event handler, create an instance of IPAddressForm and call showdialog method of IPAddressForm.

Store the ip address in some class variable.

If the showdialog result is ok, read the class variable from main form (simplest way is to declare the field as public)

malay
+4  A: 

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

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();
        }
    }
}
Francis B.
You should better use the using() statement. :)
bassfriend
Well, we learn stuff every day :), I applied using keyword in my code on everything (Pen, SqlConnection, Stream, etc.) but I never did it on a form. Probably because I never saw an example applying it...even in a book.
Francis B.
This is what I currently have private void btnAddServer_Click(object sender, EventArgs e) { string ipAdd; }Not alot I know my other form which I wish to use is named Input it has a text box called txtInput and a button called btnInput. So how would this match what you've given as an answer? Sorry but I'm new to all this.
manemawanna
+1  A: 

Looks like Francis has the correct idea which is what I would have suggested. However, just to add to this I would probably suggest using a MaskedTextBox instead of a basic TextBox and add the IP Address format as the Mask.

James
A: 

To view a complete implementation see my post at: http://zamirsblog.blogspot.com/2010/06/vb-like-inputbox-in-c.html

Zamir