开发者

Passed value is not returned

开发者 https://www.devze.com 2023-01-07 04:56 出处:网络
// here is the full code. // I would expect the btnLine_Click() method to start the timer (which it does) and then
// 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);
       开发者_JS百科         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
                }



    }

}


Maybe just maybe your timer does > 359 in which Total is reset to 0. Could that be the case?

It also appears you are trying to use properties when working with some variables.

Include a set inside of total, it helps other programmers understand exactly what you are doing.

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

Then change

float Total = angle;

to

Total = angle;

What you originally have is you've declared a local variable called Total, and that value is gone once you've reached the scope of that function. In order to maintain the value you want to set the property Total with the value of angle.

Also in the future you may want to be a bit more detailed about your issue. You need to post more relevant code like what is inside Invalidate(). And I've edited your question, when posting code samples use the little binary 0's and 1's to seperate code from actual content. It helps the people who want to help you :).

I almost honestly think your timer gets its values because that condition (if condition) is satisfied and angle is reset to 0. Otherwise, you will need to post Invalidate() to see if that changes the value of angle.


If your timer is running very often, perhaps you have mixed up your calculation of the interval, then it would run so often that it would quickly pass 359 and then be set to 0, and then the cycle would start again. As mentioned, set a breakpoint. Put it on the Invalidate statement and then mouse over Angle and see what it's value is.

Also float Total=angle; isn't accomplishing anything because Total is a local variable in this context and never gets used.


Since no answer form the original poster here what might possibly be wrong.

  1. The timer is not started. (from my comment)
  2. Setting the variable angle volatile might fix the problem. Usually, when reading/writing a variable from different threads, it might happen that the value is not updated.

Hope this help.

0

精彩评论

暂无评论...
验证码 换一张
取 消