Calculating the circumference with C# Windows Forms App - c#

For school, I have to create a calculator where you enter the radius of a circle and it calculates the circumference and the area.
I tried coding it and everything I tried just won't work. It kept giving me errors along the lines of it having difficulty converting the textbox into a double, and other errors. This is my code:
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 Circumference
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
txtCircumference = Math.PI * Math.Pow(txtRadius, 2);
txtArea = 2 * Math.PI * txtRadius;
}
}
}
I'm used to coding with python so C# is kinda out of my comfort zone.

You may be new to C# but the most glaring issue is with WinForm controls. My answer assumes that txtRadius, txtCircumference, and txtArea are all TextBox controls.
Note I am writing this freeform and not in a VS editor so I may have errors.
private void btnCalculate_Click(object sender, EventArgs e)
{
if (!double.TryParse(txtRadius.Text, out double radius))
{
throw new Exception("Radius is a not a valid double.");
}
txtCircumference.Text = (Math.PI * radius * radius).ToString();
txtArea.Text = (2.0 * Math.PI * radius).ToString();
}
Observe that each TextBox has a .Text string property. For me to interchange a double with a string requires me to parse the string into a double, or else use ToString() to write the double as a string.

Related

Drawing a circle on a pictureBox from another winform

I have two winforms in my application. One of the forms has a picturebox with a jpg loaded of our building plan. The main form has code that does facial recognition identifying people coming into certain areas. I have been asked to modify this program to show an identified individual's location on the building plan. I have a database that has all the X,Y coordinates of the locations that should map to the building plan image. I have looked around and tried to find some code that will draw a circle on the map at the X,Y coordinates as the person progresses through areas of the building by erasing all the existing circles and updating this new one. So on the map form I put in the following code:
public void DrawCircle(int x, int y)
{
Graphics gf = pictureBox1.CreateGraphics();
gf.DrawEllipse(new Pen(Color.Red), new Rectangle(x, y, 400, 400));
pictureBox1.Refresh();
}
Then from the update method (right now a button click for testing) on the main form I call this method on the map form. The method gets called, but the circle doesn't show up on the form. I have tried both Refresh and Invalidate and neither method seems to draw the circle on the image.
I haven't done winforms development for years, so I'm sure I am missing some plumbing somewhere. Here is the code on the mainform:
LocationMap map = new LocationMap();
public Form1()
{
InitializeComponent();
//set up signalR
UserName = "MovementHub1";
ConnectAsync();
//show the map screen
map.Show();
map.WindowState = FormWindowState.Maximized;
...
Then in a click event (for testing right now) I have this code:
private void button2_Click(object sender, EventArgs e)
{
map.DrawCircle(340, 258);
}
Once I get the circle drawn on the other form, then I will remove the code from the click event and move it another event that does the updating on the location. If it's possible, I would like to put a label by the circle that has the person's name. Right now this is a proof of concept, I just need help getting the circle on the form to start with.
Thanks.
I tried It out by myself and came up with that:
Form1.cs
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 StackoverflowHelp
{
public partial class Form1 : Form
{
Form2 form = new Form2();
public Form1()
{
InitializeComponent();
form.Show();
}
private void Button1_Click(object sender, EventArgs e)
{
form.DrawCircle(100, 100);
}
}
}
Form2.cs
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 StackoverflowHelp
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
DrawCircle(10, 10);
}
public void DrawCircle(int x, int y)
{
Graphics gf = Graphics.FromImage(pictureBox1.Image);
gf.DrawEllipse(new Pen(Color.Red), new Rectangle(x, y, 20, 20));
gf.Dispose();
pictureBox1.Refresh();
pictureBox1.Invalidate();
pictureBox1.Update();
}
}
}
Instead of calling CreateGraphics() on the picturebox I created the graphics object using the current image.

C# Moving a cross into a picturebox based on the Thumbstick Position

I want to draw a cross into a picturebox. In the next step I want to move the cross based on my Data.
Which is the Thumbsticks position. I want it to appear similar to the figure below.
Here is my Code:
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;
using SlimDX;
using SlimDX.XInput;
namespace Controller_Test
{
public partial class Form1 : Form
{
GamepadState Controller = new GamepadState(UserIndex.One);
Graphics drawArea;
public Form1()
{
InitializeComponent();
timer1.Enabled = true;
drawArea = pictureBox1.CreateGraphics();
}
private void timer1_Tick(object sender, EventArgs e)
{
Controller.Update();
var LeftStick = Controller.LeftStick;
float LeftStickX = LeftStick.Position.X;
float LeftStickY = LeftStick.Position.Y;
Pen blackPen = new Pen(Color.Black);
// horizontal Line
drawArea.DrawLine(blackPen, x1+LeftStickX, y1-LeftStickY, x2+LeftStickX, y2-LeftStickY);
// vertical Line
drawArea.DrawLine(blackPen, x1+LeftStickX, y1-LeftStickY, x2+LeftStickX, y2-LeftStickY);
}
}
public class GamepadState
{Get Postiton......}
}
My problem now is, if I move the Thumbstick every time a new cross is drawn but the old one is still in the
Picturebox like in the figure below. If i use the pictureBox1.Invalidate():/pictureBox1.Refresh(); Funtcion
the cross becomes invisible and is not displayed anymore. The interval of the timer is on 1.
What could i change in order to get the code to work?

Rotate a cube with my method

I have a class with this method
rotire.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using System.Collections;
namespace WpfApplication3
{
class rotire:MonoBehaviour
{
float speed = 10f;
public void rotiree()
{
transform.Rotate(new Vector3(15,40,45)*speed,Time.deltaTime);
}
}
}
I want use this method to rotate my cube made in XAML.
Unfortunately it not work and I think my code is incorectly.
Please, can someone help me whit an ideea, what i should write.
Window1,cs
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
rotire rot = new rotire();
rot.rotiree();
mycube.Transform = rot;
}
I belive the last line of code is wrong, because i receive this error
"Cannot implicitly convert type 'WpfApplication3.rotire' to 'System.Windows.Media.Media3D.Transform3D'"
That last line should be the following:
mycube.Transform = rot.transform;

Calculating a circle's area by using two classes

Its the first time I've used this forum! I'm a second year university student and have just started writing code in C# (as were we did java last year).
One of the lab exercises is to write a small program that pops up a terminal window asks for a number (decimal number) this is meant to be the radius the program calculates the area by calling the method from another class!
I've written the code in Visual Studio 2008, using the same namespace, it builds and runs but doesn't work?
Here is the code with the different classes, any help/advice would be appreciated.
The Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter The Radius:");//Text to be displayed in the console
Console.ReadLine();//Read the line and store information in temp directory
Pie one = new Pie();//Calls the method from the class in the same namespace
Console.ReadKey();//Reads and displays the next key pressed in the console
Environment.Exit(0);//Exit the Enviromet
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program4
{
class Pie
{
public void Pin ()
{
int r;//defining the value that is going to be entered as an integer
double result;//declaring the result string as a double
r = (int)Convert.ToDouble(Console.ReadLine());
result=(3.14*r*r);//the calculation to work out pie
Console.WriteLine("The Radius of the circle is " + (result));//the writeline statement
}
}
}
You could try running the code:
Pie one = new Pie();
one.Pin();
Also:
this line:
Console.ReadLine();//Read the line and store information in temp directory
that comment is very wrong. It should be //Read the line and throws the result away
and this: (int)Convert.ToDouble(Console.ReadLine());
could be replaced by this: int.Parse(Console.ReadLine())
add static to class Pie and public void Pin(). It will work
static class Pie
{
public static void Pin ()
{
int r;//defining the value that is going to be entered as an integer
double result;//declaring the result string as a double
r = (int)Convert.ToDouble(Console.ReadLine());
result=(3.14*r*r);//the calculation to work out pie
Console.WriteLine("The Radius of the circle is " + (result));//the writeline statement
}
}
or if you prefer you can instantiate the class and then call the method like that
Pie pie=new Pie();
pie.Pin();

Are vector based generators the best way to create barcodes?

Is vector based generators the best way to generate barcodes? If yes, what are the namespaces that it will make use of? How is it used? Can anyone share some knowledge on this?
Assuming that we are talking about UPC like barcodes, vector based generation is not a must. It's the matter of representing some bits as vertical lines. So, you can easily do this using any graphic library or even using direct access to video buffer. You can represent a single bit with multiple pixels if you need a larger barcode. You don't need to use any interpolation I guess. But if you need a certain size (in pixels/centimeters etc.), vector based solution might be handful but still not a must.
C# source code example for generating scalable barcode graphics.
Steps:
1) Open a new C# Windows Forms sample project named BarCode.
2) Add a PictureBox and change BackColor to White and Dock to Fill.
3) Add Load and Resize events to Form1.
4) Copy & Paste the source code below over Form1.cs file.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BarCode
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public bool[] barCodeBits;
private void Form1_Load(object sender, EventArgs e)
{
Random r = new Random();
int numberOfBits = 100;
barCodeBits = new bool[numberOfBits];
for(int i = 0; i < numberOfBits; i++) {
barCodeBits[i] = (r.Next(0, 2) == 1) ? true : false;
}
Form1_Resize(null, null);
}
private void Form1_Resize(object sender, EventArgs e)
{
int w = pictureBox1.Width;
int h = pictureBox1.Height;
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(pictureBox1.Image);
Brush b = new SolidBrush(Color.Black);
for(int pos = 0; pos < barCodeBits.Length; pos++) {
if(barCodeBits[pos]) {
g.FillRectangle(b, ((float)pos / (float)barCodeBits.Length) * w, 0, (1.0f / (float)barCodeBits.Length) * w, h);
}
}
}
}
}
You don't have to develop barcodes using vector based graphics. I fact have a look at this link on codeproject as most of the work is already done for you. This genrates a bitmap of the required barcode.

Categories

Resources