Click here to Skip to main content
15,885,186 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have 2 forms

the first includes on his page a panel (Size = 400X400)
and a button that opens the second form
that includes 2 textbox and a button

the user need to enter Width and Height and then press the button

I want that the size entered by the user will change the panel on the first (main) form without creating another object of form1 (Main) ?

form 1 - Main - Panel + Button<br />
form 2 - .... - 2 textboxs + button<br />
<br />
form 1 (Main) - code --> <br />

C#
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private int X2, Y2;
        public Form5(int W2, int H2)
        {
            X2 = W2;
            Y2 = H2;
            InitializeComponent();
            panel1.Size = new Size(X2, Y2);
        }

        private void Enter(object sender, EventArgs e) /->Button Event<-\
        {
            Form6 form2 = new Form2();
            form2.Show();
        }
    }
}

form2 - code ->
C#
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
        }

        private void BtnClick(object sender, EventArgs e) /->Button Event<-\
        {
            int W2 = int.Parse(textBox1.Text);
            int H2 = int.Parse(textBox2.Text);
            Form5 form5 = new Form5(W2, H2);
            form5.Show();
        }
    }
}

Please Help ???
Posted
Updated 28-Oct-11 7:30am
v3

Form2 with the textboes:

C#
namespace WindowsFormsApplication1
{
    public partial class Form6 : Form
    {
        private int panelWidth;
        private int panelHeight;

        public int PanelWidth
        {
            return panelWidth;
        }

        public int PanelHeight
        {
            return panelHeight;
        }


        public Form6()
        {
            InitializeComponent();
        }

        private void BtnClick(object sender, EventArgs e)
        {
            panelWidth = int.Parse(textBox1.Text);
            panelHeight = int.Parse(textBox2.Text);

            Close();
        }
    }
}



Form 1 with the panel:

C#
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void Enter(object sender, EventArgs e) /->Button Event<-\
        {
            Form6 form2 = new Form2();
            form2.ShowDialog();

            panel1.Size = new Size(form2.PanelWidth, form2.PanelHeight);
        }
    }
}
 
Share this answer
 
v2
Comments
[no name] 28-Oct-11 13:41pm    
+5I like yours better than mine. No need for a wrapper object.

Not sure what I was thinking :P
Sergey Alexandrovich Kryukov 28-Oct-11 13:43pm    
Yes, a 5.
--SA
Tim Groven 28-Oct-11 14:02pm    
Forgot that TextBoxes might not be available after the form is closed. Updated solution.
phil.o 28-Oct-11 15:34pm    
Have my 4.
I would enclose the Form6 instance in a using block in respect to Dispose pattern.
And I would correct the typos, also :)
You can inject another object to hold onto the references.

C#
public class PanelSize
{
   public int Width {get; set;}
   public int Height {get; set;}
}



Here we have a holder for it in the Form that is receiving inputs.
C#
public PanelSize Dimensions {get; set;}


Which changes the click event to be this:

C#
private void BtnClick(object sender, EventArgs e) /->Button Event<-\
{
    Dimensions.Width = int.Parse(textBox1.Text);
    Dimensions.Height = int.Parse(textBox2.Text);
}


Then rather than doing a Show() you can do a ShowDialog. Then after the ShowDialog() runs you can capture the value and set it. If you require a Show() to work you will need to do some event triggering or actual binding.

Here is how you would do with ShowDialog
C#
public PanelDimensions Dimensions {get; private set;}

private void Enter(object sender, EventArgs e)
{
   //Set up the PanelDimensions object that is to be injected
    Dimensions = new PanelDimensions();
    Form6 form2 = new Form2();
    form2.Dimensions = Dimensions; //This means both are now sharing this object and it is a class so it is by reference, i.e. same data
form
    form2.ShowDialog();
    
    panel1.Width = Dimensions.Width;
    panel1.Height = Dimensions.Height;
}
 
Share this answer
 
v2

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