如何在表单1的表单2中的图表中显示数据 [英] How to display data in a chart on Form 2 from Form 1

查看:95
本文介绍了如何在表单1的表单2中的图表中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有2个表单,form1和form2。在form1上有一个按钮,需要单击该按钮才能在form2上的图表上显示数据。如何将form1中的数据传递给form2?



表格1上的代码:



I have got 2 forms in my project, form1 and form2. On form1 there is a button that needs to be clicked to display data on a chart that is on form2. How do I pass the data from form1 on to form2 ?

Code on form1:

public partial class Form1 : Form
       {

           public Form1()
           {
               InitializeComponent();
           }
           private Form2 form2 = new Form2();


           private void button2_Click(object sender, EventArgs e)
           {
               Form2 frm2 = new Form2();
               frm2.Show();

               this.chart1.Series["Speed"].Points.AddXY("James", "90");
               this.chart1.Series["Speed"].Points.AddXY("John", "18");
               this.chart1.Series["Speed"].Points.AddXY("Carl", "83");

           }





表格2上的代码:这是我被困住了,我不要如何将信息传递给form2







Code on form 2: this is were i'm stuck , I dont how to pass the information to form2


{
               InitializeComponent();

           }

           private void Form2_Load(object sender, EventArgs e)
           {

           }

推荐答案

尝试使用下面提到的一些代码片段: -
try this like some snippet code mention below:-
public partial class Form1 : Form// This coding is form1
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2();
       
        frm.chartid;
//do stuff here more

    }





你的问题不清楚你想要什么或试试。请清除您的问题。



and your question is not clear what's you want or try. plz clear your problem.


首先生成图表,然后将图表传递给新表单。创建的图表将与您的表单大小相同。



Generate your chart first then pass the chart to your new form. The chart created will be the same size as your form.

'Create form
Dim MyForm As New Form
'Create chart
Dim Chart1 As New Chart
  'Add series and format the chart
  Chart1.ChartAreas.Add("Area51")
  Chart1.Series.Add("Speed")
  Chart1.Series("Speed").ChartType = SeriesChartType.Bar

  'Populate the chart data (Remember that X and Y need to be numbers, No Strings)
  For Each line As String In IO.File.ReadAllLines("C:\FolderWithData\MyData.csv")
    Dim points() As Double = Array.ConvertAll(line.Split(","c), Function(s) CDbl(s))
    Chart01.Series("Speed").Points.AddXY(points(1), points(0))
  Next

  'Add chart to the new form and disdplay it
  MyForm.Controls.Add(Chart01)
  MyForm.Show()



这不会你不能在Points.AddXY中使用字符串,只能使用X和Y坐标等数字。您需要为数据添加标签以将名称放入。


This will not work you can not use strings in Points.AddXY, only numbers, like X and Y coordinates. You need to add labels to your data to put the names in.

this.chart1.Series["Speed"].Points.AddXY("James", "90"); NO!

chart1.Series["Speed"].Points.AddXY(0, 90);
chart1.Series["Speed"].Points.AddXY(1, 18);
chart1.Series["Speed"].Points.AddXY(2, 83);





祝你好运



Good luck


这篇关于如何在表单1的表单2中的图表中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆