Changing the text of Main Form title at runtime


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Changing the text of Main Form title at runtime

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Posts
    3

    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

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    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!

  3. #3
    Join Date
    Sep 2005
    Posts
    3
    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

  4. #4
    Join Date
    Dec 2009
    Posts
    1

    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

  5. #5
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    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

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    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

Similar Threads

  1. Replies: 3
    Last Post: 08-09-2005, 01:59 PM
  2. Replies: 0
    Last Post: 03-17-2005, 12:47 PM
  3. Move Form without title bar
    By Jonas in forum .NET
    Replies: 2
    Last Post: 11-12-2001, 10:46 AM
  4. Top fixed screen
    By Cheng in forum Web
    Replies: 1
    Last Post: 11-06-2001, 10:43 AM
  5. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 12:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center