using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Drawing;
namespace WinFormsNetCore
{
class MainForm : Form
{
public delegate void UIChangedEventHandler(object sender, object e);
public event UIChangedEventHandler UIChanged;
readonly TextBox _textBox;
readonly ListBox _listBox;
MainForm()
{
_listBox = new ListBox()
{
Dock = DockStyle.Left,
TabIndex = 0,
};
var splitter = new Splitter()
{
Dock = DockStyle.Left,
MinSize = 100,
MinExtra = 100,
SplitPosition = 200,
TabIndex = 1,
};
_textBox = new TextBox()
{
Dock = DockStyle.Fill,
ReadOnly = true,
Multiline = true,
TabIndex = 2,
};
for (int i = 0; i < 10; i++)
_listBox.Items.Add($"Item {i + 1}");
_listBox.SelectedIndexChanged += Listbox_SelectedIndexChanged;
_listBox.KeyDown += ListBox_KeyDown;
_listBox.KeyPress += ListBox_KeyPress;
UIChanged += MainForm_UIChanged;
Size = new Size(600, 400);
SuspendLayout();
Controls.AddRange(new Control[] { _textBox, splitter, _listBox });
ResumeLayout(true);
}
private void Listbox_SelectedIndexChanged(object sender, EventArgs e)
{
_textBox.AppendText($"{DateTime.Now}: SelectedIndexChanged\r\n");
var listBox = (ListBox) sender;
UIChanged?.Invoke(this, listBox.Items[listBox.SelectedIndex].ToString());
}
private void ListBox_KeyDown(object sender, KeyEventArgs e)
{
_textBox.AppendText($"{DateTime.Now}: KeyDown\r\n");
}
private void ListBox_KeyPress(object sender, KeyPressEventArgs e)
{
_textBox.AppendText($"{DateTime.Now}: KeyPress\r\n");
}
private void MainForm_UIChanged(object sender, object e)
{
_textBox.AppendText($"{DateTime.Now}: UIChanged {e}");
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}