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

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
{
public FirstWindow() : base("Test")
{
SetDefaultSize(600, 350);
SetPosition(WindowPosition.Center);
DeleteEvent += delegate { Program.Quit(); };
VBox vBox = new VBox();
Add(vBox);
ListStore store = new ListStore
(
typeof(string),
typeof(string),
typeof(bool),
typeof(string)
);
store.AppendValues(Stock.DialogWarning, "Warning", true, Stock.Edit);
store.AppendValues(Stock.Stop, "Stop", false, Stock.Edit);
store.AppendValues(Stock.New, "New", true, Stock.Edit);
store.AppendValues(Stock.Clear, "Clear", true, Stock.Edit);
store.AppendValues(Stock.Edit, "Edit", true, Stock.Edit);
var imageCell = new CellRendererPixbuf();
var textCell = new CellRendererText();
var imageCell2 = new CellRendererPixbuf();
ComboBox comboBox = new ComboBox(store);
comboBox.PackStart(imageCell, true);
comboBox.PackStart(textCell, true);
comboBox.PackStart(imageCell2, true);
comboBox.AddAttribute(imageCell, "icon-name", 0);
comboBox.AddAttribute(textCell, "text", 1);
comboBox.AddAttribute(imageCell2, "icon-name", 3);
comboBox.AddAttribute(imageCell, "sensitive", 2);
comboBox.AddAttribute(textCell, "sensitive", 2);
comboBox.AddAttribute(imageCell2, "sensitive", 2);
comboBox.Changed += OnChanged;
comboBox.Active = 0;
HBox hBox = new HBox();
vBox.PackStart(hBox, false, false, 5);
hBox.PackStart(comboBox, false, false, 5);
ShowAll();
}
void OnChanged(object? o, EventArgs e)
{
Console.WriteLine($"Index changed to:{((ComboBox)o!).Active}");
}
}© accounting.org.ua - 2025