Exemplo n.º 1
0
        private TabPage CreateAccountPage()
        {
            accountsBox = new ListBox();
            GetAccounts(accountsBox);

            addAccountButton    = Layout.Button("Add");
            editAccountButton   = Layout.Button("Edit");
            removeAccountButton = Layout.Button("Remove");

            addAccountButton.Click += delegate
            {
                var dialog = new AccountDialog();
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    var account = dialog.Account;
                    if (ConfigManager.Settings.Accounts.Find(
                            a => a.Name == account.Name) == null)
                    {
                        ConfigManager.Settings.Accounts.Add(account);
                        accountsBox.Items.Add(account.Name);
                        accountsBox.SelectedItem = account.Name;
                    }
                }
            };

            editAccountButton.Click += delegate
            {
                var index = accountsBox.SelectedIndex;
                if (index >= 0)
                {
                    var name   = accountsBox.SelectedItem as string;
                    var dialog = new AccountDialog(
                        ConfigManager.Settings.Accounts.Find(
                            a => a.Name == name) ?? IGSAccount.DefaultAccount);
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        var account = dialog.Account;

                        ConfigManager.Settings.Accounts.RemoveAll(
                            a => a.Name == name);
                        ConfigManager.Settings.Accounts.Insert(index, account);
                        accountsBox.Items[index] = account.Name;
                    }
                }
            };

            removeAccountButton.Click += delegate
            {
                var index = accountsBox.SelectedIndex;
                if (index >= 0)
                {
                    ConfigManager.Settings.Accounts.RemoveAt(index);
                    accountsBox.Items.RemoveAt(index);
                    if (ConfigManager.Settings.Accounts.Count == 0)
                    {
                        ConfigManager.Settings.Accounts.Add(IGSAccount.DefaultAccount);
                        accountsBox.Items.Add(IGSAccount.DefaultAccount.Name);
                    }
                    if (accountsBox.SelectedIndex < 0)
                    {
                        accountsBox.SelectedIndex = accountsBox.Items.Count - 1;
                    }
                }
            };

            var accountTable = new LayoutTable(2, 1);

            accountTable.PutControl(accountsBox, 0, 0);
            accountTable.PutLayout(
                Layout.Flow(addAccountButton,
                            editAccountButton,
                            removeAccountButton),
                1, 0);
            accountTable.FixRows(1);

            var page = new TabPage()
            {
                Text = "Account",
                Dock = DockStyle.Fill
            };

            Layout.Bind(accountTable, page);
            return(page);
        }