• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

Dynamic form

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
ANINDYA
Asked:
ANINDYA
  • 5
  • 5
  • 3
  • +1
3 Solutions
 
MathiyazhaganCommented:
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
 
Gururaj BadamCommented:
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
 
ANINDYAAuthor Commented:
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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
ANINDYAAuthor Commented:
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
 
Gururaj BadamCommented:
I'm not sure whether I'll be able to compile this code. Give me more concrete compilable code.

~Gururaj
0
 
Gautham JanardhanCommented:
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
 
Gururaj BadamCommented:
This will definitely work

btnFill.Click += delegate(object sender, EventArgs e)
            {
                frm.Close();
            };
0
 
ANINDYAAuthor Commented:
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
 
Gautham JanardhanCommented:

Form frm = null;
        private void button1_Click(object sender, EventArgs e)
        {
            frm = new Form();
//... rest of your code here.
}

Open in new window

0
 
Gururaj BadamCommented:
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
 
ANINDYAAuthor Commented:
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
 
ANINDYAAuthor Commented:
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
 
Gautham JanardhanCommented:
you don't need the frm.IsDisposed condition as per my modified code. sorry for the confusion
0
 
Gururaj BadamCommented:
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

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 5
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now