Changing the text of Main Form title at runtime
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?
In .NET, all forms are instances. Somewhere in your project, you have a Main method that looks like this:
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:
/// The main entry point for the application.
static void Main()
static void Main()
Form1 frmMain = new Form1;
frmMain.Text = "Title bar text";
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
"There are two ways to write error-free programs. Only the third one works."
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.
related question - title in form during runtime
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++
Post the code that you are using that isn't working for you.
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
By maheeru in forum VB Classic
Last Post: 08-09-2005, 01:59 PM
Last Post: 03-17-2005, 12:47 PM
Last Post: 11-12-2001, 10:46 AM
Last Post: 11-06-2001, 10:43 AM
Last Post: 08-30-2001, 12:45 PM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL