不返回传递的值

时间:2010-07-13 17:41:44

标签: 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()
        {
          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
                }



    }

}

3 个答案:

答案 0 :(得分:5)

也许只是你的计时器可以> 359其中Total被重置为0.可能是这种情况吗?

在处理某些变量时,您似乎也在尝试使用属性。

在整体中包含一个集合,它可以帮助其他程序员准确理解你在做什么。

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

然后改变

float Total = angle;

Total = angle;

你最初拥有的是你已经声明了一个名为Total的局部变量,一旦你达到了该函数的范围,该值就消失了。为了保持您想要设置属性Total的值,使用angle。

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

我几乎诚实地认为你的计时器得到它的值,因为满足条件(如果条件)并且角度重置为0.否则,你需要发布Invalidate()以查看是否改变了角度的值

答案 1 :(得分:2)

如果您的计时器经常运行,也许您已经混淆了计算间隔,那么它会经常运行以至于它会快速通过359然后设置为0,然后循环将再次开始。如上所述,设置断点。将它放在Invalidate语句上,然后将鼠标悬停在Angle上,看看它的值是什么。

同样float Total=angle;没有完成任何事情,因为Total在此上下文中是一个局部变量,永远不会被使用。

答案 2 :(得分:1)

由于这里的原始海报没有回答可能出错的地方。

  1. 计时器未启动。 (来自我的评论)
  2. 设置可变角度volatile可能会解决问题。通常,当从不同的线程读取/写入变量时,可能会发生该值未更新的情况。
  3. 希望得到这个帮助。