Jump to content

How to use textbox's value of form1 from form2

RK_Takashi's Photo
Posted Oct 22 2012 10:18 PM
2594 Views

Dear Sir:
I am new in C# and I have a question for how to use my textbox value of form1 from form2.
I have two form; form1 & form2. At the form1 I have 4 TextBox for input some value & one Button for open the form2.
I would like to set the TextBox1 input as x1, TextBox2 input as y1 and so on. At the form2 I have a PictureBox for draw a
line & one Button for execute the draw program. At the beginning I open the form1 & input x1, y1, x2, y2 value into TextBoxes,
then I click the Draw button in form2 to draw a line in it. But I do know how to set the draw program of form2 to use the x1, y1,
x2, y2 value of Form1. Could anyone please help me? Thanks a lot. I am using Visual studio 2012 and my code as following:

At form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Form1ToForm2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public void TextBox_Set()
{
int x1 = Convert.ToInt32(textBox1.Text);
int y1 = Convert.ToInt32(textBox2.Text);
int x2 = Convert.ToInt32(textBox3.Text);
int y2 = Convert.ToInt32(textBox4.Text);
}

private void button1_Click_1(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.Show();
}
}
}

At form2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Form1ToForm2
{
public partial class Form2 : Form
{

public Form2()
{
InitializeComponent();
}

public void Draw_gp(object sender, PaintEventArgs e)
{
Graphics g = pictureBox1.CreateGraphics();
int x1, y1, x2, y2;
x1 = Convert.ToInt32(f1.textBox1.Text); // This setting does not worK!!!
y1 = Convert.ToInt32(f1.textBox2.Text); // This setting does not worK!!!
x2 = Convert.ToInt32(f1.textBox3.Text); // This setting does not worK!!!
y2 = Convert.ToInt32(f1.textBox4.Text); // This setting does not worK!!!
Pen p = new Pen(Color.Green, 2);
Point[] cp =
{
new Point ( x1, y1), new Point ( x2, y2)
};
g.DrawCurve(p, cp);
}

private void button1_Click(object sender, EventArgs e)
{
Paint += new PaintEventHandler(Draw_gp);
}
}
}


Best regards.

Tags:
0 Subscribe


0 Replies