Tłumaczenie VB na C#

0

Witam,kolega ostatnio pochwalił mi się kodem w VB który napisał.Wygląda on tak.

Imports System.Windows.Forms
Imports System.Drawing

Public Class CubeButton
    Inherits Button

    Private img As Image

    Dim Color1 As Color = Color.CornflowerBlue
    Dim BackGround As Image = New Bitmap(Width, Height)


    Public Property ClickColor() As Color
        Get
            Return Color1
        End Get
        Set(value As Color)
            Color1 = value
            Ref()
        End Set
    End Property
    Public Overrides Property BackColor As Color
        Get
            Return MyBase.BackColor
        End Get
        Set(value As Color)
            MyBase.BackColor = value
            Ref()
        End Set
    End Property

    Public Overrides Property Text As String
        Get
            Return MyBase.Text
        End Get
        Set(value As String)
            MyBase.Text = value
            Ref()
        End Set
    End Property

    Public Overrides Property TextAlign As ContentAlignment
        Get
            Return MyBase.TextAlign
        End Get
        Set(value As ContentAlignment)
            MyBase.TextAlign = value
            Ref()
        End Set
    End Property

    Public Sub New()
        Size = New Size(160, 30)
        BackColor = Color.RoyalBlue
        Color1 = Color.CornflowerBlue
        ForeColor = Color.White
        BackGround = New Bitmap(Width, Height)
        FillNormal()
    End Sub

    Private Sub FillNormal()
        BackGround = New Bitmap(Width, Height)
        Dim G As Graphics = Graphics.FromImage(BackGround)
        G.FillRectangle(New SolidBrush(BackColor), New Rectangle(New Point(0, 0), New Size(Width, Height)))
        If TextAlign = ContentAlignment.MiddleLeft Then
            G.DrawString(Text, Font, New SolidBrush(ForeColor), 15, 15)
        ElseIf TextAlign = ContentAlignment.MiddleCenter Then
            Dim textSize As Size = TextRenderer.MeasureText(Text, Font)
            G.DrawString(Text, Font, New SolidBrush(ForeColor), (Size.Width / 2) - (textSize.Width / 2), (Size.Height / 2) - (textSize.Height / 2))
        Else
            G.DrawString(Text, Font, New SolidBrush(ForeColor), 8, 8)
        End If
        G.Dispose()
        Refresh()
    End Sub

    Private Sub FillClick()
        BackGround = New Bitmap(Width, Height)
        Dim G As Graphics = Graphics.FromImage(BackGround)
        G.FillRectangle(New SolidBrush(BackColor), New Rectangle(New Point(0, 0), New Size(Width, Height)))
        G.FillRectangle(New SolidBrush(Color1), New Rectangle(New Point(0, 0), New Size(Width - 0, Height - 0)))
        If TextAlign = ContentAlignment.MiddleLeft Then
            G.DrawString(Text, Font, New SolidBrush(ForeColor), 15, 15)
        ElseIf TextAlign = ContentAlignment.MiddleCenter Then
            Dim textSize As Size = TextRenderer.MeasureText(Text, Font)
            G.DrawString(Text, Font, New SolidBrush(ForeColor), (Size.Width / 2) - (textSize.Width / 2), (Size.Height / 2) - (textSize.Height / 2))
        Else
            G.DrawString(Text, Font, New SolidBrush(ForeColor), 8, 8)
        End If
        G.Dispose()
        Refresh()
    End Sub

    Protected Overrides Sub OnPaint(pevent As PaintEventArgs)
        MyBase.OnPaint(pevent)
        pevent.Graphics.DrawImage(BackGround.Clone, 0, 0)
    End Sub

    Protected Overrides Sub OnMouseUp(mevent As MouseEventArgs)
        MyBase.OnMouseUp(mevent)
        FillNormal()
    End Sub

    Protected Overrides Sub OnMouseDown(mevent As MouseEventArgs)
        MyBase.OnMouseDown(mevent)
        FillClick()
    End Sub

    Public Sub Ref()
        FillNormal()
    End Sub

End Class
 

Ja spróbowałem to przetłumaczyć na C#.Oto co mi wyszło

 
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ModernButton
{
    class ModernButton : Button
    {
        public Image img;
        public Color color1 = Color.CornflowerBlue;
        public Image BackgroundImage;

        public Color ClickColor
        {
            get{return color1;}
            set {color1 = value; }   
            
        }
        public Color BackColor
        {
            get { return base.BackColor; }
            set { base.BackColor = value; }
        }
        public override string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }
        public override ContentAlignment TextAlign
        {
            get
            {
                return base.TextAlign;
            }
            set
            {
                base.TextAlign = value;
            }
        }
        public ModernButton()
        {
        Size = new Size(160, 30);
        BackColor = Color.RoyalBlue;
        color1 = Color.CornflowerBlue;
        ForeColor = Color.White;
        BackgroundImage = new Bitmap(Width, Height);
        FillNormal();
        }
        public void FillNormal()
        {
            BackgroundImage = new Bitmap(Width, Height);
            Graphics G = Graphics.FromImage(BackgroundImage);
         G.FillRectangle(new SolidBrush(BackColor), new Rectangle(new Point(0, 0), new Size(Width, Height)));
        if(TextAlign = ContentAlignment.MiddleLeft) 
        {
            G.DrawString(Text, Font, new SolidBrush(ForeColor), 15, 15);
        }
        else if (TextAlign = ContentAlignment.MiddleCenter)
        {
            Size textSize = TextRenderer.MeasureText(Text, Font);
            G.DrawString(Text, Font, new SolidBrush(ForeColor), (Size.Width / 2) - (textSize.Width / 2), (Size.Height / 2) - (textSize.Height / 2));
        }
        else
        {
            G.DrawString(Text, Font, new SolidBrush(ForeColor), 8, 8);
        }
        G.Dispose();
        Refresh();
        }
        private void FillClick()
        {
             BackgroundImage = new Bitmap(Width, Height);
        Graphics G = Graphics.FromImage(BackgroundImage);
        G.FillRectangle(new SolidBrush(BackColor), new Rectangle(new Point(0, 0), new Size(Width, Height)));
        G.FillRectangle(new SolidBrush(color1), new Rectangle(new Point(0, 0), new Size(Width - 0, Height - 0)));
        if (TextAlign = ContentAlignment.MiddleLeft)
        {
            G.DrawString(Text, Font, new SolidBrush(ForeColor), 15, 15);
        }
        else if( TextAlign = ContentAlignment.MiddleCenter )
        {
            Size textSize = TextRenderer.MeasureText(Text, Font);
            G.DrawString(Text, Font, new SolidBrush(ForeColor), (Size.Width / 2) - (textSize.Width / 2), (Size.Height / 2) - (textSize.Height / 2));
        }
        else{
            G.DrawString(Text, Font, new SolidBrush(ForeColor), 8, 8);
        }
        G.Dispose();
       
        }
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            FillClick();
        }
        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            base.OnMouseDown(mevent);
            FillNormal();
        }
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            base.OnMouseDown(mevent);
            FillNormal();
        }
        public void Ref()
        {
            FillNormal();
        }

        
    }
}

Jednak gdy próbuję opcji 'Build Solution' to otrzymuję następujący błąd Cannot implicitly convert type 'System.Drawing.ContentAlignment' to 'bool'.

Co musze poprawić i jak ?

3

Byłoby prościej jakbyś podał w której linijce ten błąd bo nie chce mi się czytać całego.

= służy do przypisania, a nie porównania, więc te twoje ify nie maja sensu.

0

Oh,przepraszam najmocniej.Błąd wyskakuje przy każdym tym fragmencie

  else if (TextAlign = ContentAlignment.MiddleCenter)
1

else if (TextAlign == ContentAlignment.MiddleCenter)

0

@DibbyDum mam jeszcze jeden problem,w tym kodzie napisanym w Basicu cały przycisk jest zamalowany,a u mnie tylko granice nie są

0

A używasz konwertera czy sam wszystko przepisujesz?

1

@Riw dodaj sobie do twoje konstruktora linijkę: FlatAppearance.BorderSize = 0;

Jesteś pewien twój kod + moja linijka:
Untitled.png

Ale cały przycisk jest niebieski albo ja ślepy jestem. :P

1 użytkowników online, w tym zalogowanych: 0, gości: 1