我在运行时使用 Tabcontrol 创建的所有标签页无法显示第三个按钮,即使我的数据库中有它
All my tabpage create in runtime using Tabcontrol can't show 3rd button even i have it in my database
Why all my tabpage created during tuntime using Tabcontrol can't show my 3rd button even it is there in my database? It's like it being limted to show 2 button only but i checked every code in my program, nothing is limiting everythings is ".count - 1"
Imports System.Data.OleDb
Public Class Services
Dim con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = Cypher.mdb;"
Dim cnnOledb As New OleDbConnection
Dim cmdload As New OleDbCommand
Dim cmdInsert As New OleDbCommand
Public categoryName As String
Private Sub Services_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CenterToScreen()
cnnOledb.ConnectionString = con
cnnOledb.Open()
cmdload.CommandText = "Select * From [Services]"
cmdload.Connection = cnnOledb
Max()
Tab()
add_btn()
End Sub
Private Sub Max()
Dim cmdMax As New OleDbCommand
cmdMax.CommandText = "Select MAX(id) from Services"
cmdMax.Connection = cnnOledb
Try
Dim dr As OleDbDataReader = cmdMax.ExecuteReader()
If dr.Read = True Then
If dr(0).ToString = "" Then
txtID.Text = "1"
Else
txtID.Text = dr(0) + 1.ToString
End If
End If
Catch
MsgBox("Error !")
End Try
End Sub
Private Sub Tab()
cobCategory.Items.Clear()
TabCategory.TabPages.Clear()
Dim cmdCategory As New OleDbCommand
cmdCategory.CommandText = "Select * from Category"
cmdCategory.Connection = cnnOledb
Dim dt As New DataTable
dt.Load(cmdCategory.ExecuteReader)
For i As Integer = 0 To dt.Rows.Count - 1
Dim tab As New TabPage()
tab.Name = "tab" + dt.Rows(i).Item(1).ToString
tab.Text = tab.Name.Remove(0, 3)
cobCategory.Items.Add(dt.Rows(i).Item(1).ToString)
TabCategory.TabPages.Add(tab)
Next
TabCategory.TabPages.Add("+")
End Sub
Private Sub add_btn()
Dim dt As New DataTable
dt.Load(cmdload.ExecuteReader)
Dim myFont As System.Drawing.Font
myFont = New System.Drawing.Font("Century Gothic", 12)
For i As Integer = 0 To dt.Rows.Count - 1
Dim btn As New Button()
btn.Name = "btn" + dt.Rows(i).Item(2).ToString
btn.Text = btn.Name.Remove(0, 3)
btn.Height = 30
btn.Width = 90
btn.Location = New Point(0, 10)
If TabCategory.TabPages((dt.Rows(i).Item(1)) - 1).Controls.count <> 0 Then
btn.Left += 100
End If
btn.Font = myFont
TabCategory.TabPages((dt.Rows(i).Item(1)) - 1).Controls.Add(btn)
Next
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Try
cmdInsert.CommandText = "Insert into [Services] values(" & txtID.Text & ", " & cobCategory.SelectedIndex + 1 & ", '" & txtServices.Text & "', " & txtDur.Text & ", " & txtPrices.Text & ");"
cmdInsert.CommandType = CommandType.Text
cmdInsert.Connection = cnnOledb
cmdInsert.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Fail to insert data")
End Try
End Sub
Private Sub TabCategory_Click(sender As Object, e As EventArgs) Handles TabCategory.Click
If TabCategory.SelectedTab Is TabCategory.TabPages(TabCategory.TabPages.Count - 1) Then
New_Category.ShowDialog()
End If
End Sub
Public Sub New_Cate()
Dim cmdMax As New OleDbCommand
cmdMax.CommandText = "Select MAX(ID) from Category"
cmdMax.Connection = cnnOledb
Dim ID As Integer
Try
Dim dr As OleDbDataReader = cmdMax.ExecuteReader()
If dr.Read = True Then
If dr(0).ToString = "" Then
ID = 1
Else
ID = dr(0) + 1
End If
End If
Catch
MsgBox("Error !")
End Try
Dim cmdAddCategory As New OleDbCommand
Try
cmdAddCategory.CommandText = "Insert into [Category] values(" & ID & ", '" & categoryName & "');"
cmdAddCategory.CommandType = CommandType.Text
cmdAddCategory.Connection = cnnOledb
cmdAddCategory.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Fail to insert new category")
End Try
Tab()
End Sub
End Class
按钮在那里,在第二个按钮的后面。您的代码最初将按钮放置在 x = 0 处,然后当 Tabpage.controls.count > 0 时,它会添加 100。因此第一个按钮之后的每个按钮都位于 x = 100.
改变这个:
btn.Location = New Point(0, 10)
If TabCategory.TabPages((dt.Rows(i).Item(1)) - 1).Controls.count <> 0 Then
btn.Left += 100
End If
最后一个按钮右边+100:
btn.Location = New Point(0, 10)
If TabCategory.TabPages((dt.Rows(i).Item(1)) - 1).Controls.count <> 0 Then
btn.Left += TabCategory.TabPages((dt.Rows(i).Item(1)) - 1).Controls(TabCategory.TabPages((dt.Rows(i).Item(1)) - 1).Controls.Count - 1).Left + 100
End If
Why all my tabpage created during tuntime using Tabcontrol can't show my 3rd button even it is there in my database? It's like it being limted to show 2 button only but i checked every code in my program, nothing is limiting everythings is ".count - 1"
Imports System.Data.OleDb
Public Class Services
Dim con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = Cypher.mdb;"
Dim cnnOledb As New OleDbConnection
Dim cmdload As New OleDbCommand
Dim cmdInsert As New OleDbCommand
Public categoryName As String
Private Sub Services_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CenterToScreen()
cnnOledb.ConnectionString = con
cnnOledb.Open()
cmdload.CommandText = "Select * From [Services]"
cmdload.Connection = cnnOledb
Max()
Tab()
add_btn()
End Sub
Private Sub Max()
Dim cmdMax As New OleDbCommand
cmdMax.CommandText = "Select MAX(id) from Services"
cmdMax.Connection = cnnOledb
Try
Dim dr As OleDbDataReader = cmdMax.ExecuteReader()
If dr.Read = True Then
If dr(0).ToString = "" Then
txtID.Text = "1"
Else
txtID.Text = dr(0) + 1.ToString
End If
End If
Catch
MsgBox("Error !")
End Try
End Sub
Private Sub Tab()
cobCategory.Items.Clear()
TabCategory.TabPages.Clear()
Dim cmdCategory As New OleDbCommand
cmdCategory.CommandText = "Select * from Category"
cmdCategory.Connection = cnnOledb
Dim dt As New DataTable
dt.Load(cmdCategory.ExecuteReader)
For i As Integer = 0 To dt.Rows.Count - 1
Dim tab As New TabPage()
tab.Name = "tab" + dt.Rows(i).Item(1).ToString
tab.Text = tab.Name.Remove(0, 3)
cobCategory.Items.Add(dt.Rows(i).Item(1).ToString)
TabCategory.TabPages.Add(tab)
Next
TabCategory.TabPages.Add("+")
End Sub
Private Sub add_btn()
Dim dt As New DataTable
dt.Load(cmdload.ExecuteReader)
Dim myFont As System.Drawing.Font
myFont = New System.Drawing.Font("Century Gothic", 12)
For i As Integer = 0 To dt.Rows.Count - 1
Dim btn As New Button()
btn.Name = "btn" + dt.Rows(i).Item(2).ToString
btn.Text = btn.Name.Remove(0, 3)
btn.Height = 30
btn.Width = 90
btn.Location = New Point(0, 10)
If TabCategory.TabPages((dt.Rows(i).Item(1)) - 1).Controls.count <> 0 Then
btn.Left += 100
End If
btn.Font = myFont
TabCategory.TabPages((dt.Rows(i).Item(1)) - 1).Controls.Add(btn)
Next
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Try
cmdInsert.CommandText = "Insert into [Services] values(" & txtID.Text & ", " & cobCategory.SelectedIndex + 1 & ", '" & txtServices.Text & "', " & txtDur.Text & ", " & txtPrices.Text & ");"
cmdInsert.CommandType = CommandType.Text
cmdInsert.Connection = cnnOledb
cmdInsert.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Fail to insert data")
End Try
End Sub
Private Sub TabCategory_Click(sender As Object, e As EventArgs) Handles TabCategory.Click
If TabCategory.SelectedTab Is TabCategory.TabPages(TabCategory.TabPages.Count - 1) Then
New_Category.ShowDialog()
End If
End Sub
Public Sub New_Cate()
Dim cmdMax As New OleDbCommand
cmdMax.CommandText = "Select MAX(ID) from Category"
cmdMax.Connection = cnnOledb
Dim ID As Integer
Try
Dim dr As OleDbDataReader = cmdMax.ExecuteReader()
If dr.Read = True Then
If dr(0).ToString = "" Then
ID = 1
Else
ID = dr(0) + 1
End If
End If
Catch
MsgBox("Error !")
End Try
Dim cmdAddCategory As New OleDbCommand
Try
cmdAddCategory.CommandText = "Insert into [Category] values(" & ID & ", '" & categoryName & "');"
cmdAddCategory.CommandType = CommandType.Text
cmdAddCategory.Connection = cnnOledb
cmdAddCategory.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Fail to insert new category")
End Try
Tab()
End Sub
End Class
按钮在那里,在第二个按钮的后面。您的代码最初将按钮放置在 x = 0 处,然后当 Tabpage.controls.count > 0 时,它会添加 100。因此第一个按钮之后的每个按钮都位于 x = 100.
改变这个:
btn.Location = New Point(0, 10)
If TabCategory.TabPages((dt.Rows(i).Item(1)) - 1).Controls.count <> 0 Then
btn.Left += 100
End If
最后一个按钮右边+100:
btn.Location = New Point(0, 10)
If TabCategory.TabPages((dt.Rows(i).Item(1)) - 1).Controls.count <> 0 Then
btn.Left += TabCategory.TabPages((dt.Rows(i).Item(1)) - 1).Controls(TabCategory.TabPages((dt.Rows(i).Item(1)) - 1).Controls.Count - 1).Left + 100
End If