05.06.2024 15:24 Gtk3
Приклад використання віджету ComboBoxText

dotnet new consoledotnet add package GtkSharpdotnet runusing Gtk;
class Program
{
public static void Main()
{
Application.Init();
new FirstWindow();
Application.Run();
}
public static void Quit()
{
Application.Quit();
}
}using Gtk;
class FirstWindow : Window
{
ComboBoxText comboBoxText = new ComboBoxText();
public FirstWindow() : base("Test")
{
SetDefaultSize(600, 350);
SetPosition(WindowPosition.Center);
DeleteEvent += delegate { Program.Quit(); };
VBox vBox = new VBox();
Add(vBox);
comboBoxText.Changed += OnChanged;
//Button
{
HBox hBox = new HBox();
vBox.PackStart(hBox, false, false, 5);
Button buttonAdd = new Button("Додати");
buttonAdd.Clicked += OnAddClick;
hBox.PackStart(buttonAdd, false, false, 5);
Button buttonClear = new Button("Очистити");
buttonClear.Clicked += OnClearClick;
hBox.PackStart(buttonClear, false, false, 5);
}
//ComboBoxText
{
HBox hBox = new HBox();
vBox.PackStart(hBox, false, false, 5);
hBox.PackStart(comboBoxText, false, false, 5);
for (int i = 1; i <= 10; i++)
comboBoxText.Append($"{i}", $"{DateTime.Now.ToString("HH:mm:ss")} - {i}");
comboBoxText.ActiveId = "5";
}
ShowAll();
}
void OnAddClick(object? sender, EventArgs args)
{
comboBoxText.Append("0", $"{DateTime.Now.ToString("HH:mm:ss")} - text");
}
void OnClearClick(object? sender, EventArgs args)
{
comboBoxText.RemoveAll();
}
void OnChanged(object? sender, EventArgs args)
{
Console.WriteLine(comboBoxText.ActiveId);
}
}© accounting.org.ua - 2025