473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Calculate and Draw the Shapes

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:
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);
}
}
}
Attached Images
File Type: jpg pic.jpg (20.3 KB, 706 views)
Dec 1 '11 #1
1 5805
arie
64
To do what you want, you basicly have to handle events of your form and controls. For example, to enable/disable some controls when combobox value is changed, you have to handle SelectedIndexChanged event of your combobox (you just need to double-click it in your design window and the method will be created for you :)) You do enabling/disabling here.

Also, you may want to override OnPaint method of your form to draw your shapes (and set DoubleBuffered property of your form to true to prevent flickering). If you use it, your shape will be redrawn every time your form is redrawn (for example after it was covered by another form)
Here they describe how it's used: msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx

Also, in Click event of button "DRAW" you decide if currently chosen shape is drawn instead of the previous one, e.g:
Expand|Select|Wrap|Line Numbers
  1. string currentShape = "none";
  2. string chosenShape = "none"
  3. private void cbxChooseShape_SelectedIndexChanged(object sender, EventArgs e)
  4. {
  5.   chosenShape = cbxChooseShape.Text;
  6.   // also disable/enable your controls here
  7. }
  8. private void btnDraw_Click(object sender, EventArgs e)
  9. {
  10.  currentShape = chosenShape;
  11.  
  12.  // read parameters such as shape width, height,radius...
  13.  
  14.  this.Refresh();
  15. }
  16. protected override void OnPaint(PaintEventArgs e)
  17. {
  18.    base.OnPaint(e);
  19.    switch(currentShape)
  20.    {
  21.     case "circle": //draw circle here
  22.       break;
  23.     case "triangle": //draw triangle here
  24.       break;
  25.     case "square": //draw square here
  26.       break;
  27.     default: //don't draw anything if shape not chosen
  28.       break;
  29.    }
  30. }
I also wouldn't pass Form1 objects to your drawing methods, instead pass Graphics object - no need to do CreateGraphics afterwards. Just pass e.Graphic to your drawing method in OnPaint(). Your class will be more universal this way, and when you create new form class called ThisStupidFormClass you won't have to modify your Shape class.
Dec 1 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Colin McGuire | last post by:
Hi there. I have written a small procedure to draw various shapes on things. A bit of it is shown below. Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics) Select Case...
2
by: Agnes | last post by:
in vb.net , any tools can draw a line the the form ?? or rectangle box ? Thanks Form Agnes
3
by: Daniel Mark | last post by:
Hello all: I want to draw some shapes, such as lines, circles on an image. The input to the program is an image and the output from the program is a superimposed image. what kind of...
2
by: SrideviMani | last post by:
Hi, I just want to draw primitive shapes like lines,circles,rectangles etc. I found a method : Graphics.drawline(Pen,x1,y1,x2,y2) The code I used is as follows: Pen blackPen = gcnew Pen(...
3
by: Spooner | last post by:
I was wonder how to center 4 lines of output like this: cout << "********" << endl; cout << "* *" << endl; cout << "* *" << endl; cout << "********" << endl; I am trying to draw...
1
by: weberwhennner | last post by:
Hi All, Since two days Ive been trying to draw lines and polygons interactively (using mouse clicks), but all ive found is some libraries that draw shapes using predefined cooredinates. Ive...
3
by: fauxanadu | last post by:
I am creating a program where the user selects a page and a map loads in a picture control. They can then draw shapes to the screen on a layer the map that are transparant. Later, they can click a...
1
by: =?Utf-8?B?bWlzc0JsdWVCYXI=?= | last post by:
Could someone please tell me how to add a circle (ellipse) to an InkCanvas but as a stroke - a collection of StylusPoints. The requirements is to allow the user to create a sketch on the canvas...
1
by: taishin | last post by:
so far i got this... but the shapes that i created i need to be able to click and dragg and move around with the mouse..help!!!! and also some where here i need to draw and equal sided triangle ...
3
by: Brandon Arnold | last post by:
I have a panel that contains a label and a picturebox. When clicked, I want to draw a rectangle over everything. I can draw the rect and set the color, no big deal. The problem is because I have to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.