Avatar of thready
thready
 asked on

c# window title setting from arbitrary panel

Hi Experts,
I'm looking for something like AfxGetMainWnd in c# so that I can set the window title.  I'm in a panel and this panel could be in another panel or in a child window, etc.....  Is there a way to do this in c#?

Thanks!
Mike
.NET ProgrammingC#

Avatar of undefined
Last Comment
Mike Tomlinson

8/22/2022 - Mon
w00te

Application.OpenForms[0].Text = "Hello.";

should always refer to your first opened form, the applicaiton window.  That will change the title to "Hello."

Good luck :)
-w00te
ASKER CERTIFIED SOLUTION
Fernando Soto

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
w00te

Oh, and just in case for some reason [0] doens't refer to your main form you can use this to change the title based on the type of form (assuming you only have one of your main form open at any time).  If you have multiple of the same form, you can change getType() and typeof() and use Name == "your form name" to explicitly target a specific form you've made by name.  If this were your main form though, its started in your program file and allocated without a name, you would have to save the name or explicitly name it and pass it in to use the name method.

I think the first post will cover the problem though :)

-w00te
for (int i = 0; i < Application.OpenForms.Count; ++i)
            {
                if (Application.OpenForms[i].GetType() == typeof(Form1))
                {
                    Application.OpenForms[i].Text = "Hello.";
                    break;
                }
            }

Open in new window

Mike Tomlinson

Do you just want the "active" form?

    Form.ActiveForm.Text = "Hello!";

Or the current form?

    this.Text = "Hello!";
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
thready

ASKER
I want only the main window (the one that opens as soon as my app starts)...
thready

ASKER
Thank you - this is what I was looking for!
Mike Tomlinson

You could STORE the reference to your starting form as a static member in program.cs:

    static class Program
    {

        public static Form MainForm = null;

        ///
        /// The main entry point for the application.
        ///
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Program.MainForm = new Form1();
            Application.Run(Program.MainForm);
        }

    }

Now you can use "Program.MainForm" from anywhere in the app:

        private void button1_Click(object sender, EventArgs e)
        {
            Program.MainForm.Text = "Hello Starting Form!";
        }
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.