Solved

Dynamic form

Posted on 2010-09-21
14
474 Views
Last Modified: 2013-12-16
Experts
I want to implement the code as explained in the codeproject (urls is
http://www.codeproject.com/KB/miscctrl/CratingDynamicForms.aspx)

Now I am facing an error in the following section
btnFill.Click += new System.EventHandler(btnFillClick);
            btnFill.Click += frm.Close();
            frm.Controls.Add(btnFill);

For more clarity of my question and to see the error please see the attached code ( here is my code which I am using and also the code in the code project original code) and attached image.

The original code of the codeproject is also attached in the code section along with my code which I am using

Thanking you

//My code is here 
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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //int a = int.Parse(txtQt.Text);
            //int a = int.Parse(txtSizeFont.Text);


            //vertical position of each object on the form
            int intVertPos = 0;
            //maximum width of labels on the form
            int intMaxWidthLabel = 0;
            //maximum width of textBox on the form
            int intWidthTextBox = 0;
            //gap between fields
            int intGapHeight = 0;
            //string to be measured
            string measureString = "";
            //Font of the string
            Font stringFont = null;
            //Size of the string
            SizeF stringSize;
            //Dynamic tabIndex
            int intIndex = 0;

            //use an invisible picturebox that will help to graphically measure strings according their font and font size
            Graphics g = pictureBox1.CreateGraphics();

            //Calculate the height gap that has to be generated. For this calculation, we have to follow the principles above:
            //The gap should be determined only by the height of the tallest object on window: the textBox
            //Simulate the drawing of a dummy textBox, that will never been showed, and retrieve its height for spacing purposes.
            TextBox dummyTextBox = new TextBox();
            dummyTextBox.Font = new System.Drawing.Font(cmbFont.Text, float.Parse(txtSizeFont.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            int intHeight = int.Parse(txtSizeFont.Text) + int.Parse(txtSizeFont.Text) / 2;
            dummyTextBox.Name = "TextBoxDummy";
            intGapHeight = dummyTextBox.Height;


            //if (frm != null) frm.Close();
            Form frm = new Form();
            frm.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            frm.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            frm.Name = "frm_test";
            //dimension is irrelevant at the moment
            frm.ClientSize = new System.Drawing.Size(10, 10);
            //the parent will be the current form
            //frm.MdiParent = this;
            //splash screen mode form, why not...
            frm.ControlBox = false;
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            frm.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            frm.BackColor = System.Drawing.Color.LightGray;
            frm.Width = 200;
            frm.Height = 400;

            //for all the content of the Labels listbox
            for (int i = 0; i < lbLabels.Items.Count; i++)
            {
                //Object label
                Label aLabel = new Label();
                aLabel.Font = new System.Drawing.Font(cmbFont.Text, float.Parse(txtSizeFont.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                //backcolor is for testing purposes, to see if the control fits correctly
                aLabel.BackColor = System.Drawing.Color.AliceBlue;
                aLabel.Location = new System.Drawing.Point(5, intVertPos);
                // Set up string.
                measureString = lbLabels.Items[i].ToString();
                stringFont = new Font(cmbFont.Text, float.Parse(txtSizeFont.Text));
                // Measure string.
                stringSize = new SizeF();
                stringSize = g.MeasureString(measureString, stringFont);
                int intWidthLabel = int.Parse(stringSize.Width.ToString("####0"));
                if (intWidthLabel > intMaxWidthLabel)
                {
                    intMaxWidthLabel = intWidthLabel;
                }
                aLabel.Size = new System.Drawing.Size(intWidthLabel, intGapHeight);
                aLabel.Name = "LabelTitle";
                aLabel.Text = lbLabels.Items[i].ToString();

                intVertPos += intGapHeight;

                frm.SuspendLayout();
                aLabel.SuspendLayout();
                frm.Controls.Add(aLabel);
            }


            // Size of textBox padding with "W" the largest char in ascii representation
            char chrPadding = 'W';
            if (int.Parse(txtQt.Text) > 30)
                measureString = measureString.PadRight(30, chrPadding);
            else
                measureString = measureString.PadRight(int.Parse(txtQt.Text), chrPadding);
            stringFont = new Font(cmbFont.Text, float.Parse(txtSizeFont.Text));
            // Measure string.
            stringSize = new SizeF();
            stringSize = g.MeasureString(measureString, stringFont);
            intWidthTextBox = int.Parse(stringSize.Width.ToString("####0"));
            intVertPos = 0;

            //for all the content of the Labels listbox - designing textbox
            for (int i = 0; i < lbLabels.Items.Count; i++)
            {
                //Object label
                TextBox aTextBox = new TextBox();
                aTextBox.Font = new System.Drawing.Font(cmbFont.Text, float.Parse(txtSizeFont.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                aTextBox.BackColor = System.Drawing.Color.Yellow;
                aTextBox.Location = new System.Drawing.Point(5 + intMaxWidthLabel, intVertPos);
                aTextBox.Size = new System.Drawing.Size(intWidthTextBox + 10, intGapHeight);
                //giving a name to all your object will be the only way to retrieve them and use them
                //for the purpose of this sample, the name can be the same for all textboxes.
                aTextBox.Name = "TextBoxTitle";
                //giving the maximun size in caracters for the textbox.
                aTextBox.MaxLength = int.Parse(txtQt.Text);
                //tab have to be ordered
                aTextBox.TabIndex = intIndex;
                intIndex += 1;
                //Vertical position is to be manage according the tallest object in the form, in this case the textbox it self
                intVertPos += intGapHeight;

                //adding the textbox to the form
                frm.SuspendLayout();
                aTextBox.SuspendLayout();
                frm.Controls.Add(aTextBox);
            }

            //put an action button to the left
            Button btnFill = new System.Windows.Forms.Button();
            btnFill.Location = new System.Drawing.Point(intMaxWidthLabel + intWidthTextBox + 20, 0);
            btnFill.Name = "btnNew";
            btnFill.Size = new System.Drawing.Size(75, 23);
            btnFill.TabIndex = intIndex;
            btnFill.Text = "&Fill";
            //define an event on click button to fill all the textboxes
            btnFill.Click += new System.EventHandler(btnFillClick);
            btnFill.Click += frm.Close();
            frm.Controls.Add(btnFill);


            //frm.Width = intMaxWidthLabel + intWidthTextBox + 95;
            //frm.Height = intVertPos + 10;
            //if (frm.Height > Screen.PrimaryScreen.WorkingArea.Height || frm.Width > Screen.PrimaryScreen.WorkingArea.Width)
            //    MessageBox.Show("Beware! The size of the window is bigger than your actual definitions...", "Warning", MessageBoxButtons.OK);
            frm.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}



//here is the code of the original code project 


/*
 * Created by SharpDevelop.
 * User: dsi-c14
 * Date: 08-01-2007
 * Time: 17:18
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace DynamicForm
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    /// 
    
    public partial class DynamicForm
    {
        Form frm;
        [STAThread]
        public static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new DynamicForm());
        }
        
        public DynamicForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            
            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }
        
    
        void BtnGenerateClick(object sender, System.EventArgs e)
        {
            //control the filling of data
            if (lbLabels.Items.Count <= 0)
            {
                MessageBox.Show("You have to add at least one label","Error", MessageBoxButtons.OK );
                txtLabel.Focus();
                return;
            }
            if (txtQt.Text == "")
            {
                MessageBox.Show("You have to fill the length of the textboxes","Error", MessageBoxButtons.OK );
                txtQt.Focus();
                return;
            }
            else    
            {
                try
                {
                    int a = int.Parse (txtQt.Text);
                }
                catch(Exception f)
                {
                    MessageBox.Show("This field should be numeric","Error", MessageBoxButtons.OK );
                    txtQt.Focus();
                    return;
                }
                if (int.Parse (txtQt.Text) < 1 || int.Parse (txtQt.Text) > 100)
                {
                    MessageBox.Show("Choose a value between 1 and 100 only","Error", MessageBoxButtons.OK );
                    txtQt.Focus();
                    return;
                }
            }
            if (cmbFont.Text == "")
            {
                MessageBox.Show("You have to choose the font","Error", MessageBoxButtons.OK );
                cmbFont.Focus();
                return;
            }
            if (txtSizeFont.Text == "")
            {
                MessageBox.Show("You have to fill the size of the font","Error", MessageBoxButtons.OK );
                txtSizeFont.Focus();
                return;
            }
            else    
            {
                try
                {
                    int a = int.Parse (txtSizeFont.Text);
                }
                catch(Exception f)
                {
                    MessageBox.Show("This field should be numeric","Error", MessageBoxButtons.OK );
                    txtSizeFont.Focus();
                    return;
                }
                if (int.Parse (txtSizeFont.Text) < 1 || int.Parse (txtSizeFont.Text) > 36)
                {
                    MessageBox.Show("Choose a value between 1 and 36 only","Error", MessageBoxButtons.OK );
                    txtSizeFont.Focus();
                    return;
                }
            }
            
            
            //vertical position of each object on the form
            int intVertPos = 0;
            //maximum width of labels on the form
            int intMaxWidthLabel = 0;
            //maximum width of textBox on the form
            int intWidthTextBox = 0;
            //gap between fields
            int intGapHeight = 0;
            //string to be measured
            string measureString = "";
            //Font of the string
            Font stringFont = null;
            //Size of the string
            SizeF stringSize;
            //Dynamic tabIndex
            int intIndex = 0;
            
            //use an invisible picturebox that will help to graphically measure strings according their font and font size
            Graphics g = pictureBox1.CreateGraphics();
            
            //Calculate the height gap that has to be generated. For this calculation, we have to follow the principles above:
            //The gap should be determined only by the height of the tallest object on window: the textBox
            //Simulate the drawing of a dummy textBox, that will never been showed, and retrieve its height for spacing purposes.
            TextBox dummyTextBox = new TextBox();
            dummyTextBox.Font = new System.Drawing.Font(cmbFont.Text, float.Parse(txtSizeFont.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            int intHeight = int.Parse(txtSizeFont.Text) + int.Parse(txtSizeFont.Text)/2;
            dummyTextBox.Name = "TextBoxDummy";
            intGapHeight = dummyTextBox.Height;
            
            
            //Draw the Form object
            //close it, if eventually existing
            if (frm != null) frm.Close();
            frm = new Form();
            frm.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            frm.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            frm.Name = "frm_test";
            //dimension is irrelevant at the moment
            frm.ClientSize = new System.Drawing.Size(10, 10);
            //the parent will be the current form
            //frm.MdiParent = this;
            //splash screen mode form, why not...
            frm.ControlBox = false;
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            frm.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            frm.BackColor = System.Drawing.Color.LightGray;
            
            //for all the content of the Labels listbox
            for (int i=0;i<lbLabels.Items.Count;i++)
            {
                //Object label
                Label aLabel = new Label();
                aLabel.Font = new System.Drawing.Font(cmbFont.Text, float.Parse(txtSizeFont.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                //backcolor is for testing purposes, to see if the control fits correctly
                aLabel.BackColor = System.Drawing.Color.AliceBlue;   
                aLabel.Location = new System.Drawing.Point(5, intVertPos);
                // Set up string.
                measureString = lbLabels.Items[i].ToString();
                stringFont = new Font(cmbFont.Text, float.Parse(txtSizeFont.Text));
                // Measure string.
                stringSize = new SizeF();
                stringSize = g.MeasureString(measureString, stringFont);
                int intWidthLabel = int.Parse(stringSize.Width.ToString("####0"));
                if (intWidthLabel > intMaxWidthLabel)
                {
                    intMaxWidthLabel = intWidthLabel;
                }
                aLabel.Size = new System.Drawing.Size(intWidthLabel, intGapHeight);
                aLabel.Name = "LabelTitle";
                aLabel.Text = lbLabels.Items[i].ToString();
                
                intVertPos += intGapHeight;
                
                frm.SuspendLayout();
                aLabel.SuspendLayout();
                frm.Controls.Add(aLabel);
            }
            

            // Size of textBox padding with "W" the largest char in ascii representation
            char chrPadding = 'W';
            if (int.Parse(txtQt.Text) > 30)
                measureString = measureString.PadRight(30, chrPadding);
            else
                measureString = measureString.PadRight(int.Parse(txtQt.Text), chrPadding);
            stringFont = new Font(cmbFont.Text, float.Parse(txtSizeFont.Text));
            // Measure string.
            stringSize = new SizeF();
            stringSize = g.MeasureString(measureString, stringFont);
            intWidthTextBox = int.Parse(stringSize.Width.ToString("####0"));
            intVertPos = 0;
            
            //for all the content of the Labels listbox - designing textbox
            for (int i=0;i<lbLabels.Items.Count;i++)
            {
                //Object label
                TextBox aTextBox = new TextBox();
                aTextBox.Font = new System.Drawing.Font(cmbFont.Text, float.Parse(txtSizeFont.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                aTextBox.BackColor = System.Drawing.Color.Yellow;   
                aTextBox.Location = new System.Drawing.Point(5 + intMaxWidthLabel, intVertPos);
                aTextBox.Size = new System.Drawing.Size(intWidthTextBox + 10, intGapHeight);
                //giving a name to all your object will be the only way to retrieve them and use them
                //for the purpose of this sample, the name can be the same for all textboxes.
                aTextBox.Name = "TextBoxTitle";
                //giving the maximun size in caracters for the textbox.
                aTextBox.MaxLength = int.Parse(txtQt.Text);
                //tab have to be ordered
                aTextBox.TabIndex = intIndex;
                intIndex +=1;
                //Vertical position is to be manage according the tallest object in the form, in this case the textbox it self
                intVertPos += intGapHeight;
                
                //adding the textbox to the form
                frm.SuspendLayout();
                aTextBox.SuspendLayout();
                frm.Controls.Add(aTextBox);
            }

            //put an action button to the left
             Button btnFill = new System.Windows.Forms.Button();
               btnFill.Location = new System.Drawing.Point(intMaxWidthLabel + intWidthTextBox + 20,  0);
            btnFill.Name = "btnNew";
            btnFill.Size = new System.Drawing.Size(75, 23);
            btnFill.TabIndex = intIndex;
            btnFill.Text = "&Fill";
            //define an event on click button to fill all the textboxes
            btnFill.Click += new System.EventHandler(btnFillClick);
            frm.Controls.Add(btnFill);

            
            frm.Width = intMaxWidthLabel + intWidthTextBox + 95;
            frm.Height = intVertPos + 10;
            if (frm.Height > Screen.PrimaryScreen.WorkingArea.Height || frm.Width > Screen.PrimaryScreen.WorkingArea.Width)
                MessageBox.Show("Beware! The size of the window is bigger than your actual definitions...", "Warning", MessageBoxButtons.OK);
            frm.Show();
        }
        
        private void btnFillClick(object sender, System.EventArgs e)
        {
            foreach (Control chcontrol in frm.Controls)
            {
                if (chcontrol.Name == "TextBoxTitle")
                 {
                    //just to see if the changes apply
                    if (chcontrol.Text == "Filling Test")
                       chcontrol.Text = "Filling Test Again";
                    else        
                       chcontrol.Text = "Filling Test";
                 }
            }
        }
        
        void MainFormLoad(object sender, System.EventArgs e)
        {
            cmbFont.Items.Clear();
            cmbFont.Items.Add("Times new roman");
            cmbFont.Items.Add("Arial");
            cmbFont.Items.Add("Tahoma");
        }
        
        void TxtLabelKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                lbLabels.Items.Add(txtLabel.Text);
                txtLabel.Clear();
            }
        }
        
        void LbLabelsDoubleClick(object sender, System.EventArgs e)
        {
            lbLabels.Items.RemoveAt(lbLabels.SelectedIndex);
        }
        
        
        
        
    }
}

Open in new window

error.JPG
0
Comment
Question by:ANINDYA
  • 5
  • 5
  • 3
  • +1
14 Comments
 
LVL 10

Accepted Solution

by:
Mathiyazhagan earned 167 total points
Comment Utility
hi,
as you added event
   btnFill.Click += new System.EventHandler(btnFillClick);
in ypur code, you should need to create a method named "btnFillClick" to bind it to btnFill.Click . I have attached the same code from code project article; add this code to your form.
and
  btnFill.Click += frm.Close();
here, you are assigning a mehod of frm object to an event. it is wrong .you should remove it .

//

// Event click function

//

private void btnFillClick(object sender,System.EventArgs e)

    {

        foreach (Control chcontrol in frm.Controls)

        {

            if (chcontrol.Name == "TextBoxTitle")

         {

                //just to see if the changes apply

                if (chcontrol.Text == "Filling Test")

                   chcontrol.Text = "Filling Test Again";

                else        

                   chcontrol.Text = "Filling Test";

         }

    }

}

Open in new window

0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
You're seeing the first error as the compiler is not able to find btnFillClick in the same class as btnFill.Click +=...

The second error because of the statement btnFill.Click += frm.Close(); is very simple. You're trying to a method return to a event handler.

I believe changing btnFill.Click += btnFillClick; will fix your problem
0
 

Author Comment

by:ANINDYA
Comment Utility
Expert Mathiyazhagan
your suggestion is working fine but there is a problem that is if I press the generate button second time after closing the dynamically created form then the following error is coming .
Please see the attached image for more clarification.
Thanking you,
anindya
error.JPG
0
 

Author Comment

by:ANINDYA
Comment Utility
Novice Novice
it is nice to see you again .
sir I think I have made some changes and  please see the attaced code here and suggest me why I am not been able to get the form created again when I am pressing the generate button.
take regards
anindya
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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Form frm = new Form();
        private void button1_Click(object sender, EventArgs e)
        {
            //int a = int.Parse(txtQt.Text);
            //int a = int.Parse(txtSizeFont.Text);


            //vertical position of each object on the form
            int intVertPos = 0;
            //maximum width of labels on the form
            int intMaxWidthLabel = 0;
            //maximum width of textBox on the form
            int intWidthTextBox = 0;
            //gap between fields
            int intGapHeight = 0;
            //string to be measured
            string measureString = "";
            //Font of the string
            Font stringFont = null;
            //Size of the string
            SizeF stringSize;
            //Dynamic tabIndex
            int intIndex = 0;

            //use an invisible picturebox that will help to graphically measure strings according their font and font size
            Graphics g = pictureBox1.CreateGraphics();

            //Calculate the height gap that has to be generated. For this calculation, we have to follow the principles above:
            //The gap should be determined only by the height of the tallest object on window: the textBox
            //Simulate the drawing of a dummy textBox, that will never been showed, and retrieve its height for spacing purposes.
            TextBox dummyTextBox = new TextBox();
            dummyTextBox.Font = new System.Drawing.Font(cmbFont.Text, float.Parse(txtSizeFont.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            int intHeight = int.Parse(txtSizeFont.Text) + int.Parse(txtSizeFont.Text) / 2;
            dummyTextBox.Name = "TextBoxDummy";
            intGapHeight = dummyTextBox.Height;


            //if (frm != null) frm.Close();
            
            frm.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            frm.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            frm.Name = "frm_test";
            //dimension is irrelevant at the moment
            frm.ClientSize = new System.Drawing.Size(10, 10);
            //the parent will be the current form
            //frm.MdiParent = this;
            //splash screen mode form, why not...
            frm.ControlBox = false;
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            frm.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            frm.BackColor = System.Drawing.Color.LightGray;
            frm.Width = 800;
            frm.Height = 400;

            //for all the content of the Labels listbox
            for (int i = 0; i < lbLabels.Items.Count; i++)
            {
                //Object label
                Label aLabel = new Label();
                aLabel.Font = new System.Drawing.Font(cmbFont.Text, float.Parse(txtSizeFont.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                //backcolor is for testing purposes, to see if the control fits correctly
                aLabel.BackColor = System.Drawing.Color.AliceBlue;
                aLabel.Location = new System.Drawing.Point(5, intVertPos);
                // Set up string.
                measureString = lbLabels.Items[i].ToString();
                stringFont = new Font(cmbFont.Text, float.Parse(txtSizeFont.Text));
                // Measure string.
                stringSize = new SizeF();
                stringSize = g.MeasureString(measureString, stringFont);
                int intWidthLabel = int.Parse(stringSize.Width.ToString("####0"));
                if (intWidthLabel > intMaxWidthLabel)
                {
                    intMaxWidthLabel = intWidthLabel;
                }
                aLabel.Size = new System.Drawing.Size(intWidthLabel, intGapHeight);
                aLabel.Name = "LabelTitle";
                aLabel.Text = lbLabels.Items[i].ToString();

                intVertPos += intGapHeight;

                frm.SuspendLayout();
                aLabel.SuspendLayout();
                frm.Controls.Add(aLabel);
            }


            // Size of textBox padding with "W" the largest char in ascii representation
            char chrPadding = 'W';
            if (int.Parse(txtQt.Text) > 30)
                measureString = measureString.PadRight(30, chrPadding);
            else
                measureString = measureString.PadRight(int.Parse(txtQt.Text), chrPadding);
            stringFont = new Font(cmbFont.Text, float.Parse(txtSizeFont.Text));
            // Measure string.
            stringSize = new SizeF();
            stringSize = g.MeasureString(measureString, stringFont);
            intWidthTextBox = int.Parse(stringSize.Width.ToString("####0"));
            intVertPos = 0;

            //for all the content of the Labels listbox - designing textbox
            for (int i = 0; i < lbLabels.Items.Count; i++)
            {
                //Object label
                TextBox aTextBox = new TextBox();
                aTextBox.Font = new System.Drawing.Font(cmbFont.Text, float.Parse(txtSizeFont.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                aTextBox.BackColor = System.Drawing.Color.Yellow;
                aTextBox.Location = new System.Drawing.Point(5 + intMaxWidthLabel, intVertPos);
                aTextBox.Size = new System.Drawing.Size(intWidthTextBox + 10, intGapHeight);
                //giving a name to all your object will be the only way to retrieve them and use them
                //for the purpose of this sample, the name can be the same for all textboxes.
                aTextBox.Name = "TextBoxTitle";
                //giving the maximun size in caracters for the textbox.
                aTextBox.MaxLength = int.Parse(txtQt.Text);
                //tab have to be ordered
                aTextBox.TabIndex = intIndex;
                intIndex += 1;
                //Vertical position is to be manage according the tallest object in the form, in this case the textbox it self
                intVertPos += intGapHeight;

                //adding the textbox to the form
                frm.SuspendLayout();
                aTextBox.SuspendLayout();
                frm.Controls.Add(aTextBox);
            }

            //put an action button to the left
            Button btnFill = new System.Windows.Forms.Button();
            btnFill.Location = new System.Drawing.Point(intMaxWidthLabel + intWidthTextBox + 20, 0);
            btnFill.Name = "btnNew";
            btnFill.Size = new System.Drawing.Size(75, 23);
            btnFill.TabIndex = intIndex;
            btnFill.Text = "&Fill";
            //define an event on click button to fill all the textboxes
            btnFill.Click += new System.EventHandler(btnFillClick);
            //btnFill.Click += frm.Close();
            
            frm.Controls.Add(btnFill);


            //frm.Width = intMaxWidthLabel + intWidthTextBox + 95;
            //frm.Height = intVertPos + 10;
            //if (frm.Height > Screen.PrimaryScreen.WorkingArea.Height || frm.Width > Screen.PrimaryScreen.WorkingArea.Width)
            //    MessageBox.Show("Beware! The size of the window is bigger than your actual definitions...", "Warning", MessageBoxButtons.OK);
            frm.Show();
        }


        private void btnFillClick(object sender, System.EventArgs e)
        {
            
            frm.Close();
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

Open in new window

0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
I'm not sure whether I'll be able to compile this code. Give me more concrete compilable code.

~Gururaj
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility
that because the form would have been disposed by now. second time open the form you need to recreate it.

You can do a check like
if(frm.IsDisposed)
{
// create again
}
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
This will definitely work

btnFill.Click += delegate(object sender, EventArgs e)
            {
                frm.Close();
            };
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ANINDYA
Comment Utility
Expert gauthampj
I am not getting the exact location where I am supposed to put the recreation of the form.
Please see the attached image for more clarity.
Thanking you,
anindya
error.JPG
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility

Form frm = null;

        private void button1_Click(object sender, EventArgs e)

        {

            frm = new Form();

//... rest of your code here.

}

Open in new window

0
 
LVL 8

Assisted Solution

by:Gururaj Badam
Gururaj Badam earned 166 total points
Comment Utility
try this
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 WindowsFormsApplication2

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        Form frm = null;

        private void button1_Click(object sender, EventArgs e)

        {

		frm =	new Form();

            //int a = int.Parse(txtQt.Text);

            //int a = int.Parse(txtSizeFont.Text);





            //vertical position of each object on the form

            int intVertPos = 0;

            //maximum width of labels on the form

            int intMaxWidthLabel = 0;

            //maximum width of textBox on the form

            int intWidthTextBox = 0;

            //gap between fields

            int intGapHeight = 0;

            //string to be measured

            string measureString = "";

            //Font of the string

            Font stringFont = null;

            //Size of the string

            SizeF stringSize;

            //Dynamic tabIndex

            int intIndex = 0;



            //use an invisible picturebox that will help to graphically measure strings according their font and font size

            Graphics g = pictureBox1.CreateGraphics();



            //Calculate the height gap that has to be generated. For this calculation, we have to follow the principles above:

            //The gap should be determined only by the height of the tallest object on window: the textBox

            //Simulate the drawing of a dummy textBox, that will never been showed, and retrieve its height for spacing purposes.

            TextBox dummyTextBox = new TextBox();

            dummyTextBox.Font = new System.Drawing.Font(cmbFont.Text, float.Parse(txtSizeFont.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

            int intHeight = int.Parse(txtSizeFont.Text) + int.Parse(txtSizeFont.Text) / 2;

            dummyTextBox.Name = "TextBoxDummy";

            intGapHeight = dummyTextBox.Height;





            //if (frm != null) frm.Close();

            

            frm.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

            frm.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            frm.Name = "frm_test";

            //dimension is irrelevant at the moment

            frm.ClientSize = new System.Drawing.Size(10, 10);

            //the parent will be the current form

            //frm.MdiParent = this;

            //splash screen mode form, why not...

            frm.ControlBox = false;

            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;

            frm.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;

            frm.BackColor = System.Drawing.Color.LightGray;

            frm.Width = 800;

            frm.Height = 400;



            //for all the content of the Labels listbox

            for (int i = 0; i < lbLabels.Items.Count; i++)

            {

                //Object label

                Label aLabel = new Label();

                aLabel.Font = new System.Drawing.Font(cmbFont.Text, float.Parse(txtSizeFont.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

                //backcolor is for testing purposes, to see if the control fits correctly

                aLabel.BackColor = System.Drawing.Color.AliceBlue;

                aLabel.Location = new System.Drawing.Point(5, intVertPos);

                // Set up string.

                measureString = lbLabels.Items[i].ToString();

                stringFont = new Font(cmbFont.Text, float.Parse(txtSizeFont.Text));

                // Measure string.

                stringSize = new SizeF();

                stringSize = g.MeasureString(measureString, stringFont);

                int intWidthLabel = int.Parse(stringSize.Width.ToString("####0"));

                if (intWidthLabel > intMaxWidthLabel)

                {

                    intMaxWidthLabel = intWidthLabel;

                }

                aLabel.Size = new System.Drawing.Size(intWidthLabel, intGapHeight);

                aLabel.Name = "LabelTitle";

                aLabel.Text = lbLabels.Items[i].ToString();



                intVertPos += intGapHeight;



                frm.SuspendLayout();

                aLabel.SuspendLayout();

                frm.Controls.Add(aLabel);

            }





            // Size of textBox padding with "W" the largest char in ascii representation

            char chrPadding = 'W';

            if (int.Parse(txtQt.Text) > 30)

                measureString = measureString.PadRight(30, chrPadding);

            else

                measureString = measureString.PadRight(int.Parse(txtQt.Text), chrPadding);

            stringFont = new Font(cmbFont.Text, float.Parse(txtSizeFont.Text));

            // Measure string.

            stringSize = new SizeF();

            stringSize = g.MeasureString(measureString, stringFont);

            intWidthTextBox = int.Parse(stringSize.Width.ToString("####0"));

            intVertPos = 0;



            //for all the content of the Labels listbox - designing textbox

            for (int i = 0; i < lbLabels.Items.Count; i++)

            {

                //Object label

                TextBox aTextBox = new TextBox();

                aTextBox.Font = new System.Drawing.Font(cmbFont.Text, float.Parse(txtSizeFont.Text), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

                aTextBox.BackColor = System.Drawing.Color.Yellow;

                aTextBox.Location = new System.Drawing.Point(5 + intMaxWidthLabel, intVertPos);

                aTextBox.Size = new System.Drawing.Size(intWidthTextBox + 10, intGapHeight);

                //giving a name to all your object will be the only way to retrieve them and use them

                //for the purpose of this sample, the name can be the same for all textboxes.

                aTextBox.Name = "TextBoxTitle";

                //giving the maximun size in caracters for the textbox.

                aTextBox.MaxLength = int.Parse(txtQt.Text);

                //tab have to be ordered

                aTextBox.TabIndex = intIndex;

                intIndex += 1;

                //Vertical position is to be manage according the tallest object in the form, in this case the textbox it self

                intVertPos += intGapHeight;



                //adding the textbox to the form

                frm.SuspendLayout();

                aTextBox.SuspendLayout();

                frm.Controls.Add(aTextBox);

            }



            //put an action button to the left

            Button btnFill = new System.Windows.Forms.Button();

            btnFill.Location = new System.Drawing.Point(intMaxWidthLabel + intWidthTextBox + 20, 0);

            btnFill.Name = "btnNew";

            btnFill.Size = new System.Drawing.Size(75, 23);

            btnFill.TabIndex = intIndex;

            btnFill.Text = "&Fill";

            //define an event on click button to fill all the textboxes

            btnFill.Click += delegate(object sender, System.EventArgs e)

		{

			frm.Close();

			frm = null;

		}

            

            frm.Controls.Add(btnFill);





            //frm.Width = intMaxWidthLabel + intWidthTextBox + 95;

            //frm.Height = intVertPos + 10;

            //if (frm.Height > Screen.PrimaryScreen.WorkingArea.Height || frm.Width > Screen.PrimaryScreen.WorkingArea.Width)

            //    MessageBox.Show("Beware! The size of the window is bigger than your actual definitions...", "Warning", MessageBoxButtons.OK);

            frm.Show();

        }





        private void Form1_Load(object sender, EventArgs e)

        {



        }

    }

}

Open in new window

0
 

Author Comment

by:ANINDYA
Comment Utility
Expert gauthampj
I am facing an error that is
object reference not set to an instance of an object.
For more clarity please see the attached image.
Thanking you
anindya

error.JPG
0
 

Author Comment

by:ANINDYA
Comment Utility
Expert Novice Novice
please see the attached image.
I have used your latest code which you provided .
there is an error.
Thanking you
anindya
error.JPG
0
 
LVL 29

Assisted Solution

by:Gautham Janardhan
Gautham Janardhan earned 167 total points
Comment Utility
you don't need the frm.IsDisposed condition as per my modified code. sorry for the confusion
0
 
LVL 8

Expert Comment

by:Gururaj Badam
Comment Utility
change sender to s and e to args

I advise you to see the error description and think about it before asking. You're completely expecting to be spoon fed - not very good.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now