如何在C#中编写函数 [英] How to write function in c#

查看:50
本文介绍了如何在C#中编写函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
                }
            }
        }



我已经为menustrip编写了此代码,现在我希望将此代码编写为函数,以便我也可以其他形式使用此代码.请帮助我编写上述代码的函数.



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.

推荐答案

创建一个参数类型为ToolStripItemRenderEventArgs的方法.然后在您的方法中,放置代码并将e替换为参数.
Create a method with a parameter of type ToolStripItemRenderEventArgs. And then in your method, place your code and replace e with the parameter.


您已经具有要封装在类中的功能.您已经可以在任何上下文中创建MyRenderer实例并将其应用于工具栏.我认为您不太了解自己在问什么–基于事件的UI交互和功能编程实际上并不能很好地融合在一起.
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.


我建​​议以每种形式调用此函数或以每种形式调用相同的函数您可以制作一个母版页并在其中使用上面的功能.并以您要应用此菜单的每种形式应用相同的母版页.
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.


这篇关于如何在C#中编写函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆