如何为自动生成的10个标签赋予不同的名称。 [英] how to give different name for automatically generated 10 label .
本文介绍了如何为自动生成的10个标签赋予不同的名称。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我自动生成10个标签,但现在我需要给出所有标签的不同名称如何执行此操作
i使用此代码,但此代码仅为所有标签提供一个名称
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
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);
}
}
}
}
推荐答案
在循环内,执行下面
inside the loop, do as below
labelInput.Text = j;
labelInput.Name = "label" +i;
试试这个:
Try this:
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;
}
}
这篇关于如何为自动生成的10个标签赋予不同的名称。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文