02.06.2024 10:37 Gtk3
Приклад введення інформації в окремому діалоговому вікні

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
{
Label labelInputText = new Label();
public FirstWindow() : base("Test")
{
SetDefaultSize(600, 600);
SetPosition(WindowPosition.Center);
DeleteEvent += delegate { Program.Quit(); };
VBox vBox = new VBox();
Add(vBox);
HBox hBox = new HBox();
vBox.PackStart(hBox, false, false, 5);
Button button = new Button("Відкрити діалогове вікно");
button.Clicked += OnOpenWindowDialog;
hBox.PackStart(button, false, false, 5);
HBox hBoxLabel = new HBox();
vBox.PackStart(hBoxLabel, false, false, 5);
hBoxLabel.PackStart(labelInputText, false, false, 5);
ShowAll();
}
void OnOpenWindowDialog(object? sender, EventArgs args)
{
string InputText = "";
ResponseType ModalResult = ResponseType.None;
using (FormInput windowFormInput = new FormInput())
{
windowFormInput.TransientFor = this;
windowFormInput.Modal = true;
windowFormInput.Title = "Введіть опис";
windowFormInput.Resizable = false;
windowFormInput.CallBack_ResultInputText = (string x) => { InputText = x; };
windowFormInput.Show();
while (ModalResult == ResponseType.None)
{
ModalResult = windowFormInput.ModalResult;
Application.RunIteration(true);
}
}
labelInputText.Text = InputText;
}
}using Gtk;
class FormInput : Window
{
public ResponseType ModalResult { get; set; } = ResponseType.None;
Button bOk = new Button("Ok");
Button bCancel = new Button("Закрити");
TextView InputText = new TextView() { WrapMode = WrapMode.Word };
public System.Action<string>? CallBack_ResultInputText { get; set; }
public FormInput() : base("Введення тексту")
{
SetPosition(WindowPosition.Center);
DeleteEvent += delegate { OnCancel(bCancel, new EventArgs()); };
BorderWidth = 5;
ScrolledWindow scrollTextView = new ScrolledWindow() { ShadowType = ShadowType.In, WidthRequest = 400, HeightRequest = 100 };
scrollTextView.SetPolicy(PolicyType.Automatic, PolicyType.Automatic);
scrollTextView.Add(InputText);
VBox vBox = new VBox(false, 0);
vBox.PackStart(scrollTextView, false, false, 0);
bOk.Clicked += OnOk;
bCancel.Clicked += OnCancel;
HBox hBoxButton = new HBox();
hBoxButton.PackStart(bOk, false, false, 5);
hBoxButton.PackStart(bCancel, false, false, 5);
vBox.PackStart(hBoxButton, false, false, 5);
Add(vBox);
ShowAll();
}
void OnOk(object? sender, EventArgs args)
{
ModalResult = ResponseType.Ok;
CallBack_ResultInputText?.Invoke(InputText.Buffer.Text);
ThisClose();
}
void OnCancel(object? sender, EventArgs args)
{
ModalResult = ResponseType.Cancel;
ThisClose();
}
void ThisClose()
{
this.Hide();
this.Dispose();
this.Destroy();
}
}© accounting.org.ua - 2025