2009-08-09 2 views
0

이 작업을 수행하기 위해 수행해야 할 작업을 정확히 모르기 때문에 내 설명이 부족할 수 있습니다. 본질적으로 나는로드 할 때마다 스스로를 재현하는 프로그램 실행 프로그램을 작성하고 있습니다. 그것은 SQLite 데이터베이스에서 탭과 버튼에 관한 데이터를 가져오고 런타임에 동적으로 빌드합니다. 버튼을 만드는 함수에 탭 이름을 전달하면 문제가 발생합니다. 데이터베이스에서 올바른 단추 집합을 가져 오려면 이름이 필요합니다. 그런 다음 이름을 사용하여 오른쪽 탭에 단추를 배치하려고했지만 디버거에서는 올바르게 가리 키지 않기 때문에 null 참조라고합니다. (적어도 내가 추측하고있는 것이 그것을 가리키고 자 노력하고있는) tabpage에. 이 작업을 올바르게 수행하는 방법에 대한 아이디어가 있습니까?vb.net은 이전에 생성 된 개체 인스턴스로 문자열을 사용합니다.

Private Sub CreateTabs() 
    Dim SQLconnect As New SQLite.SQLiteConnection() 
    Dim SQLcommand As SQLiteCommand 
    SQLconnect.ConnectionString = "Data Source=" & sPath & "\dock.db;" 
    SQLconnect.Open() 
    SQLcommand = SQLconnect.CreateCommand 
    SQLcommand.CommandText = "SELECT title FROM tabs" 
    Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader() 
    Dim Tabs(25) As String 
    Dim c As Integer = 1 
    While SQLreader.Read() 
     Tabs(c) = SQLreader(0) 
     c = c + 1 
    End While 
    SQLcommand.Dispose() 
    SQLconnect.Close() 
    For i = 1 To UBound(Tabs) 
     If Tabs(i) <> "" Then 
      Launcher.TabPages.Add(Tabs(i)) 
      CreateButtons(Tabs(i)) 
     End If 
    Next 
End Sub 

Private Sub CreateButtons(ByVal tab) 
    Dim SQLconnect As New SQLite.SQLiteConnection() 
    Dim SQLcommand As SQLiteCommand 
    SQLconnect.ConnectionString = "Data Source=" & sPath & "\dock.db;" 
    SQLconnect.Open() 
    SQLcommand = SQLconnect.CreateCommand 
    SQLcommand.CommandText = "SELECT id,name,path FROM buttons WHERE tab = '" & tab & "'" 
    Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader() 
    While SQLreader.Read() 
     For i = 1 To 9 
      Dim NewButton(i) As Button 
      If Not SQLreader(2) Is System.DBNull.Value Then 
       Dim myIcon As System.Drawing.Icon = Icon.ExtractAssociatedIcon(SQLreader(2)) 
      End If 
      Dim toolTip1 As ToolTip = New System.Windows.Forms.ToolTip(Me.components) 
      Me.Controls(tab).tabpages.add(NewButton(i)) '<--this causes my problem 
      'NewButton(i).Width = 32 
      'NewButton(i).Height = 32 
      'NewButton(i).Text = i 
      'NewButton(i).Image = myIcon.ToBitmap 
      'If Not SQLreader(1) Is System.DBNull.Value Then 
      'toolTip1.SetToolTip(NewButton(i), SQLreader(1)) 
      'toolTip1.Active = True 
      'End If 
     Next 
    End While 
    SQLcommand.Dispose() 
    SQLconnect.Close() 
End Sub 

답변

0

도움 주셔서 감사하지만 내 대답을 발견했습니다. 외관상으로는 탭의 인덱스를 다음과 같이 참조 할 필요가 있습니다.

Private Sub CreateTabs() 
    Dim SQLconnect As New SQLite.SQLiteConnection() 
    Dim SQLcommand As SQLiteCommand 
    SQLconnect.ConnectionString = "Data Source=" & sPath & "\dock.db;" 
    SQLconnect.Open() 
    SQLcommand = SQLconnect.CreateCommand 
    SQLcommand.CommandText = "SELECT title FROM tabs" 
    Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader() 
    Dim Tabs(25) As String 
    Dim c As Integer = 1 
    While SQLreader.Read() 
     Tabs(c) = SQLreader(0) 
     c = c + 1 
    End While 
    SQLcommand.Dispose() 
    SQLconnect.Close() 
    For i = 1 To UBound(Tabs) 
     If Tabs(i) <> "" Then 
      Launcher.TabPages.Add(Tabs(i)) 
      CreateButtons(Tabs(i), i - 1) 
     End If 
    Next 
End Sub 

Private Sub CreateButtons(ByVal tab, ByVal TabIndex) 
    Dim SQLconnect As New SQLite.SQLiteConnection() 
    Dim SQLcommand As SQLiteCommand 
    SQLconnect.ConnectionString = "Data Source=" & sPath & "\dock.db;" 
    SQLconnect.Open() 
    SQLcommand = SQLconnect.CreateCommand 
    SQLcommand.CommandText = "SELECT id,name,path FROM buttons WHERE tab = '" & tab & "'" 
    Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader() 
    While SQLreader.Read() 
     For i = 1 To 9 
      Dim NewButton As New Button 
      Launcher.TabPages.Item(TabIndex).Controls.add(NewButton) 
      NewButton.Width = 32 
      NewButton.Height = 32 
      NewButton.Location = New Point(10 + (SQLreader(0) * 32) + 10, 10) 
      NewButton.Text = SQLreader(0) 
      If Not SQLreader(2) Is System.DBNull.Value Then 
       Dim toolTip1 As ToolTip = New System.Windows.Forms.ToolTip(Me.components) 
       Dim myIcon As System.Drawing.Icon = Icon.ExtractAssociatedIcon(SQLreader(2)) 
       NewButton.Image = myIcon.ToBitmap 
       toolTip1.SetToolTip(NewButton, SQLreader(1)) 
       toolTip1.Active = True 
      End If 
     Next 
    End While 
    SQLcommand.Dispose() 
    SQLconnect.Close() 
End Sub 
0

나는 여기에 두 가지 문제를 가지고 생각 :

  • 당신은 탭 이름으로 탭 컨트롤을 찾기 위해 노력하고 있습니다. 대신 탭 컨트롤 자체를 찾거나 그 컨트롤을 전달하여 컨트롤이 안에있는 탭의 이름을 찾아야합니다. 그렇지 않으면 특정 탭 대신에 탭 페이지 모음에 단추를 추가하려고합니다.

  • 너는 실제로는 새 단추를 만듭니다.. 루프의 모든 반복에 대해 배열 개의 버튼을 만들지 만 새로운 실제 Button 객체는 만들지 않습니다.

내가 의심 당신이 (tabControlTabControl이 전달입니다) 루프로이 원하는 ... 틀림이 탭 페이지에 의한 있다는 당신의 의심과 비웃음하지 않는,하지만 확실히 문제입니다 :

While SQLreader.Read() 
    For i = 1 To 9 
     If Not SQLreader(2) Is System.DBNull.Value Then 
      Dim myIcon As Icon = Icon.ExtractAssociatedIcon(SQLreader(2)) 
     End If 
     Dim toolTip1 As ToolTip = New ToolTip(Me.components) 
     Dim NewButton As Button = New Button 
     NewButton.Width = 32 
     NewButton.Height = 32 
     NewButton.Text = i 
     NewButton.Image = myIcon.ToBitmap 
     tabControl.TabPages(tab).Controls.Add(NewButton) 
     If Not SQLreader(1) Is System.DBNull.Value Then 
      toolTip1.SetToolTip(NewButton, SQLreader(1)) 
      toolTip1.Active = True 
     End If 
    Next 
End While 

는 희망 맞아 - 내 VB.NET이되지 중대하다 ...

내가 원래 코드는하지만 컴파일 놀랐어요 - 당신이 Option Strict을해야합니까?

명령과 연결에 대해서도 Using 문을 사용해야 예외가 발생할 경우에도 처리됩니다.

+0

컴파일되지 않았지만 여전히 변경되지 않았습니다. 디버그 모드에서 "Object variable 또는 With 블록 변수가 설정되지 않았습니다."와 같은 오류가 발생합니다. 같은 줄을 기준으로 – MaQleod

+0

컴파일되지 않았다면 디버거에서 어떻게 볼 수 있습니까? 실행하지 않고는 디버그 할 수 없으며 컴파일하지 않고는 실행할 수 없습니다. 내 변경 사항으로'tabControl'으로 무엇을 전달합니까? –

+0

난 그냥 디버깅을 시작, 그리고 그것을 시도하고 컴파일 오류를 제공합니다. 지금 두 개의 인수를 전달하고 있습니다. tabcontrol은 탭 컨트롤 이름이고 tab은 오른쪽 단추를 선택하기 위해 SQL 문을 올바르게 한정 할 수 있도록 탭 페이지 텍스트입니다. 오류가 발생합니다 : "개체 변수 또는 With 블록 변수가 설정되지 않았습니다." 탭에 새 버튼을 추가하려고하는 행에서 tabControl.tabpages (tab) .add (NewButton) – MaQleod

관련 문제