Passed value is not returned
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | // 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() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.ClientSize = new System.Drawing.Size(800, 650); this.Text="line5"; this.components = new System.ComponentModel.Container(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.SuspendLayout(); 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"; this.Controls.Add(this.btnLine); // 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); this.ResumeLayout(false); 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( this.ClientRectangle.Width, this.ClientRectangle.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); InitializeDrawingArea(); } // 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) angle=0; Console.WriteLine("**timer1 angle= {0}", angle); float myangle = angle; if(angle == 90) this.timer1.Enabled = false; Invalidate(); } 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。是这样吗?
在处理某些变量时,您似乎也在尝试使用属性。
在total中包含一个集合,它可以帮助其他程序员准确理解您正在做什么。
1 | public float Total { get { return angle; } set { angle = value; } } |
然后改变
1 | float Total = angle; |
到
1 | Total = angle; |
您最初拥有的是声明了一个名为total的局部变量,一旦您达到该函数的作用域,该值就会消失。为了保持该值,您需要使用角度值设置属性total。
另外,在将来,您可能希望对您的问题有一点更详细的了解。您需要发布更相关的代码,比如invalidate()中的代码。我已经编辑了你的问题,当发布代码示例时,使用小二进制0和1将代码与实际内容分开。它帮助那些想帮助你的人:)。
我几乎诚实地认为你的计时器得到它的值是因为那个条件(如果条件)是满足的并且角度被重置为0。否则,您将需要post invalidate()来查看这是否会更改角度值。
如果你的计时器经常运行,也许你混淆了你对时间间隔的计算,那么它会经常运行,以至于它会很快通过359,然后被设置为0,然后循环会再次开始。如前所述,设置断点。把它放在invalidate语句上,然后鼠标悬停在angle上,看看它的值是什么。
另外,
因为这里的原始海报上没有答案,可能出了什么问题。
希望这有帮助。