返回首页

我已经有一个类,在窗口形式应用形状程序,但我不知道如何写在Form1.cs的代码显示的面积和形状绘制组合框后选择 - 圆,三角形,正方形。当点击"区域"按钮",并绘制形状时,单击"绘制",以及如何启用和禁用输入号码在使用时,即文本框。圈需要只填写半径,然后改变ComboBox的三角形的需求,以填补基地和高度。

Shape类如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing;

 

namespace Class_Shape

{

class Shape

{

public string[] GetShape()

{

 

string []s = {"circle","triangle","square"};

return s;

 

}

public double CircleArea(double radius)

{

return Math.PI * radius * radius;

}

public double RectangleArea(double width, double height)

{

return width * height;

}

public double TangleArea(double width,double height)

{

return 0.5 * width * height;

}

public void DrawCircle(double r,Form1 f)

{

Pen big = new Pen(Brushes.Red ,5);

f.Refresh ();

f.CreateGraphics().DrawEllipse(big, (float) 110,(float) 160,(float) r,(float) r);

f.CreateGraphics().FillEllipse(Brushes.YellowGreen , (float)110, (float)160, (float)r, (float)r);

}

public void DrawRect(double w,double h, Form1 f)

{

Pen big = new Pen(Brushes.Red, 5);

f.Refresh();

f.CreateGraphics().DrawRectangle (big, (float)110, (float)160, (float)w, (float)h);

f.CreateGraphics().FillRectangle (Brushes.YellowGreen, (float)110, (float)160, (float)w, (float)h);

}

public void DrawTang(double w, double h, Form1 f)

{

Pen big = new Pen(Brushes.Red, 5);

Point [] p = {new Point(120, 160),new Point(120,160+(int)h), new Point(120+(int)w,160+(int)h) } ;

f.Refresh();

f.CreateGraphics().DrawPolygon(big, p);

f.CreateGraphics().FillPolygon(Brushes.YellowGreen , p);

}

}

}

回答

评论会员:游客 时间:2012/02/07
SAKryukov:OK,我可以看到你的主要问题是:你正在试图完全滥用OOP的。这是无法修复。代表作为分类的目的字符串的形状,简直是犯罪的,对自己不利。把所有的一类,是一个面向对象的滥用,会导致你一事无成。您将需要一个抽象的形状类,并从中获得一些具体形状类。你需要使用继承和多态。你是根本没有准备好,以解决这类问题。不要浪费你的时间,停止它。了解一些面向对象编程,NET和C#mdash;从头开始。这将需要太多的文字,给你参考。嗯,看了一些书。是的,我100%肯定。你在做什么也没用。不要回到这个问题,直到你熟悉所有的技术和概念,首先训练自己最简单的练习,可能没有用户界面,就像控制台应用程序。当你面对一些具体问题,并能够对您的问题,准确mdash;回来,我们会很乐意帮助你。mdash;的SA
PingpinkApple
评论会员:游客 时间:2012/02/07
感谢您的建议,我可以解决的方案使用System.Collections.Generic使用System.ComponentModel;使用System.Data;使用System.Drawing;使用System.Linq的;使用System.Text;使用System.Windows.Forms;命名空间Class_Shape{公共部分类Form1:表格{形状S=新的形状()市民向Form1() {()} 私人无效Form1_Load的(对象发件人,EventArgs的五) { cbbShape.DataSource=s.GetShape(); }私人无效btnCal_Click(对象发件人,EventArgs的五){//圆如果(cbbShape.SelectedIndex==0){txtArea.Text=s.CircleArea(Convert.ToDouble(txt1.Text)的)的ToString() } //三角 否则,如果(cbbShape.SelectedIndex==1) { 。txtArea.Texts.TangleArea(Convert.ToDouble(txt1.Text)的,Convert.ToDouble(txt2.Text)的)的ToString() } //矩形否则,如果(cbbShape.SelectedIndex==2) {。txtArea.Texts.RectangleArea(Convert.ToDouble(txt1.Text)的,Convert.ToDouble(txt2.Text)的)的ToString() }}私人无效cbbShape_SelectedIndexChanged(对象发件人,EventArgs的五){ 如果(cbbShape.SelectedIndex==0) { label4.Text=";Radiusquot; txt2.Visible=FALSE; lblHeight.Visible=FALSE;} 否则,如果(cbbShape.SelectedIndex==1) { label4.Text=";hightquot;lblHeight.Visible=真;txt2.Visible=真;} 否则,如果(cbbShape.SelectedIndex==2){ label4.Text=";widthquot;lblHeight.Visible=真;txt2.Visible=真; }}私人无效btnDraw_Click(对象发件人,EventArgs的五) {//圆如果(cbbShape.SelectedIndex==0){ s.DrawCircle(Convert.ToDouble(txt1.Text)) }//三角如果(cbbShape.SelectedIndex==1){ s.DrawTang(Convert.ToDouble(txt1.Text),Convert.ToDouble(txt2.Text),本); } //矩形 否则,如果(cbbShape.SelectedIndex==2) { s.DrawRect(Convert.ToDouble(txt1.Text),Convert.ToDouble(txt2.Text),本); }} }}
试试这个链接:



{A1}
评论会员:游客 时间:2012/02/07
YvesDaoust:所有必需的文本框上安装的工作形式您可以通过他们的Visible属性设置为true或false,他们的出现,并在会消失。另一种选择是让所有的文本框可见,但它们需要启用/禁用。为此目的,使用Enabled属性。你可能会在ComboBox的SelectionChanged事件处理程序编写