zBomb
zBomb

Reputation: 361

C# winforms event in Main method for any new form shown

I have two windows forms apps projects (one test and one prod) and a dll project in the same solution. The two winforms app projects have only one class/function, Program.Main(), the icons I'm asking about, and the app.config files. They both reference the same dll that contains everything else (including the forms).
What I want is to be able to set the icon (Form.Icon) and text (Form.Text) each time a new form in the app is shown. The purpose of this is to have different window titles and icons for test and prod (as well as different publish location settings). How can I accomplish this? I've tried setting the icon with the Properties>Application>Resources>Icon and Manifest, but it doesn't work. I would be happy with just getting a different icon, but the text would be a big plus. Is there an event to subscribe to in the Program.Main() method, before the Application.Run(new Form()), that I can set the Icon and Text Properties as the form is shown, or any other solutions? Edit: I was hoping for somthing like this because there are lots of forms:

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        SomeClass.NewWinFormShown += NewWinFormShown;
        Application.Run(new FrmTrackingList());
    }

    private static void NewWinFormShown(object sender, NewWinFormShownEventArgs e)
    {
        e.NewWinFormShown.Icon = ThisAppsIcon;//from resources
        e.NewWinFormShown.Text += " (TEST)";
    }

Upvotes: 0

Views: 700

Answers (3)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73442

You can create a BaseForm for all the forms you use in your Application, It is not that hard

public class AppForm : Form
{
    public AppForm()
    {
        InitializeComponent();
        This.Icon = MyAppSettings.AppIcon; <--read here
        this.Text  = "App Text"
    }
}

Edit: For Accessing Icon in dll, just create a static class in Dll and use it

//In dll project
public static class MyAppSettings
{
     public Icon AppIcon {get;set;}
}

//In Exe project
static void Main()
{
    MyAppSettings.AppIcon = Resources.Icon;  <--set here
    //Rest of starting App Code goes here
}

Then make all your form derive from AppForm. this should solve your problem.

Upvotes: 1

Scott Mermelstein
Scott Mermelstein

Reputation: 15397

I don't see why you can't set the icon and text in each Program.Main. Save the form, and tweak it. Instead of

Application.Run(new Form());

do

Form myForm = new Form();
myForm.Icon = theFancyIcon; // from resources, or any other way you want to get it
myForm.Text = myTitleBarString;
Application.Run(myForm);

Just make the code different in each version, since you already have different Program.Main files, this should be easy.

Upvotes: 0

jerrylagrou
jerrylagrou

Reputation: 511

Get values from fields in app.config.

Use slowcheetah to have a app.config that is different for different builds.

Upvotes: 0

Related Questions