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

  • Home
  • SEARCH
  • 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 6629425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:15:30+00:00 2026-05-25T22:15:30+00:00

I’m designing a UI manager class that will manage all my UI elements (this

  • 0

I’m designing a UI manager class that will manage all my UI elements (this is for an XNA game, so there is no existing UI framework) but I’m wondering how to deal with situations where I want the UI manager to have special access to data in the UI elements that other classes can’t access.

For example, I want to have a SetFocus method to focus a specific element and this method needs to ensure that the previously focused element loses focus. The individual UI elements themselves can’t handle this because they don’t have access to the list of UI elements, which means the manager has to handle it, but how can I allow the manager and only the manager to set the variable on a UI element?

The thought occurs to me to just store the currently focused element on the manager, however I don’t particularly like that solution and, given a single UI element, I would like to query it to find out if it has focus. Even if it makes sense to store the currently focused element on the manager since its just a single variable, there are other things I need to deal with that would require arrays to associate the data with the elements if its stored on the manager, and that just seems to defeat the purpose of OOP.

I know I don’t need to have it so that the manager is the only one with access to this data, I could just make all the data public, but that’s not good design…

  • 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-05-25T22:15:31+00:00Added an answer on May 25, 2026 at 10:15 pm

    What you are looking for is a C# equivalent of the C++ friend concept. As you read in the linked post ‘the closest that’s available (and it isn’t very close) is InternalsVisibleTo‘ (citing Jon Skeet). But using InternalsVisibleTo in order to accomplish what you want would mean you’d have to break up your complete application into a library per class, which would probably create a DLL hell.

    Building upon MattDavey’s example got me:

    interface IFocusChecker
    {
        bool HasFocus(Control control);
    }
    
    class Manager : IFocusChecker
    {
        private Control _focusedControl;
    
        public void SetFocus(Control control)
        {            
            _focusedControl = control;
        }
    
        public bool HasFocus(Control control)
        {
            return _focusedControl == control;
        }
    }
    
    class Control
    {
        private IFocusChecker _checker;
    
        public Control(IFocusChecker checker)
        {
            _checker = checker;
        }
    
        public bool HasFocus()
        {
            return _checker.HasFocus(this);
        }
    }
    

    Whether a Control has focus is now only stored in the Manager and only the Manager can change the focused Control.

    A little example how to put things together, for completeness:

    class Program
    {
        static void Main(string[] args)
        {
            Manager manager = new Manager();
            Control firstControl = new Control(manager);
            Control secondControl = new Control(manager);
    
            // No focus set yet.
            Console.WriteLine(string.Format("firstControl has focus? {0}",
                firstControl.HasFocus()));
            Console.WriteLine(string.Format("secondControl has focus? {0}",
                secondControl.HasFocus()));
    
            // Focus set to firstControl.
            manager.SetFocus(firstControl);
    
            Console.WriteLine(string.Format("firstControl has focus? {0}",
                firstControl.HasFocus()));
            Console.WriteLine(string.Format("secondControl has focus? {0}",
                secondControl.HasFocus()));
    
            // Focus set to firstControl.
            manager.SetFocus(secondControl);
    
            Console.WriteLine(string.Format("firstControl has focus? {0}",
                firstControl.HasFocus()));
            Console.WriteLine(string.Format("secondControl has focus? {0}",
                secondControl.HasFocus()));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6

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.