在C#中添加标签或编辑框作为按钮的操作

时间:2015-06-29 06:57:29

标签: c#

"我正在使用Visual Basic C#2010,我想创建一个用于形状绘制的表单,其中包含三种形状,即线形,矩形,椭圆形。最初我只显示三个带有文字的按钮作为' Line' ,'矩形',' Ellipse'。根据我点击的按钮,我想要标签和编辑框出现,要求绘制形状的属性。例如,如果我点击线,我想要标签要求显示长度。如果我点击了矩形,我希望标签只询问长度和宽度。那么如何将这些标签作为特定按钮的动作来实现呢? "

我已经编写了这段代码,我之前解决了如何针对不同的按钮点击不同地询问用户输入的问题。谢谢回复! 现在在这段代码中,剩下的只是创建一个可以绘制形状的区域。我不明白该怎么做。任何人都可以帮忙吗? 为什么形状没有被绘制?请注意,我想在原始表单上绘制形状而不是弹出窗体。 请帮忙。

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

namespace ShapeDrawing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public abstract class SHAPE
        {
            public Pen MyPen = new Pen(Color.Brown, 3.5F);
            public abstract void getparameters();
            public abstract void drawshape(Form1 e);
        }

        public class LINE : SHAPE
        {
            float x1,x2,y1,y2;
            public override void getparameters()
            {
                PopForLine Popup = new PopForLine();
                DialogResult dialogresult = Popup.ShowDialog();
                if (dialogresult==DialogResult.OK)
                {
                    try
                    {
                        this.x1 = (float)Convert.ToDouble(Popup.Point1XText.Text);
                        this.y1 = (float)Convert.ToDouble(Popup.Point1YText.Text);
                        this.x2 = (float)Convert.ToDouble(Popup.Point2XText.Text);
                        this.y2 = (float)Convert.ToDouble(Popup.Point2YText.Text);
                    }
                    catch(Exception e) 
                    {
                        x1=x2=y1=y2=0;
                    }
                } 
                Popup.Dispose();
            }
            public override void drawshape(Form1 e) 
            {
                Graphics formGraphics=e.CreateGraphics();
                formGraphics.DrawLine(MyPen,x1,y1,x2,y2);
                formGraphics.Dispose();
            }
        }

        class RECTANGLE : SHAPE
        {
            float Length, Breadth;
            public override void getparameters()
            {
                PopForRect Popup = new PopForRect();
                DialogResult dialogresult = Popup.ShowDialog();
                if (dialogresult == DialogResult.OK)
                {
                    try
                    {
                        Length = (float)Convert.ToDouble(Popup.LengthText.Text);
                        Breadth =(float) Convert.ToDouble(Popup.BreadthText.Text);
                    }
                    catch (Exception e) 
                    {
                        Length = 0;
                        Breadth = 0;
                    }
                }
                Popup.Dispose();
            }
             public override void drawshape(Form1 e)
             {
                Graphics formGraphics = e.CreateGraphics();
                formGraphics.DrawRectangle(MyPen, 0, 0, Length,Breadth);
                formGraphics.Dispose();            
             }
        }

        class ELLIPSE : SHAPE
        {
            float radius1, radius2;
            public override void getparameters()
            {
            PopForEllipse Popup = new PopForEllipse();
            DialogResult dialogresult = Popup.ShowDialog();
            if (dialogresult == DialogResult.OK)
            {
                try
                {
                    this.radius1 = (float)Convert.ToDouble(Popup.SRadText.Text);
                    this.radius2 = (float)Convert.ToDouble(Popup.LRadText.Text);
                }
                catch (Exception e)
                {
                    radius1 = 0;
                    radius2 = 0;
                }
            }
            Popup.Dispose();
        }
        public override void drawshape(Form1 e)
        {
            Graphics formGraphics = e.CreateGraphics();
            formGraphics.DrawEllipse(MyPen, 0, 0, radius1, radius2);
            formGraphics.Dispose();
        }
    }

    private void LineButton_Click(object sender, EventArgs e)
    {
        LINE MyLine = new LINE();
        MyLine.getparameters();
        MyLine.drawshape(this);
        MyLine.MyPen.Dispose();
    }

    private void EllipseButton_Click(object sender, EventArgs e)
    {
        ELLIPSE MyEllipse = new ELLIPSE();
        MyEllipse.getparameters();
        MyEllipse.drawshape(this);
        MyEllipse.MyPen.Dispose();
    }

        private void RectButton_Click(object sender, EventArgs e)
        {
            RECTANGLE MyRectangle = new RECTANGLE();
            MyRectangle.getparameters(); 
            MyRectangle.drawshape(this);
            MyRectangle.MyPen.Dispose();
        }
    }

}

3 个答案:

答案 0 :(得分:0)

添加标签,在每个按钮的ButtonClick事件上,您可以设置标签的Text属性

如果点击了圈子

labelName.Text ="输入半径&#34 ;;

如果点击Square

labelName.Text ="输入边长&#34 ;;

这有助于您的要求

答案 1 :(得分:0)

如果这是WPf应用程序并且您正在使用MVVM,那么在按钮单击时,您可以使用适当的文本通知您的文本属性。

答案 2 :(得分:0)

我终于得到了这段代码,并按照我想要的方式完成了工作。

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

namespace ShapeDrawing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public abstract class SHAPE
        {
            public Pen MyPen = new Pen(Color.Brown, 3.5F);
            public abstract void getparameters();
            public abstract void drawshape(Form1 e);
            public abstract void description(Form1 e);
            public void enabling(Form1 e) 
            {
                e.RetryButton.Enabled = true;
                e.ExitButton.Enabled = true;
            }
        }

        public class LINE : SHAPE
        {
            float x1,x2,y1,y2;
            public override void getparameters()
            {
                PopForLine Popup = new PopForLine();
                DialogResult dialogresult = Popup.ShowDialog();
                if (dialogresult==DialogResult.OK)
                {
                    try
                    {
                        this.x1 = (float)Convert.ToDouble(Popup.Point1XText.Text);
                        this.y1 = (float)Convert.ToDouble(Popup.Point1YText.Text);
                        this.x2 = (float)Convert.ToDouble(Popup.Point2XText.Text);
                        this.y2 = (float)Convert.ToDouble(Popup.Point2YText.Text);
                    }
                    catch(Exception e) 
                    {
                        x1=x2=y1=y2=0;
                    }
                }
                Popup.Dispose();
            }
            public override void drawshape(Form1 e) 
            {
                Graphics formGraphics = e.ShapeSpace.CreateGraphics();
                formGraphics.DrawLine(MyPen,x1,y1,x2,y2);
                formGraphics.Dispose();
            }

            public override void description(Form1 e) 
            {
                double linelength;
                linelength = Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
                e.Description.Text = "This is a line of length " + Convert.ToString(linelength) + " starting from (" + Convert.ToString(x1) + "," + Convert.ToString(y1) + ") to (" + Convert.ToString(x2) + " , " + Convert.ToString(y2)+") .\n";
            }
        }

        class RECTANGLE : SHAPE
        {
            float Length, Breadth;
            public override void getparameters()
            {
                PopForRect Popup = new PopForRect();
                DialogResult dialogresult = Popup.ShowDialog();
                if (dialogresult == DialogResult.OK)
                {
                    try
                    {
                        Length = (float)Convert.ToDouble(Popup.LengthText.Text);
                        Breadth =(float) Convert.ToDouble(Popup.BreadthText.Text);
                    }
                    catch (Exception e) 
                    {
                        Length = 0;
                        Breadth = 0;
                    }
                }
                Popup.Dispose();
            }
            public override void drawshape(Form1 e)
            {
                Graphics formGraphics = e.ShapeSpace.CreateGraphics();
                formGraphics.DrawRectangle(MyPen, 0, 0, Length,Breadth);
                formGraphics.Dispose();            
            }

            public override void description(Form1 e)
            {
                e.Description.Text = "This is a rectangle of width= " + Convert.ToString(Length) + " and height= " + Convert.ToString(Breadth) + " .\n";
            }
        }

        class ELLIPSE : SHAPE
        {
            float radius1, radius2;
            public override void getparameters()
            {
                PopForEllipse Popup = new PopForEllipse();
                DialogResult dialogresult = Popup.ShowDialog();
                if (dialogresult == DialogResult.OK)
                {
                    try
                    {
                        this.radius1 = (float)Convert.ToDouble(Popup.SRadText.Text);
                        this.radius2 = (float)Convert.ToDouble(Popup.LRadText.Text);
                    }
                    catch (Exception e)
                    {
                        radius1 = 0;
                        radius2 = 0;
                    }
                }
                Popup.Dispose();
            }
            public override void drawshape(Form1 e)
            {
                Graphics formGraphics = e.ShapeSpace.CreateGraphics();
                formGraphics.DrawEllipse(MyPen, 0, 0, radius1, radius2);
                formGraphics.Dispose();
            }

            public override void description(Form1 e)
            {
                e.Description.Text = "This is an ellipse of width= " + Convert.ToString(radius2) + " and height= " + Convert.ToString(radius1) + " .\n";
            }
        }

        private void LineButton_Click(object sender, EventArgs e)
        {
            LINE MyLine = new LINE();
            MyLine.getparameters();
            MyLine.drawshape(this);
            MyLine.MyPen.Dispose();
            MyLine.description(this);
            MyLine.enabling(this);
        }

        private void EllipseButton_Click(object sender, EventArgs e)
        {
            ELLIPSE MyEllipse = new ELLIPSE();
            MyEllipse.getparameters();
            MyEllipse.drawshape(this);
            MyEllipse.MyPen.Dispose();
            MyEllipse.description(this);
            MyEllipse.enabling(this);
        }

        private void RectButton_Click(object sender, EventArgs e)
        {
            RECTANGLE MyRectangle = new RECTANGLE();
            MyRectangle.getparameters();
            MyRectangle.drawshape(this);
            MyRectangle.MyPen.Dispose();
            MyRectangle.description(this);
            MyRectangle.enabling(this);
        }

        private void ExitButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void RetryButton_Click(object sender, EventArgs e)
        {
            Description.Text = " ";
            ShapeSpace.Image = null;
        }
    }

}