如何以第二种形式在光标下方显示值?

时间:2018-09-18 19:16:25

标签: c# .net winforms charts

enter image description here 我从文件数据中绘制了一个烛台图。

单击要显示的按钮时,创建一个新表单(第二个表单): 时间,高,低,打开,关闭。

namespace stock5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.Load += new System.EventHandler(this.CandleStick_Load);
        }

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

        public void CHART()
        {
             *************************************************
            //The code reads the data from the file is skipped.

            chart1.Series.Clear();
            Series price = new Series("price");
            chart1.Series.Add(price);
            chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
            chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
            chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
            chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
            chart1.ChartAreas[0].AxisY.IsStartedFromZero = false;
            chart1.Series["price"].ChartType = SeriesChartType.Candlestick;
            chart1.Series["price"]["OpenCloseStyle"] = "Triangle";
            chart1.Series["price"]["ShowOpenClose"] = "Both";
            chart1.Series["price"]["PointWidth"] = "2.0";
            chart1.Series["price"]["PriceUpColor"] = "Blue"; 
            chart1.Series["price"]["PriceDownColor"] = "Red";
            chart1.Series["price"].BorderColor = Color.Black;
            chart1.Series["price"]["MaxPixelPointWidth"] = "2.0";

            for (i = 0; i < count - 1; i++)
            {
                chart1.Series["price"].Points.AddXY(index[i], mass[i, 1], mass[i, 2], mass[i, 0], mass[i, 3]);//index, high, low, open, close
            }

            int INDEX = 0;
            foreach (DataPoint point in chart1.Series["price"].Points)
            {
                point.AxisLabel = nums[INDEX].ToString();//Replacing the index values for the time(To avoid empty values when markets are closed on weekends)
                INDEX++;
            }  
        }
    }
}

要根据光标位置打印值的第二种形式。

private void button1_Click(object sender, EventArgs e)
        {
            Form newForm = new Form();
            newForm.Show();
            newForm.Width = 170;
            newForm.Height = 230;
        }

还有一个过去的问题:如何获取代替时间形式的索引?

1 个答案:

答案 0 :(得分:0)

There is multiple ways you can communicate with first form in time and here is one of it:

I assume your Form is your new form? (I will use it in my example as it is)

For this example i will create custom class (object) which i will be returning to newly created form every 0.2 sec.

So new object would be

public class MyNewObject
{
    int Id { get; set; }
    string Name { get; set; }
    int Speed { get; set; }
}

So class of Form will look like this (at least code which is needed to perform this)

public partial class Form : Form
{
    Timer t = new Timer();
    private YourFirstForm myFirstForm;

    public Form(YourFirstForm form)
    {
        InitializeComponents();
        myFirstForm = form;

        t.Interval = 200; //0.2 sec 

        Thread t1 = new Thread(FirstFormListener);
        t1.Start();
    }

    private void FirstFormListner()
    {
        Timer t = new Timer();
        t.Interval = 200; //0.2 sec

        t.Tick += new EventHandler(timer1_Tick); 
        t.Enabled = true; 
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        MyNewObject someData = myFirstForm.GetSomeData();
        MessageBox.Show(someData.Name);
    }
}

And inside your first form:

public partial class YourFirstForm : Form
{
    string someString = "Some random string";

    public YourFirstForm()
    {
        InitializeComponents();
    }

    public MyNewObject GetSomeData()
    {
        MyNewObject mno = new MyNewObject();
        mno.Name = "Random name";
        mno.Id = 1;
        mno.Speed = 200;
        return mno;
    }
}

This is one way of doing it and i think it is practical since you can adjust interval of thick so set update time (on slower pc set it to lower value for faster you can to faster value). Also updating is done on separate thread so it doesn't affect your UI (freeze) and you can continue with job.