Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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");

           }


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


C#
{
               InitializeComponent();

           }

           private void Form2_Load(object sender, EventArgs e)
           {

           }
Posted

try this like some snippet code mention below:-
C#
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.
 
Share this answer
 
Generate your chart first then pass the chart to your new form. The chart created will be the same size as your form.

VB
'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()

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.
VB
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900