Click here to Skip to main content
16,018,418 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
private class MyRenderer : ToolStripProfessionalRenderer
        {
            protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
            {
                if (!e.Item.Selected)
                {
                    string xCol = "#BF08B2";
                    Color c = System.Drawing.ColorTranslator.FromHtml(xCol);
                    SolidBrush brush = new SolidBrush(c);
                    Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);
                    e.Graphics.FillRectangle(brush, rc);
                    e.Graphics.DrawRectangle(
                    Pens.Transparent, 0, 0, rc.Width - 2, rc.Height - 1);
                }
                else
                {
                    string xCol = "#6F2687";
                    Color c = System.Drawing.ColorTranslator.FromHtml(xCol);
                    SolidBrush brush = new SolidBrush(c);
                    Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);
                    e.Graphics.FillRectangle(brush, rc);
                    e.Graphics.DrawRectangle(Pens.Transparent, 0, 0, rc.Width - 2, rc.Height - 1);
                }
            }
        }


Hi I have written this code for menustrip and now I want this code to be written in function so that I can use this code in other forms too. Please help me writing function for the above code.
Posted
Updated 2-Aug-11 1:12am
v2
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 2:41am    
What do you call "function"? There are no functions and non-functions. Sometimes functions are called methods returning values (not void return type in C/C++/C# terms). Every function/procedure is called "method". What you need is to abstract out the code from your renderer type. Next time, formulate your question accurately.
--SA

Create a method with a parameter of type ToolStripItemRenderEventArgs. And then in your method, place your code and replace e with the parameter.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 2:37am    
Correct, my 5. In general case, the reference to the declaring type would also be passed as a parameter, but in this case it would be redundant.
--SA
You already have the functionality you want encapsulated in a class. You can already create an instance of MyRenderer in any context and apply it to a toolbar. I don't think you quite understand what you're asking – event based UI interaction and functional programming aren't really two things that go together all that well.
 
Share this answer
 
Rather calling this function in each form or calling the same function in each form i will suggest you to make a master page and use the above function in it . And apply the same master page in every form in which you want to apply this menu.
 
Share this answer
 
Comments
walterhevedeich 2-Aug-11 16:55pm    
For the record, I did not downvote you. But judging from the code of OP(ToolStripProfessionalRenderer
), it appears that he is writing a windows application, so your solution will not work for his case.
using keyword 'void/int' and you can give any name according to your requirement, that's called is user defined function....
For Example:----
C#
class niitian
{
   public void manish()
    {
           System.console.WriteLine("This is the example of creating function");
    }
}
class second_niitian
{
  Public Static Void Main(String args[])
{
  niitian obj=new niitian();
obj.manish();
}
}

In above Example, manish() is a user defined method..
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900