2011-10-18 125 views
0

我有一個類叫做class1,在那個類中我不想讓Form1的文本框中寫入東西,但它不會讓我訪問Form1.TextBox1.text,另一個問題是它不會給我在Excel中的保存對話框,但那是另一個問題。不能從另一個類訪問form1

我如何訪問Form1控件?

Form1的代碼

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Call ThisDrawing.GetFilepath(True) 
    If Me.TextBox1.Text = "" Or Me.TextBox1.Text = "False" Then MsgBox("Er is geen geldige filenaam opgegeven") 
End Sub 

Class1的代碼

Public Shared Sub GetFilepath(ByVal hide As Boolean) 
    Dim Xl As New Excel.Application 
    Dim Filepath As String 
    Call ExcelKoppelen("Z:\test\test.xls") 
    Filepath = Xl.GetSaveAsFilename("", "Excel file", "*.xls") 
    Form1.TextBox1.text = Filepath 

End Sub 

Excelkoppelen代碼

Shared Sub ExcelKoppelen(ByVal Bestand As String) 
    Dim Xl As Excel.Application 
    Dim Filepath As String 
    Dim Workbook As Excel.Workbook 
    Dim Worksheet1 As Worksheet 
    Dim Worksheet2 As Worksheet 
    On Error Resume Next 
    Xl = GetObject(, "Excel.application") 
    If Err.Number Then 
     Information.Err.Clear() 
     Xl = CreateObject("Excel.application") 
    End If 
    Xl.Visible = True 
    Xl.ScreenUpdating = True 
    Workbook = Xl.Workbooks.Open(Bestand) 
    If Err.Number Then 
     Workbook = Xl.Workbooks.Open("Z:\test\test.xls") 
     Information.Err.Clear() 
     Workbook.SaveAs(Bestand) 
    End If 

    If Err.Number Then Exit Sub 
    On Error GoTo 0 

    Worksheet1 = Workbook.Worksheets.Item(1) 
    Worksheet2 = Workbook.Worksheets.Item(2) 
    Xl.Visible = False 
End Sub 

回答

1

您無法訪問到Form1一樣,直接的控制,如下創建爲Form對象並通過使用該對象來獲取控件。

Dim F as New Form1 
F.TextBox1.text = Filepath 
+0

謝謝,現在完美的作品,除了excel部分。 – PandaNL