我如何dynamic显示标签中的多个值?

我试图build立一个应用程序,在Excel文件的特定列中searchstring,如果findstring,显示相应的列值为例如:说我正在searchExcel文件列“N”中的string,如果我find的string被find,然后显示同一行的“E”列的值。事情是,我的代码显示多个单一的值在多个标签的第二种forms。

FORM1

Imports Excel = Microsoft.Office.Interop.Excel Imports Microsoft.Office.Interop.Excel Imports System.Globalization Imports System.Runtime.InteropServices Public Class Form1 Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim xlApp As Excel.Application Dim xlWorkBook As Excel.Workbook Dim xlWorkSheet As Excel.Worksheet Dim range As Excel.Range Dim Obj As Object Dim pass As String If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName) MessageBox.Show("You have selected" + OpenFileDialog1.FileName) 'sr.Close() End If xlApp = New Excel.Application xlWorkBook = xlApp.Workbooks.Open(OpenFileDialog1.FileName) xlWorkSheet = xlWorkBook.Worksheets("sheet1") range = xlWorkSheet.UsedRange For rCnt = 1 To range.Rows.Count For cCnt = 14 To range.Columns.Count If xlWorkSheet.Cells(rCnt, cCnt).value = "3" Or xlWorkSheet.Cells(rCnt, cCnt).value = "4" Or xlWorkSheet.Cells(rCnt, cCnt).value = "5" Or xlWorkSheet.Cells(rCnt, cCnt).value = "6" Or xlWorkSheet.Cells(rCnt, cCnt).value = "7" Or xlWorkSheet.Cells(rCnt, cCnt).value = "8" Or xlWorkSheet.Cells(rCnt, cCnt).value = "9" Or xlWorkSheet.Cells(rCnt, cCnt).value = "10" Then Obj = CType(range.Cells(rCnt, "E"), Excel.Range) 'MessageBox.Show(Obj.value) Foo = Obj.value Form2.Show() End If Next Next xlWorkBook.Close() xlApp.Quit() releaseObject(xlApp) releaseObject(xlWorkBook) releaseObject(xlWorkSheet) End Sub Private Sub releaseObject(ByVal obj As Object) Try System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) obj = Nothing Catch ex As Exception obj = Nothing Finally GC.Collect() End Try End Sub End Class 

FORM2

 Imports System.Linq Imports System.Drawing Public Class Form2 Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint Dim label As New Label() Dim count As Integer = Panel1.Controls.OfType(Of Label)().ToList().Count label.Location = New Point(10, (25 * count)) 'label.Size = New Size(40, 20) label.Name = "label_" & (count + 1) label.Text = Foo '& (count + 1) label.AutoSize = True Panel1.Controls.Add(label) Dim button As New Button() button.Location = New System.Drawing.Point(250, 25 * count) button.Size = New System.Drawing.Size(60, 20) button.Name = "Print" & (count + 1) button.Text = "Print" '& (count + 1) AddHandler button.Click, AddressOf Button_Click Panel1.Controls.Add(button) MessageBox.Show(Foo) End Sub Private Sub Button_Click(sender As Object, e As EventArgs) Dim button As Button = TryCast(sender, Button) MessageBox.Show(button.Name + " clicked") End Sub End Class 

 Module Module1 Public Foo As String End Module 

如果我正确理解你的问题,那么事件处理程序Panel1_Paint被调用多次,以获得相同的Excel单元格值。 所以可以简单地通过检查是否已经存在具有特定名称的标签来解决,例如像这样:

 Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint For Each c As Control In Panel1.Controls If c Is Label Then If ((Label) c).Text = Foo Then ' Label alredy there, exit sub... End If End If Next