2013-08-05 87 views
-2

我發佈這個後,通過搜索失去我的時間&沒有得到正確的答案。Visual Basic 2010 - 動態標籤,控件和記錄數據

我的要求是創建一個許可證制度。 當我點擊「新許可證」按鈕(位於tabcontrol上面的表單的頂部)時,表單&上有一個選項卡控件,應該打開一個新的選項卡(名爲「新許可證」),其上帶有文本框。 每次點擊「新許可證」按鈕,同時打開相同類型的選項卡。 當我們在文本字段中選擇一個選項卡&填充&點擊「發行許可證」按鈕(位於上方的TabControl形式的頂部),標籤應同時有專門的許可證文本字段中的數據保存到Access數據庫關閉數。

我創建的形式&「新證」,「發行許可證」按鈕& TabControl的是在用戶控件「ucPermit」。 我很驚訝創建這些動態標籤&將這些數據獲取到數據庫中。

'' '' ''' '' 附件 '' '' '' '' '' '' '' ''' '' '' '' '' ''' '' ''」 '''

@DonA

我在窗體上有兩個面板。 面板01(左側)有按鈕「摘要,許可證,LOTO,HotWork」。 當每個按鈕被點擊時,相應的用戶控件(在運行時加載到右側的面板02上)出現在前面。

Public Class Home 
    Dim ucSummary As New ucSummary 
    Dim ucPermitMain As New ucPermitMain 
    Dim ucLotoMain As New ucLotoMain 
    Dim ucHotWorkMain As New ucHotWorkMain 

Private Sub Home_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    lblDate.Text = Format(Now, "MMMM dd, yyyy") 
    lblTime.Text = Format(Now, "h:mm tt") 
    btnSummary.BackColor = Color.LightGreen 
    Panel2.Controls.Add(ucSummary) 
    ucSummary.Dock = DockStyle.Fill 
    Panel2.Controls.Add(ucPermitMain) 
    ucPermitMain.Dock = DockStyle.Fill 
    Panel2.Controls.Add(ucLotoMain) 
    ucLotoMain.Dock = DockStyle.Fill 
    Panel2.Controls.Add(ucHotWorkMain) 
    ucHotWorkMain.Dock = DockStyle.Fill 
End Sub 

Private Sub btnSummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSummary.Click 
    btnSummary.BackColor = Color.LightGreen 
    btnPermit.BackColor = Color.LightBlue 
    btnLoto.BackColor = Color.LightBlue 
    btnHotWork.BackColor = Color.LightBlue 
    ucSummary.BringToFront() 
End Sub 

Private Sub btnPermit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPermit.Click 
    btnSummary.BackColor = Color.LightBlue 
    btnPermit.BackColor = Color.LightGreen 
    btnLoto.BackColor = Color.LightBlue 
    btnHotWork.BackColor = Color.LightBlue 
    ucPermitMain.BringToFront() 
End Sub 

Private Sub btnLoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoto.Click 
    btnSummary.BackColor = Color.LightBlue 
    btnPermit.BackColor = Color.LightBlue 
    btnLoto.BackColor = Color.LightGreen 
    btnHotWork.BackColor = Color.LightBlue 
    ucLotoMain.BringToFront() 
End Sub 

Private Sub btnHotWork_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHotWork.Click 
    btnSummary.BackColor = Color.LightBlue 
    btnPermit.BackColor = Color.LightBlue 
    btnLoto.BackColor = Color.LightBlue 
    btnHotWork.BackColor = Color.LightGreen 
    ucHotWorkMain.BringToFront() 
End Sub 
End Class 

我剛開始&目前的工作許可證部分與ucPermitMain用戶控件的作品。 它有一個工具條(tsPermit)在頂部&下面是一個空的tabcontrol(tcPermit)。 Toolstrip有按鈕「NewPermit,問題,關閉」(現在..更多與設計) 每次NewPermit被點擊新的標籤與其上的文本框並行打開。 當選擇一個特定的選項卡&單擊「發佈」時,選定選項卡的文本框中的數據將被放入數據庫「PTW.accdb」,&選項卡以關閉。

Public Class ucPermitMain 
    Dim PTW As New OleDb.OleDbConnection 
    Dim ucPermit As New ucPermit 
    Dim txtbox As New TextBox 

Public Sub New() 
    ' This call is required by the designer. 
    InitializeComponent() 
    ' Add any initialization after the InitializeComponent() call. 
    PTW = New OleDb.OleDbConnection 
    PTW.ConnectionString = "Provider=Microsoft.ACE.Oledb.12.0; Data Source=" & Application.StartupPath & "\PTW.accdb" 
End Sub 

Public Sub tsbtnNewPermit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnNewPermit.Click 
    tsbtnClose.Visible = True 
    tsbtnClose.Enabled = True 

    Dim CTP As New CustomTabPage("New PTW", New Object) 
    Me.tcPermit.TabPages.Add(CTP) 
    tcPermit.SelectTab(tcPermit.TabPages.Count - 1) 
End Sub 

Public Sub tsbtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnClose.Click 
    tcPermit.TabPages.RemoveAt(tcPermit.SelectedIndex) 
    'This bottom code is telling it to remove the selected tab and countdown minus (-) 1 (one) 
    If tcPermit.TabPages.Count > 0 Then 
     tcPermit.SelectTab(tcPermit.SelectedIndex) 
    Else 
     tsbtnClose.Visible = False 
     tsbtnClose.Enabled = False 
    End If 
End Sub 

Public Sub tsbtnIssue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnIssue.Click 
    Dim cmd As New OleDb.OleDbCommand 
    Dim CTB As CustomTextBox 
    If Not PTW.State = ConnectionState.Open Then 
     'open connection if it is not yet open 
     PTW.Open() 
    End If 

    cmd.Connection = PTW 
    cmd.CommandText = "INSERT INTO tblPermit(ptwNo)" & _ 
         "VALUES('" & ?????????????? & "')" 
    cmd.ExecuteNonQuery() 
End Sub 
End Class 

CustomTextBox & CustomTabPage是兩個不同的類來創建自定義這些控件。

Imports System.Windows.Forms 
Public Class CustomTabPage 
Inherits TabPage 

Public Sub New(ByVal Name As String, ByVal NewConstruct As Object) 
    InitTextBox(1) 
    Me.Text = Name 
End Sub 

Private Sub InitTextBox(ByVal Num As Integer) 
    Dim LocX As Integer = 10 
    Dim LocY As Integer = 10 
    Dim CTB As New CustomTextBox("") 
    CTB.Location = New System.Drawing.Point(LocX, LocY) 
    Me.Controls.Add(CTB) 
End Sub 
End Class 

Imports System.Windows.Forms 
Public Class CustomTextBox 
Inherits TextBox 

Public Sub New(ByVal Name As String) 
    Me.Text = Name 
End Sub 
End Class 

這是唯一的代碼,我現在高達它們&可能不是最優化的實現。 希望你會花時間去完成。

回答

0

這裏的教訓是有Data Layer,這一層的所有數據保存到數據庫的方法和邏輯。當新標籤生成時,您有一個屬性允許它訪問Data Layer。它可能甚至需要一些封裝(UserControl)與一組控制相同的邏輯來保存/驗證數據。

您還應該顯示一些代碼 - 在尋求幫助之前,您可以付出代價。

+0

我用我的代碼編輯了我的問題。看看隊友。 – uboy

+0

我做到了 - 我覺得我的聲明依然存在。每個許可證都應該是UserControl,並通過調用「DAL」 - 數據訪問層來獲取和設置數據。現在,這可能稍高於你的頭,但如果你堅持並繼續努力,你就會做到。 – OneFineDay

+0

Mhhh ...你能給我一個開始與DAL? – uboy