我尝试将此代码转换为C#并出错 [英] I Try Convert This Code To C# and Got An Error

查看:79
本文介绍了我尝试将此代码转换为C#并出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨Codeproject ..

我从VB.NET的此站点获得了一些代码,
这段代码是如何使带有图标的LinkLabel ...

但是我遇到了一些错误...您能帮我修复它吗..并告诉我它有什么问题

Hi Codeproject..

I got some code from this site in VB.NET,
this code is how to make a LinkLabel with an Icons...

but i Got Some Error...Can u help me to fix it..and tell me what''s wrong with it

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;

namespace IntegratedExpress.Library.UI
{
    [Designer(typeof(IconicLinkLabelDesigner)), ToolboxItem(true), ToolboxBitmap(typeof(LinkLabel))]
    public class IconicLinkLabel : System.Windows.Forms.Label
    {

        #region " API "

        private const int WM_SETCURSOR = 0x20;
        private const int WM_MOUSELEAVE = 0x2a3;

        private const int IDC_HAND = 32649;
        [DllImport("user32.dll")]
        private static extern int LoadCursor(int hInstance, int lpCursorName);

        [DllImport("user32.dll")]
        private static extern int SetCursor(int hCursor);

        #endregion

        private Color _ColorNormal = SystemColors.ActiveCaption;

        private Color _ColorHover = SystemColors.GradientActiveCaption;
        [System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("Color of the text while the mouse is NOT hovering over it.")]
        public System.Drawing.Color ColorNormal
        {
            get { return this._ColorNormal; }
            set
            {
                this.ForeColor = value;
                this._ColorNormal = value;
            }
        }

        [System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("Color of the text while the mouse is hovering over it.")]
        public System.Drawing.Color ColorHover
        {
            get { return this._ColorHover; }
            set { this._ColorHover = value; }
        }

        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            base.WndProc(ref m);
            switch (m.Msg)
            {
                case WM_SETCURSOR:
                    SetCursor(LoadCursor(0, IDC_HAND));
                    this.Font = new System.Drawing.Font(this.Font.Name, this.Font.Size, System.Drawing.FontStyle.Underline);
                    this.ForeColor = this._ColorHover;
                    break;
                case WM_MOUSELEAVE:
                    SetCursor(0);
                    this.Font = new System.Drawing.Font(this.Font.Name, this.Font.Size, System.Drawing.FontStyle.Regular);
                    this.ForeColor = this._ColorNormal;
                    break;
            }
        }

        public IconicLinkLabel()
        {
            this.ForeColor = this._ColorNormal;
            this.AutoSize = false;
            this.Height = 16;
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Rectangle imageRect = new Rectangle();
            Rectangle textRect = new Rectangle();

            imageRect.X = 0 + this.Padding.Left;
            imageRect.Y = 0 + this.Padding.Top;

            if ((this.Image != null))
            {
                imageRect.Width = this.Image.Width;
                imageRect.Height = this.Image.Height;

                textRect.X = this.Image.Width + this.Padding.Left + 5;

                e.Graphics.DrawImage(this.Image, imageRect);
            }
            else
            {
                textRect.X = 0 + this.Padding.Left;
            }

            textRect.Y = 1.25 + this.Padding.Top;
            textRect.Height = this.Height - (this.Padding.Bottom + this.Padding.Top);
            textRect.Width = this.Width - textRect.X - this.Padding.Right;

            e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
        }

    }

    public class IconicLinkLabelDesigner : System.Windows.Forms.Design.ControlDesigner
    {


        private DesignerActionListCollection lists;
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                if (lists == null)
                {
                    lists = new DesignerActionListCollection();
                    lists.Add(new IconicLinkLabelActionList(this.Component));
                }
                return lists;
            }
        }

        //remove the properties that we don't want the user to change
        protected override void PreFilterProperties(System.Collections.IDictionary properties)
        {
            properties.Remove("Cursor");
            properties.Remove("ForeColor");
            properties.Remove("ImageAlign");
            properties.Remove("TextAlign");
            properties.Remove("AutoSize");
        }
    }

    public class IconicLinkLabelActionList : System.ComponentModel.Design.DesignerActionList
    {
        private IconicLinkLabel MyLabel;
        private DesignerActionUIService designerActionSvc = null;

        public IconicLinkLabelActionList(IComponent component)
            : base(component)
        {
            this.MyLabel = Component;
            this.designerActionSvc = (DesignerActionUIService)GetService(typeof(DesignerActionUIService));
        }

        private PropertyDescriptor GetPropertyByName(string propName)
        {
            PropertyDescriptor prop = null;
            prop = TypeDescriptor.GetProperties(MyLabel)[propName];
            if (prop == null)
            {
                throw new ArgumentException("Invalid property.", propName);
            }
            else
            {
                return prop;
            }
        }

        public System.Drawing.Color ColorNormal
        {
            get { return MyLabel.ColorNormal; }
            set { GetPropertyByName("ColorNormal").SetValue(MyLabel, value); }
        }

        public System.Drawing.Color ColorHover
        {
            get { return MyLabel.ColorHover; }
            set { GetPropertyByName("ColorHover").SetValue(MyLabel, value); }
        }

        public System.Drawing.Image Image
        {
            get { return MyLabel.Image; }
            set { GetPropertyByName("Image").SetValue(MyLabel, value); }
        }

        public override DesignerActionItemCollection GetSortedActionItems()
        {
            DesignerActionItemCollection items = new DesignerActionItemCollection();
            items.Add(new DesignerActionHeaderItem("Appearance"));
            items.Add(new DesignerActionPropertyItem("Image", "Link Image", "Appearance", "Sets the image to be displayed in the label."));
            items.Add(new DesignerActionPropertyItem("ColorNormal", "Normal Color", "Appearance", "Sets the color of the text while the mouse is NOT hovering over it."));
            items.Add(new DesignerActionPropertyItem("ColorHover", "Hover Color", "Appearance", "Sets the color of the text while the mouse is hovering over it."));
            return items;
        }

    }
}


无法将类型"System.ComponentModel.IComponent"隐式转换为"IntegratedExpress.Library.UI.IconicLinkLabel"时显示错误.存在显式转换(您是否缺少演员表?)第149行


第二个错误是无法将类型"double"隐式转换为"int".存在显式转换(是否缺少强制转换?)第101行我认为我理解第二个错误..但我不介意如何将double转换为integer

很快需要一个答案..
Thx


it''s show error on Cannot implicitly convert type ''System.ComponentModel.IComponent'' to ''IntegratedExpress.Library.UI.IconicLinkLabel''. An explicit conversion exists (are you missing a cast?) Line 149


and the second error is Cannot implicitly convert type ''double'' to ''int''. An explicit conversion exists (are you missing a cast?) Line 101 I think i understand the second error..but i don''t mind how to convert double to integer

Need an Answer soon..
Thx

推荐答案

转到此处进行转换(它是一个免费的基于Web的转换器).

http://www.developerfusion.com/tools/convert/vb-to-csharp/ [^ ]

在大多数情况下,它的性能都很好,但是您可能仍然需要手动对其进行调整.

如果需要将其转换为很快",那么最好通过它进行调试-相信我.
Go here for conversion (it''s a free web-based converter).

http://www.developerfusion.com/tools/convert/vb-to-csharp/[^]

It does a fairly good job most of the time, but you may still have to tweak it manually.

If you need it converted "soon", you''ll be better of muscling through it - believe me.


尝试调试代码以查看错误的原因和发生的位置. br/> 它可能会帮助您解决问题.
Try debugging your code to see what the error is and where it occurs.
It will probably help you solve it.


这篇关于我尝试将此代码转换为C#并出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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