?
Solved

c# window title setting from arbitrary panel

Posted on 2010-09-10
7
Medium Priority
?
588 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:thready
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 12

Expert Comment

by:w00te
ID: 33647720
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
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 33647769
Hi thready;

Because Application.OpenForms is a collection of all open forms in the application the first one in the collection may not be the one you want to change. so use this form to get a reference to the correct window.
 
Form openForms = Application.OpenForms["Form Name Goes Here"];

Fernando
0
 
LVL 12

Expert Comment

by:w00te
ID: 33647798
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33647875
Do you just want the "active" form?

    Form.ActiveForm.Text = "Hello!";

Or the current form?

    this.Text = "Hello!";
0
 
LVL 1

Author Comment

by:thready
ID: 33648427
I want only the main window (the one that opens as soon as my app starts)...
0
 
LVL 1

Author Closing Comment

by:thready
ID: 33648481
Thank you - this is what I was looking for!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33648529
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!";
        }
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question