Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can pass it in the constructor (you're not limited to the generated constructor, just make sure to call <code>base</code> with appropriate arguments) or you can set some properties on <code>MainWindow</code> before entering the main loop.</p> <p>Here are examples for both solutions. Note that I always start with a clean Gtk# project and MonoDevelop-generated <code>Main.cs</code> and <code>MainWindows.cs</code> and change the label text using different methods. Note that you can probably change the label title directly if you want, but this is just an example: the same can be applied to any other widget property or part of the window that require more logic than just an assignment.</p> <p>The label was created using the stetic designer and named just <code>label</code>.</p> <p><strong>Method 1 - Changing the MainWindow constructor</strong></p> <p>Let's start from the code generated by MonoDevelop and pass the window title as an argument to the constructor. This is the code generated by MonoDevelop:</p> <pre><code>public partial class MainWindow : Gtk.Window { public MainWindow() : base(Gtk.WindowType.Toplevel) { this.Build(); } } </code></pre> <p>And this how we modify the constructor:</p> <pre><code>public partial class MainWindow : Gtk.Window { public MainWindow(string labelText) : base(Gtk.WindowType.Toplevel) { // Note param here ^^^^^^^^^ this.Build(); // And how we use it on the following line this.label.text = labelText; } } </code></pre> <p>You should also change <code>Main</code> as follows:</p> <pre><code>public static void Main (string[] args) { Application.Init(); // Note the extra parameter on next line. var win = new MainWindow("Your label text here"); win.ShowAll(); Application.Run(); } </code></pre> <p><strong>Method 2 - Using a property</strong></p> <p>This is nice because you can use it after initialization to change some parts of the UI from other parts of your application. No changes to the constructor, you just add a property - or method in case you need to pass several parameters. I'll show both, starting with property:</p> <pre><code>public string LabelText { get { return label.Text; } set { this.label.Text = value; } } </code></pre> <p>Then method:</p> <pre><code>public void SetLabelText(string text) { this.label.Text = text; } </code></pre> <p>Note that I made <code>this</code> explicit and it is the <code>MainWindow</code> but it is safe (and good) to leave it out.</p> <p>An example of <code>Main</code>:</p> <pre><code>public static void Main (string[] args) { Application.Init(); var win = new MainWindow(); win.ShowAll(); // Using the property win.LabelText = "Your label text here"; // Or the method win.SetLabelText("Another label text"); Application.Run(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload