CSharp - 未返回 C# 传递的值


//here is the full code.

//I would expect the btnLine_Click() method to start the timer (which it does) and then 

//accept"angle" from the"public float myangle" getter but it does not. So it only draws//one 30 degree hard coded line. I desire the line to be incremented around in 10

//degree steps sort of like the second hands of a clock.

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Drawing.Imaging;

using System.Collections.Generic; 

using System.Text.RegularExpressions;

using System.IO; 

using System.Text;

namespace DrawingShapesApp


 public class Form1 : Form


 private Bitmap DrawingArea;

 private Pen myPen;

 private System.Windows.Forms.Timer timer1;

 private System.ComponentModel.IContainer components;

 private Button btnLine;

 float angle;

 public float x1=300;//1st point pair

 public float y1=500;

 public float x2=500;//2nd point pair

 public float y2=300;

 public double angleX = 30;//30 degrees

 public double Xdist;

 public double sqrt;

 public Form1()




 protected override void Dispose( bool disposing )


 if( disposing )


 if (components!= null) 





 base.Dispose( disposing );


 private void InitializeComponent()


 this.ClientSize = new System.Drawing.Size(800, 650); 


 this.components = new System.ComponentModel.Container();

 this.timer1 = new System.Windows.Forms.Timer(this.components);


 btnLine = new Button();

 this.btnLine.Location = new System.Drawing.Point(8, 88);

 this.btnLine.Name ="btnLine";

 this.btnLine.Size = new System.Drawing.Size(40, 40);

 this.btnLine.TabIndex = 1;

 this.btnLine.Text ="Step"; 


//Load this"frmGraphics_Load" at load time

 this.Load += new System.EventHandler(this.frmGraphics_Load);

 this.Closed += new System.EventHandler(this.frmGraphics_Closed);

 this.Paint += new System.Windows.Forms.PaintEventHandler(this.frmGraphics_Paint);


 myPen = new Pen(Color.Blue);

 this.btnLine.Click += new System.EventHandler(this.btnLine_Click);


 static void Main() 


 Application.Run(new Form1());


 private void frmGraphics_Load(object sender, System.EventArgs e)//a clickable event


 Console.WriteLine("First...load this: frmGraphics_Load");

//create a new bitmap...defining the bitmap the same size as the form

 DrawingArea = new Bitmap(






//copy from bitmap to the form

 private void InitializeDrawingArea()


 Graphics oGraphics;//oGraphics is the form

 oGraphics = Graphics.FromImage(DrawingArea);//copy bitmap to form

 myPen.Color = Color.AliceBlue;

 for ( int x = 0; x 359)


 Console.WriteLine("**timer1 angle= {0}", angle); 

 float myangle = angle;

 if(angle == 90) 

 this.timer1.Enabled = false; 



 public float myangle


 set { angle = value; }

 get { return angle; }


 private void btnLine_Click(object sender, System.EventArgs e)


 this.timer1.Enabled = true;

 this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

//timer now active...why does it not take the angle from timer1_Tick()?

//I need to increment the drawline by 10 degrees like the hand of a clock

 Console.WriteLine("in btnLine_Click"); 

//draws a hardcoded line but cannot get"angle" from timer1_Tick()

 Graphics oGraphics; 

 oGraphics = Graphics.FromImage(DrawingArea);//copy from bitmap to form

 Pen Grn = new Pen(Color.Green, 2); 

 Pen penBlue = new Pen(Color.Blue, 1); 

 Rectangle rect = new Rectangle(100, 100, 400, 400);//draw circle

 oGraphics.DrawEllipse( penBlue, rect ); 

 oGraphics.DrawLine(Grn, 300, 300, 500, 300);//draw horiz. line 

 PointF p1 = new PointF(x1, y1);

 PointF p2 = new PointF(x2, y2); 

 double Xdist = Math.Abs(p2.X - p1.X);

 Console.WriteLine("Xdist= {0}", Xdist); 

 double angle30 = Math.Cos(DegreeToRadian(30));//need myangle in place of 30

 Console.WriteLine("angle30= {0}", angle30); 

 double side_c = Math.Tan(DegreeToRadian(angleX)) * Xdist;

 Console.WriteLine("side_c= {0}", side_c); 

 sqrt = (Xdist*Xdist + side_c*side_c);

 double side_a = Math.Sqrt(sqrt); 

 Console.WriteLine("side_a= {0}", side_a); 

//this draws a 

 oGraphics.DrawLine(Grn, 300, 300, 300+(float)173.21, 300-100);//intersects circle

 oGraphics.DrawLine(Grn, 300+(float)173.21, 300, 300+(float)173.21, 300-100);//draw vert. line

 oGraphics.Dispose();//clean up graphics object 

 this.Invalidate();//force form to redraw itself


 private double DegreeToRadian(double angle)


 return Math.PI * angle/180.0;


 private double RadianToDegree(double angle)


 return angle * (180.0/Math.PI);


 public void timer_stop()


 Console.WriteLine("Halting timer!"); 

 this.timer1.Enabled = false;//halts operation




时间: 原作者:

也许你的定时器会出现> 359,总的时间被重置为 0. 可能是这种情况?



public float Total { get { return angle; } set { angle = value; } }


float Total = angle;

Total = angle;


你最初已经声明了一个称为sum的局部变量,一旦达到了该函数的范围,该值就会消失。 使用角度值来维护要设置属性总数的值。

将来你可能希望对你的问题有更详细的了解。 你需要发布更多相关代码,如 Invalidate() 内的内容。 我编辑了你的问题,当发布代码示例使用小二进制 0和 1来分离实际内容的代码。 它帮助那些想要帮助你的人:) 。

我几乎认为你的定时器会得到它的值,因为这个条件是( 如果条件),角度被重置为0. 否则,你需要 post Invalidate() 来查看是否改变了角度的值。