using System;
using System.Drawing;
using System.Windows.Forms;
namespace SimpleWinForms
{
class LoginForm : Form
{
TextBox _txtUserName;
TextBox _txtPassword;
public string Username => _txtUserName.Text;
public string Password => _txtPassword.Text;
public LoginForm()
{
Text = "Login";
ShowIcon = false;
SizeGripStyle = SizeGripStyle.Show;
Button btnConnect = new Button() { Text = "Connect" };
btnConnect.Click += (o, e) => DialogResult = DialogResult.OK;
AcceptButton = btnConnect;
var btnCancel = new Button() { Text = "Cancel" };
btnCancel.Click += (o, e) => DialogResult = DialogResult.Cancel;
CancelButton = btnCancel;
LayoutControls(accept: btnConnect, cancel: btnCancel);
}
void LayoutControls(Button accept, Button cancel)
{
Padding = new Padding(5);
Size = new Size(260, 150);
AutoSize = true;
SuspendLayout();
var table = new TableLayoutPanel()
{
Dock = DockStyle.Fill,
AutoSize = true,
ColumnCount = 2,
RowCount = 3
};
table.ColumnStyles.Add(new ColumnStyle());
table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 80f));
table.SuspendLayout();
_txtUserName = AddLabelAndTextBox(table, "User Name:");
_txtPassword = AddLabelAndTextBox(table, "Password:");
var flow = new FlowLayoutPanel()
{
AutoSize = true,
Anchor = AnchorStyles.Right | AnchorStyles.Bottom
};
flow.SuspendLayout();
flow.Controls.Add(accept);
flow.Controls.Add(cancel);
flow.ResumeLayout();
table.Controls.Add(flow);
table.SetColumnSpan(flow, 2);
table.ResumeLayout();
Controls.Add(table);
ResumeLayout(true);
}
TextBox AddLabelAndTextBox(TableLayoutPanel table, string label)
{
table.Controls.Add(new Label()
{
Text = label,
AutoSize = true,
Anchor = AnchorStyles.Right
});
var textBox = new TextBox()
{
AutoSize = true,
Anchor = AnchorStyles.Left | AnchorStyles.Right
};
table.Controls.Add(textBox);
return textBox;
}
}
class TestForm : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new LoginForm().ShowDialog();
}
}
}