Windows Develop Bookmark and Share   
 index > Windows Forms General > how to make a variable avaiable to another class on the same level?
 

how to make a variable avaiable to another class on the same level?

please show me how to make a varaible avaiable from a class to a class on the same level.
from custom controm to another costum control.

kind regards


Natural_D  Sunday, September 27, 2009 2:25 AM
I may be misunderstanding what you want to do, but perhaps you are talking about using a property:

private string myVariable; // your variable that you want to share

// the following property gives a getter and setter to allow you to read and assign to the private variable
public string MyVariable
{
get
{
return this.myVariable;
}
set
{
this.myVariable = value;
}
}

This would allow you to access your variable from any other class. If you want to make it so that no other libraries can see it, you can the property from public to internal. This will limit visibility to a single namespace.

Hope this helps.

There are 10 types of people in this world, those who understand Binary, and those who don't.
anubisascends  Sunday, September 27, 2009 5:11 AM
I may be misunderstanding what you want to do, but perhaps you are talking about using a property:

private string myVariable; // your variable that you want to share

// the following property gives a getter and setter to allow you to read and assign to the private variable
public string MyVariable
{
get
{
return this.myVariable;
}
set
{
this.myVariable = value;
}
}

This would allow you to access your variable from any other class. If you want to make it so that no other libraries can see it, you can the property from public to internal. This will limit visibility to a single namespace.

Hope this helps.

There are 10 types of people in this world, those who understand Binary, and those who don't.
anubisascends  Sunday, September 27, 2009 5:11 AM
wel, i knew that solution but it doesn't work.

i have a namespace, witch has toolstrip classes(toolstripitem, dropdown, Menustrip, renderer, etc, etc).
via the classclass 'cRenderer : ToolStripProfessionalRenderer' i provide ownerdraw effects to the 'cMenuItem : ToolStripMenuItem'

namespace cMenu
{  
    public class cMenuStrip : MenuStrip
    {
        public cMenuStrip()
        {
            this.Renderer = new cRenderer();
            //this.ContextMenu = new cContextMenu();
            this.ContextMenuStrip = new cContexMenuStrip();
            //base.Height = 32;
            this.BackColor = Color.LightGray;
            this.ForeColor = Color.Black; 
        }
    }
    public class cMenuItem : ToolStripMenuItem
    {
        public bool mouseOver = false;
        //public delegate 

        public bool MouseOver
        {
            get
            {
                return this.mouseOver;
            }
            set
            {
                this.mouseOver = value;
            }
        }


        protected override void OnMouseHover(EventArgs e)
        {
            base.OnMouseHover(e);
            mouseOver = true;
            
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            mouseOver = false;
        }
    }

    class cRenderer : ToolStripProfessionalRenderer
    {
        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
        {
           if (e.Item.Selected) // mouseover - apply color
           {
           }
           else
           {
           }
	}
    }
}



how ever if a toolstripitem has a dropdown collection and gets shown, the applied effect disappears if you hover the mouse over the dropdown menu.
i wanna try to mimic of the original menustrip behaviour.
keep the effect applied until the mouse leaves the dropdown/contextmenu.

the only acces from inside 'cRenderer : ToolStripProfessionalRenderer' to 'cMenuItem' = ToolStripItemAccessibleObject.


System.Windows.Forms.ToolStripItem.ToolStripItemAccessibleObject
System.Windows.Forms.ToolStripDropDownItemAccessibleObject

these 2 classes seems to provide what i'm looking for, but i dont know how to use them, i can't find examples from other people on this subject.


kind regards
Natural_D  Sunday, September 27, 2009 2:55 PM
I am assuming that this is because you have the OnMouseLeave(EventArgs e) method overridden. Once you leave the menu item, the MouseLeave event will be called and anything that would run because of this will...well run.

How about something like this:

public class MyToolStripMenu : System.Windows.Forms.ToolStripMenu
{

List<MyMenuItem> menuItems;

public void CloseMenus()
{
foreach(MyMenuItem item in menuItems)
{
item.TryClose();
}
}
}

public class MyToolStripMenuItem : System.Windows.Forms.ToolStripMenuItem
{
private bool mouseOver = false;

public void TryClose()
{
if(mouseOver)
{
// code that closes this menu
}
}
}

Now, everytime you raise the OnMouseEnter(EventArgs) method, you can try this:

((MyToolStripMenu)this.Parent).CloseMenus();
There are 10 types of people in this world, those who understand Binary, and those who don't.
anubisascends  Monday, September 28, 2009 1:48 AM

You can use google to search for other answers

Custom Search

More Threads

• Drawing above all windows
• WebBrowser - tracking button clicks
• Manipulate Windows Explorer folder properties dialog
• How to save full path of Cerain file into XML file
• how to get dll from windows forms application to add it as reference in another projects
• Format Text Box For Currenct Value
• Reference to instantiating Form OK in VB, not in C#?
• use single usercontrol in different folder
• Chinese characters in a C# windows form
• How do you mimic Undo and Redo Lists in toolbar