using System;
using System.ComponentModel;
using System.Net;
using System.Windows.Forms;

namespace HwComWrapperNETGui
{
    public partial class IpDialog : Form
    {
        public IpDialog()
        {
            InitializeComponent();
        }

        public IpDialog(string title, string defaultIpAddress)
        {
            InitializeComponent();
            this.Text = title;
            this.tbIpAddress.Text = defaultIpAddress;
        }

        public string IpAddress
        {
            get
            {
                return this.tbIpAddress.Text;
            }
            set
            {
                this.tbIpAddress.Text = value;
            }
        }

        private void TbIpAddressTextChanged(object sender, EventArgs e)
        {
            this.btnOk.Enabled = IPAddress.TryParse(this.tbIpAddress.Text, out var temp);
        }

        private void TbIpAddressValidating(object sender, CancelEventArgs e)
        {
            e.Cancel = !IPAddress.TryParse(this.tbIpAddress.Text, out var temp);
        }

        private void TbIpAddressLeave(object sender, EventArgs e)
        {
            if (IPAddress.TryParse(this.tbIpAddress.Text, out var temp))
            {
                this.tbIpAddress.Text = temp.ToString();
            }
        }
    }
}
