Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I already have a class to do a shape program in window form application , but I don't know how to write a code in Form1.cs to show the area of shapes and draw after choosing the combobox - circle , triangle, square. when click the 'Area button' and draw the shape when click 'draw' , and how to enable and disable textbox of typing number when in use , ie. Circle needs to fill radius only , then change the combobox to triangle needs to fill base and height.

The Shape Class is below:
C#
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);
}
}
}
Posted
Updated 30-Nov-11 19:41pm
v2
Comments
Sergey Alexandrovich Kryukov 1-Dec-11 2:38am    
Would you consider moving to WPF? All shapes are already there. :-)
--SA
Sergey Alexandrovich Kryukov 1-Dec-11 2:38am    
OK, and what's your problem?
--SA

OK, I can see your main problem: you are trying to totally abuse OOP. This is beyond repair. Representing a shape as a string for classification purpose is simply a crime, against yourself. Putting all in one class is an abuse of OOP and will lead you nowhere. You would need to make an abstract shape class and derive a number of concrete shape classes from it. You need to use inheritance and polymorphism.

You are simply not ready to solve such problem. Don't waste your time, stop it. Learn some OOP, .NET and C# — from scratch. It would take too much text to give you all the reference. Well, read some books. Yes, I'm 100% sure. What are you doing is useless. Don't get back to this problem until you are familiar with all the techniques and concepts, first train yourself on simplest exercises, probably without UI, just as console applications.

When you face some specific problem and able to pose your problem accurately — come back; we will gladly help you.

—SA
 
Share this answer
 
 
Share this answer
 
Thanks for your recommendations,I can solve the program!

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

namespace Class_Shape
{
public partial class Form1 : Form
{
Shape s = new Shape();


public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

cbbShape.DataSource = s.GetShape();
}

private void btnCal_Click(object sender, EventArgs e)
{
// Circle

if (cbbShape.SelectedIndex == 0)
{

txtArea.Text = s.CircleArea(Convert.ToDouble(txt1.Text)).ToString();



}
// Triangle

else if (cbbShape.SelectedIndex == 1)
{
txtArea.Text = s.TangleArea(Convert.ToDouble(txt1.Text), Convert.ToDouble(txt2.Text)).ToString();

}
// Rectangle
else if(cbbShape.SelectedIndex == 2)
{
txtArea.Text = s.RectangleArea(Convert.ToDouble(txt1.Text), Convert.ToDouble(txt2.Text)).ToString();
}
}

private void cbbShape_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbbShape.SelectedIndex == 0)
{
label4.Text = "Radius";
txt2.Visible = false;
lblHeight.Visible = false;
}
else if(cbbShape.SelectedIndex == 1)
{
label4.Text = "hight";
lblHeight.Visible = true;
txt2.Visible = true;

}
else if (cbbShape.SelectedIndex == 2)
{
label4.Text = "width";
lblHeight.Visible = true;
txt2.Visible = true;

}
}

private void btnDraw_Click(object sender, EventArgs e)
{
// Circle
if (cbbShape.SelectedIndex == 0)
{
s.DrawCircle(Convert.ToDouble(txt1.Text),this);


}
// Triangle
if (cbbShape.SelectedIndex == 1)
{
s.DrawTang(Convert.ToDouble(txt1.Text),Convert.ToDouble(txt2.Text), this);


}
// Rectangle

else if (cbbShape.SelectedIndex == 2)
{

s.DrawRect(Convert.ToDouble(txt1.Text), Convert.ToDouble(txt2.Text), this);
}

}


}
}
 
Share this answer
 
Install all required TextBoxes on the working form.

You can make them appear and disappear at will by setting their Visible property to true or false.

Another option is to keep all TextBoxes visible but enable/disable them as required. Use the Enabled property for that purpose.

You will probably code this in the handler of the SelectionChanged event of the ComboBox.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900