如何摆脱闪烁

时间:2018-10-17 21:40:50

标签: c# .net winforms charts

namespace stock4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.chart1.AxisViewChanged += chart1_AxisViewChanged;
            this.chart1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.chart1_MouseMove);
        }

        private void CandleStick_Load(object sender, EventArgs e)
        {
            CHART();
        }

        string path = @"C:\Users\1\Documents\Visual Studio 2013\Projects\stock3\stock3\bin\Debug\#S-PG1440.csv";
        static int count = System.IO.File.ReadAllLines(@"C:\Users\1\Documents\Visual Studio 2013\Projects\stock3\stock3\bin\Debug\#S-PG1440.csv").Length;
        int[] index = new int[count];
        DateTime[] nums = new DateTime[count];
        double[,] mass = new double[count, 4];
        public void CHART()
        {   
        //Here the data from the file is read and entered into the array.
        //chart1.Series["price"].Points.AddXY(index[i], mass[i, 1], mass[i, 2], mass[i, 0], mass[i, 3]);
        }

        private void chart1_AxisViewChanged(object sender, ViewEventArgs e)
        {
         //Autoscaling the graph
        }

        public static string str;

        private void button1_Click(object sender, EventArgs e)
        {
            Form newForm = new Form();
            newForm.DoubleBuffered = true;//Error   1 Cannot access 
            //protected member 'System.Windows.Forms.Control.DoubleBuffered' 
            //via a qualifier of type 'System.Windows.Forms.Form';
            //the qualifier must be of type 'stock4.Form1' 
            //(or derived from it)  
            newForm.Show();
            newForm.Width = 150;
            newForm.Height = 230;
            newForm.BackColor = Color.White;
            newForm.Paint += new PaintEventHandler(MyPaintHandler);
        }

        private void chart1_MouseMove(object sender, MouseEventArgs e)
        {
            chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(e.Location, false);
            chart1.ChartAreas[0].CursorY.SetCursorPixelPosition(e.Location, false);
            int val = (int)chart1.ChartAreas[0].CursorX.Position;
            if (val >= 0)
            {
                double current = chart1.ChartAreas[0].CursorY.Position;
                str = "time: " + nums[val] + "\n" +
                    "current: " + current + "\n" +
                    "open:    " + mass[val, 0] + "\n" +
                    "high: "    + mass[val, 1] + "\n" +
                    "low: "     + mass[val, 2] + "\n" +
                    "close: "   + mass[val, 3];
            }
        }

        static void MyPaintHandler(object objSender, PaintEventArgs pea)
        {
            Form newForm = (Form)objSender;
            Graphics grfx = pea.Graphics;
            grfx.DrawString(str, newForm.Font, Brushes.Black, 0, 0);
            newForm.Invalidate();
            Thread.Sleep(1);
        }
    }
}

我从表格中读取数据并将其传递给另一个显示。 我创建一个表单来在按钮事件发生时显示(button1_Click)。 “ str”的数据来自另一种形式。

也许不需要第二种形式? 我需要一个与主窗体不同的窗口来显示数据。 更新了密码!部分代码不在主题上,已从注释中删除。

如何将字符串“ newForm.DoubleBuffered = true”正确地放入“ button1_Click”中?可能有代码示例吗?

enter image description here

1 个答案:

答案 0 :(得分:2)

忘记绘画,双缓冲等。

只需在第二个表单中添加一个Label控件,然后在图表的MouseMove事件中访问它即可!

使newForm为类级变量:

Form newForm = null;

在您的按钮中,单击类似这样的文字:

newForm = new Form();
..
Label lbl = new Label() { Name = "myLabel", Parent = newForm };
newForm.Show();

在您的MouseMove中输入以下内容:

if (newForm != null && newForm.Controls.ContainsKey("myLabel"))
   ((Label)newForm.Controls["myLabel"]).Text = str;

非常简单,没有闪烁,您可以随意设置Label的样式。

enter image description here