Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8144117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:17:06+00:00 2026-06-06T13:17:06+00:00

My code isn’t really that relevant but just to give you background, I have

  • 0

My code isn’t really that relevant but just to give you background, I have a method that opens an instance of a window messageWindow like this;

private void SetMessagePosition(Controls.Button btn, string text)
        {
            messageWindow = new messageWindow(text);
            relativePoint = btn.TransformToAncestor(this).Transform(new Point(0, 0));

            messageWindow.Left = relativePoint.X + this.Left;
            messageWindow.Top = relativePoint.Y + this.Top;
            messageWindow.Show();
        }

But I want to see if I can use this method to open other windows as well. This obviously means passing it the name of the new window I want to open as a parameter. My question is, how? I have tried passing parameters like so;

private void SetMessagePosition(Window newWin, Controls.Button btn, string text))
        {
            newWin = new newWin(text);
            ...

Where newWin = the type of window I want to open. But obviously the new newWin part throws an error because VS doesn’t know of a window called newWin.

I know your first thought might be, why not just instantiate the windows before calling this method, then I can skip this line all together. Well this method actually sets the position of the new window relative to the parent window at it’s time of opening, ergo, I can’t have set it’s position before now.

Another thing I thought to try was;

List<Window> winList;
List<Type> winListType;

winList.Add(window1);
winList.Add(window2);
winList.Add(window3);

winListType.Add(Window1);
winListType.Add(Window2);
winListType.Add(Window3);

SetMessagePosition(winList[2], winListType[2], btn1, "Yes");

private void SetMessagePosition(Window newWin, Type newWinType, Controls.Button btn, string text))
        {
            newWin = new newWinType(text);
            ...

But newWinType does not like being passed a Type and not a variable, even though it is a list of Type. I would be very impressed if anyone knew a way/workaround to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:17:08+00:00Added an answer on June 6, 2026 at 1:17 pm

    You need to make it a generic method so you can pass the type:

    private void SetMessagePosition<T>(Controls.Button btn, string text) where T : Window, new()
    {
        T window = new T();
        relativePoint = btn.TransformToAncestor(this).Transform(new Point(0, 0));
        window.Left = relativePoint.X + this.Left;
        window.Top = relativePoint.Y + this.Top;
        window.Show();
    }
    

    Then, you can call it like this:

    SetMessagePosition<messageWindow>(btn, text);
    

    However, the downfall to that approach is that you can’t pass anything into the constructor when you are instantiating an object via a generic type. So, you’d need to set the text on the window in a different way such as creating an interface that has a SetText method, or something like that, and that could be added as another constraint on the generic type.

    You could simplify this whole mess by simply expecting the new window to already be instantiated and passed into the method rather than having the method instantiate it itself:

    private void SetMessagePosition(Window window, Controls.Button btn)
    {
        relativePoint = btn.TransformToAncestor(this).Transform(new Point(0, 0));
        window.Left = relativePoint.X + this.Left;
        window.Top = relativePoint.Y + this.Top;
        window.Show();
    }
    

    Then, you could just call it like this:

    SetMessagePosition(new newWin(text), btn);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i know that there isn't code inside my question, but i think this is
I have the problem that my code isn't triggering the action in my backing
This maybe a really simple question but for some reason my code isn't working.
Hy, I don't really understand why my code isn't working. Short description: I have
I'll start by saying that this isn't a problem, just curiosity. I have some
I have this old-style non-PDO MySQL query (the code isn't tightened up, just to
This code isn't localized: Enum.GetNames(typeof(DayOfWeek)) I want a method that returns a list of
I have no idea why this code isn't working, but hopefully the fix is
Just wondering why my code isn't working. Am I doing this completely wrong? window.onload
OK. I may be splitting hairs here, but my code isn't consistent and I'd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.