Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i generate 10 label automatically but now i required to give the different name of all the label how to do this
i use this code but this code is only give one name for all label
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private static Microsoft.Office.Interop.Excel.Workbook mWorkBook;
        private static Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
        private static Microsoft.Office.Interop.Excel.Worksheet mWSheet1;
        private static Microsoft.Office.Interop.Excel.Application oXL;
        private List<textbox> inputTextBoxes;
        public Form1()
        {
            InitializeComponent();
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.AutoSize = true;
            this.Padding = new Padding(0, 0, 20, 20);
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = @"C:\KBE\solid piston1.xls";

            oXL = new Microsoft.Office.Interop.Excel.Application();

            oXL.Visible = false;

            oXL.DisplayAlerts = false;

            mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false);

            //Get all the sheets in the workbook

            mWorkSheets = mWorkBook.Worksheets;

            //Get the allready exists sheet


            mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("INPUT");
            Excel.Range xlRange = mWSheet1.UsedRange;


            //Get the number of input text boxes to generate
            int inputNumber = Int32.Parse(textBox1.Text);

            //Initialize list of input text boxes
            inputTextBoxes = new List<textbox>();
            
           
            //Generate labels and text boxes
            for (int i = 1; i <= inputNumber; i++)
               
                   
            {
                //Create a new label and text box
                Label labelInput = new Label();
                TextBox textBoxNewInput = new TextBox();

                int p = 2;
    
                {     
                    string j = (mWSheet1.Cells[p++, 2].Text);
                    labelInput.Text = j;    
                        
                }
            


                //Initialize label's property
              
                
                labelInput.Location = new Point(30, textBox1.Bottom + (i * 30));
                labelInput.AutoSize = true;

                //Initialize textBoxes Property
                textBoxNewInput.Location = new Point(labelInput.Width, labelInput.Top - 3);

                //Add the newly created text box to the list of input text boxes
                inputTextBoxes.Add(textBoxNewInput);

                //Add the labels and text box to the form
                this.Controls.Add(labelInput);
                this.Controls.Add(textBoxNewInput);
            }
        }
    }
}
Posted
Updated 12-Jun-14 0:16am
v2
Comments
[no name] 12-Jun-14 6:27am    
Why can't you use "i" for your label name?
[no name] 12-Jun-14 7:25am    
because mu name is coming from Excelsheet not manually
[no name] 12-Jun-14 7:36am    
No it is not. You are not setting the Name property anywhere. And the other problem is, is that you expect us to read your mind.
Pikoh 12-Jun-14 7:39am    
are the labels text loading correctly? if so,just add labelInput.name==j before labelInput.text=j;

inside the loop, do as below
C#
labelInput.Text = j; 
labelInput.Name = "label" +i;
 
Share this answer
 
v2
Comments
[no name] 12-Jun-14 6:40am    
bz i required to give name from excelshheet sodiffrent lable diffrent name that is coming from excel
Try this:
C#
private void Form1_Load(object sender, EventArgs e)
{
    int y=50;
    for (int x = 0; x <= 4; x++)
    {
        Label label = new Label();
        label.Text = "Label " + x.ToString();
        label.Name = "Label " + x.ToString();
        label.Location = new Point(10, y);
        this.Controls.Add(label);
        y += 50;
    }
}
 
Share this answer
 
Comments
[no name] 12-Jun-14 6:48am    
my label name is not label1,label2 but my label name is length,width etc,so differnt label have diffrent name

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900