Solved

Dynamic form

Posted on 2010-09-21
14
482 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
ID: 33731750
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
ID: 33731849
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
ID: 33731854
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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

Author Comment

by:ANINDYA
ID: 33731870
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
ID: 33731880
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
ID: 33731894
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
ID: 33731897
This will definitely work

btnFill.Click += delegate(object sender, EventArgs e)
            {
                frm.Close();
            };
0
 

Author Comment

by:ANINDYA
ID: 33731939
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
ID: 33731951

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
ID: 33731960
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
ID: 33731977
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
ID: 33731991
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
ID: 33731994
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
ID: 33732000
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 Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

832 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