Winify – Rev 7

Subversion Repositories:
Rev:
using System;
using System.Linq;
using System.Windows.Forms;
using Servers;
using Winify.Properties;
using Winify.Utilities;

namespace Winify
{
    public partial class SettingsForm : Form
    {
        #region Private Delegates, Events, Enums, Properties, Indexers and Fields

        private readonly Servers.Servers _servers;

        #endregion

        #region Constructors, Destructors and Finalizers

        private SettingsForm()
        {
            InitializeComponent();
        }

        public SettingsForm(Servers.Servers servers) : this()
        {
            _servers = servers;

            listBox1.DataSource = _servers.Server;
            listBox1.DisplayMember = "Name";
            listBox1.DataBindings.Add(new Binding("Text", _servers.Server, "Name", true,
                DataSourceUpdateMode.OnPropertyChanged));
        }

        #endregion

        #region Event Handlers

        private void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            Settings.Default.LaunchOnBoot = ((CheckBox) sender).Checked;

            Miscellaneous.LaunchOnBootSet(Settings.Default.LaunchOnBoot);
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            var name = serverNameTextBox.Text;
            var host = serverHostTextBox.Text;
            var port = serverPortTextBox.Text;
            var username = serverUsernameTextBox.Text;
            var password = serverPasswordTextBox.Text;

            if (string.IsNullOrEmpty(name) ||
                string.IsNullOrEmpty(host) ||
                string.IsNullOrEmpty(port) ||
                string.IsNullOrEmpty(username) ||
                string.IsNullOrEmpty(password))
            {
                return;
            }

            var server = new Server(name, host, port, username, password);

            if (_servers.Server.Any(servers => servers.Name == server.Name))
            {
                return;
            }

            _servers.Server.Add(server);
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            listBox1.InvokeIfRequired(listBox =>
            {
                var item = listBox.SelectedItem;

                _servers.Server.Remove((Server) item);
            });
        }

        private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var listBox = (ListBox) sender;

            if (listBox.SelectedIndex == -1)
            {
                serverNameTextBox.Text = string.Empty;
                serverHostTextBox.Text = string.Empty;
                serverPortTextBox.Text = string.Empty;
                serverUsernameTextBox.Text = string.Empty;
                serverPasswordTextBox.Text = string.Empty;

                return;
            }

            var server = (Server) listBox.SelectedItem;

            serverNameTextBox.Text = server.Name;
            serverHostTextBox.Text = server.Host;
            serverPortTextBox.Text = server.Port;
            serverUsernameTextBox.Text = server.Username;
            serverPasswordTextBox.Text = server.Password;
        }

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.