-
Changing the text of Main Form title at runtime
Hi All,
I would like to change the text appearing in the title (main bar at the top of the window) of the main form in a C# windows app at runtime.
However, to change any property of a form at runtime, it appears the form needs to be an instance of the form, rather than the form itself. For all forms other than the main, this will usually be the case, however it appears that the main form cannot be instantiated at startup.
Is there any way around this?
Many thanks,
Steve Krenek
-
In .NET, all forms are instances. Somewhere in your project, you have a Main method that looks like this:
Code:
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
That code creates an instance of Form1 and passes it to the Application.Run method. If you want to change the form's title before passing it to Application.Run, you can do this instead:
Code:
static void Main()
{
Form1 frmMain = new Form1;
frmMain.Text = "Title bar text";
Application.Run(frmMain);
}
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Inside the form itself you can change its title using the this keyword:
this.Text = "This is my new title"
From another class, you have to pass its instance like Phil suggested. If the class is created within the form itself, again you can pass the this keyword
Marco
"There are two ways to write error-free programs. Only the third one works."
Unknown
-
Thanks, Phil, this has been very helpful in getting my mental picture of instances of forms etc clearer.
However, my requirement was to change the title text *after* the program had started (ie in response to something happening during execution).
I now find that I can do this using
this.Text = "new title text";
at any point in the code.
Steve
-
related question - title in form during runtime
hey Steve,
interested in the same issue - used the "this.Text" command but doesn't do anything for me - in fact it, the program essentially stops processing anything after that command and just sits waiting on input from the form, which works.
did you have any other problems? or was it just that easy?
running VS 2008 and C#/C++
thanks
-
Post the code that you are using that isn't working for you.
Similar Threads
-
By maheeru in forum VB Classic
Replies: 3
Last Post: 08-09-2005, 12:59 PM
-
Replies: 0
Last Post: 03-17-2005, 12:47 PM
-
Replies: 2
Last Post: 11-12-2001, 10:46 AM
-
Replies: 1
Last Post: 11-06-2001, 10:43 AM
-
Replies: 3
Last Post: 08-30-2001, 11:45 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|