O'Reilly Forums: How To Use Value Of Textbox Of Form1 From Form2 - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Use Value Of Textbox Of Form1 From Form2

#1 User is offline   RK_Takashi 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 21-October 12

Posted 21 October 2012 - 09:49 PM

Dear Sir:
I had two form, form1 & form2. At form1 I had 4 TextBox for input some value & one Button for click it to shows form2, at form2 I had one PictureBox for show my draw & one button for draw a cuver line and my code as following;

public Form1()
{
InitializeComponent();
}

public void TextBox_Set()
{

double x1 = Convert.ToDouble(textBox1.Text);
double y1 = Convert.ToDouble(textBox2.Text);
double x2 = Convert.ToDouble(textBox3.Text);
double y2 = Convert.ToDouble(textBox4.Text);
}

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

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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;
Pen p = new Pen(Color.Green, 2);
Pen p1 = new Pen(Color.Red, 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);
}

My question is how can I use the values which I input into the 4 Textboxs of form1?
Could anyone please help me? Thanks a lot.
0

#2 User is offline   Sikta_26785 

  • Active Member
  • PipPipPip
  • Group: Members
  • Posts: 67
  • Joined: 18-May 11

Posted 22 October 2012 - 09:31 AM

You just need to pass the values in via the constructor of Form2. You will want to put some validation checks in to ensure that the user has entered a valid value into the textboxes before calling the constructor though.

public Form1()
{
    InitializeComponent();
}

private void button1_Click_1(object sender, EventArgs e)
{
    Form2 f2 = new Form2(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text), Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text));
    f2.Show();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public partial class Form2 : Form
{
    private int x1, y1, x2, y2;

    public Form2(int val1, int val2, int val3, int val4)
    {
        x1 = val1;
        y1 = val2;
        x2 = val3;
        y2 = val4;
        InitializeComponent();
    }

    public void Draw_gp(object sender, PaintEventArgs e)
    {
        Graphics g = pictureBox1.CreateGraphics();
        Pen p = new Pen(Color.Green, 2);
        Pen p1 = new Pen(Color.Red, 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);
    }

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users