2008年9月9日 星期二
建立連動的DGV與ProgressBar
'------滑鼠點下去的瞬間,row會被ProgressBar取代掉,而ProgressBar會隨著DGV 的滾動而上下移動,看起來ProgressBar是『黏在』DGV的ROW上面
'----全域變數
Public Class Form1
Dim DS As New DataSet
Dim V_RowIndex As Integer
Dim V_pcb_name As String
'-----表單開始
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sDT, eDT As String
sDT = Date.Now.Date.ToString("yyyy/MM/dd")
eDT = Convert.ToDateTime(sDT).AddDays(1).ToString("yyyy/MM/dd")
Dim Ii, TimeStep, RoomStep As Integer
TimeStep = 1
RoomStep = 5
For Ii = 0 To 24 Step TimeStep
Dim V_col As New DataGridViewTextBoxColumn
DGV.Columns.Add(V_col)
DGV.Columns(Ii).Width = 30
DGV.Columns(Ii).HeaderText = Ii & ":00"
Next
Dim limingTable As DataTable = DS.Tables.Add("rm_sch_d")
'假裝成從資料庫撈資料
' 建立「Rm_no」欄位
limingTable.Columns.Add("Rm_no", System.Type.GetType("System.String"))
' 建立「arr_date」欄位
limingTable.Columns.Add("arr_time", System.Type.GetType("System.String"))
' 建立「dep_date」欄位
limingTable.Columns.Add("dep_time", System.Type.GetType("System.String"))
' 以下我們要將資料列新增至資料集 DS 當中的「rm_sch_d」資料表
Dim newRow As DataRow
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2201"
newRow("arr_time") = "06:30"
newRow("dep_time") = "14:13"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2202"
newRow("arr_time") = "12:15"
newRow("dep_time") = "16:05"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2203"
newRow("arr_time") = "09:23"
newRow("dep_time") = "12:00"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2204"
newRow("arr_time") = "10:13"
newRow("dep_time") = "14:30"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2205"
newRow("arr_time") = "11:30"
newRow("dep_time") = "20:30"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2206"
newRow("arr_time") = "12:59"
newRow("dep_time") = "20:30"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2207"
newRow("arr_time") = "01:11"
newRow("dep_time") = "23:30"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2208"
newRow("arr_time") = "02:30"
newRow("dep_time") = "20:30"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2209"
newRow("arr_time") = "07:30"
newRow("dep_time") = "20:30"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2210"
newRow("arr_time") = "11:30"
newRow("dep_time") = "11:45"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2211"
newRow("arr_time") = "01:30"
newRow("dep_time") = "23:30"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2212"
newRow("arr_time") = "11:30"
newRow("dep_time") = "20:30"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2213"
newRow("arr_time") = "03:30"
newRow("dep_time") = "20:30"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2214"
newRow("arr_time") = "19:30"
newRow("dep_time") = "20:30"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2215"
newRow("arr_time") = "05:30"
newRow("dep_time") = "20:30"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2216"
newRow("arr_time") = "19:30"
newRow("dep_time") = "20:00"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2217"
newRow("arr_time") = "11:30"
newRow("dep_time") = "12:30"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2218"
newRow("arr_time") = "01:30"
newRow("dep_time") = "23:30"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2219"
newRow("arr_time") = "11:30"
newRow("dep_time") = "23:30"
DS.Tables("rm_sch_d").Rows.Add(newRow)
newRow = DS.Tables("rm_sch_d").NewRow()
newRow("Rm_no") = "2220"
newRow("arr_time") = "02:22"
newRow("dep_time") = "04:30"
DS.Tables("rm_sch_d").Rows.Add(newRow) ' 接受所新增的資料列 limingTable.AcceptChanges()
For Ii = 0 To DS.Tables("rm_sch_d").Rows.Count - 1
DGV.Rows.Add()
DGV.Rows(Ii).HeaderCell.Value = DS.Tables("rm_sch_d").Rows(Ii).Item("Rm_no") Next
DGV.RowHeadersWidth = 60
'v_click = False
V_RowIndex = 1000
End Sub
Private Sub DGV_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellClick
Dim ii As Integer
Dim Gen_PCB As New PCB_bar
For ii = 0 To DGV.Rows.Count - 1
Try
Dim obj As Object = DGV.Controls.Find("PCB" & DGV.Rows(ii).HeaderCell.Value, False) Dim PCB As ProgressBar = CType(obj(0), ProgressBar)
PCB.Dispose()
Catch ex As Exception
End Try
Next
Gen_PCB.Gen_PCB(DGV, e.RowIndex - DGV.Rows.GetFirstRow(DataGridViewElementStates.Displayed), DS.Tables(0).Rows(e.RowIndex).Item("arr_time"), DS.Tables(0).Rows(e.RowIndex).Item("dep_time"), DS.Tables(0).Rows(e.RowIndex).Item("arr_time") & "~" & DS.Tables(0).Rows(e.RowIndex).Item("dep_time"))
V_pcb_name = "PCB" & DGV.Rows(e.RowIndex - DGV.Rows.GetFirstRow(DataGridViewElementStates.Displayed)).HeaderCell.Value
End Sub
Private Sub DGV_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DGV.Paint
Dim i As Integer
Dim Gen_Box As New Paint_bar
For i = DGV.Rows.GetFirstRow(DataGridViewElementStates.Displayed) To DGV.Rows.GetLastRow(DataGridViewElementStates.Displayed) - 1
If V_RowIndex <> i Then
Gen_Box.Gen_Box(DGV, i - DGV.Rows.GetFirstRow(DataGridViewElementStates.Displayed), DS.Tables(0).Rows(i).Item("Arr_time"), DS.Tables(0).Rows(i).Item("dep_time"), "", Brushes.Aqua, Brushes.Pink)
End If
Next
End Sub
Private Sub DGV_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DGV.Scroll
'MsgBox(e.NewValue)
'MsgBox(e.OldValue)
Dim ii As Integer
Try
Dim obj As Object = DGV.Controls.Find(V_pcb_name, False)
Dim PCB As ProgressBar = CType(obj(0), ProgressBar)
PCB.Location = New System.Drawing.Point(PCB.Location.X, PCB.Location.Y + (e.OldValue - e.NewValue) * DGV.Rows(0).Height)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
'----class
Public Class Paint_bar
Public Sub Gen_Box(ByVal DGV1 As DataGridView, ByVal R_index As Integer, ByVal Stime As DateTime, ByVal Etime As DateTime, ByVal TT_str As String, ByVal V_color As Brush, ByVal V_N_color As Brush)
Dim G As Graphics = DGV1.CreateGraphics
Dim Jj As Integer
Dim kk, Pp As DateTime
Dim Spoint_X, Spoint_y, Epoint_X, Epoint_Y As Integer
kk = Stime.ToString("HH:mm")
Pp = Etime.ToString("HH:mm")
Jj = DateDiff(DateInterval.Minute, kk, Pp)
Spoint_X = DGV1.RowHeadersWidth + DGV1.Columns(R_index).Width * Stime.ToString("HH") + (Stime.ToString("mm") / 60 * DGV1.Columns(R_index).Width)
Spoint_y = DGV1.Rows(R_index).Height * R_index + DGV1.ColumnHeadersHeight + 1 Epoint_X = Jj / 60 * DGV1.Columns(R_index).Width
Epoint_Y = DGV1.Rows(R_index).Height - 1
Dim rect As New Rectangle(Spoint_X, Spoint_y, Epoint_X, Epoint_Y)
G.FillRectangle(V_color, rect)
Dim fontb As New Font("新細明體", 8, FontStyle.Bold)
G.DrawString(Stime.ToString("HH:mm") & " ~ " & Etime.ToString("HH:mm"), fontb, V_N_color, Spoint_X, Spoint_y)
End Sub
End Class
Public Class PCB_bar
Public Sub Gen_PCB(ByVal DGV1 As DataGridView, ByVal R_index As Integer, ByVal Stime As DateTime, ByVal Etime As DateTime, ByVal TT_str As String)
Dim PCB As New ProgressBar
Dim Jj As Integer
Dim kk, Pp As DateTime
Dim tt As New ToolTip
Dim g As Graphics = PCB.CreateGraphics
Dim Spoint_X, Spoint_y, Epoint_X, Epoint_Y As Integer
PCB.Name = "PCB" & DGV1.Rows(R_index).HeaderCell.Value
Spoint_X = DGV1.RowHeadersWidth + DGV1.Columns(R_index).Width * Stime.ToString("HH") + (Stime.ToString("mm") / 60 * DGV1.Columns(R_index).Width)
Spoint_y = DGV1.Rows(R_index).Height * R_index + DGV1.ColumnHeadersHeight PCB.Location = New System.Drawing.Point(Spoint_X, Spoint_y)
kk = Stime.ToString("HH:mm")
Pp = Etime.ToString("HH:mm")
Jj = DateDiff(DateInterval.Minute, kk, Pp)
Epoint_X = Jj / 60 * DGV1.Columns(R_index).Width
Epoint_Y = DGV1.Rows(R_index).Height
PCB.Size = New System.Drawing.Size(Epoint_X, Epoint_Y)
PCB.Value = 100
PCB.Show()
tt.SetToolTip(PCB, TT_str)
DGV1.Controls.Add(PCB)
Dim fontb As New Font("新細明體", 14, FontStyle.Bold)
g.DrawString(TT_str, fontb, Brushes.DarkKhaki, 0, 0)
AddHandler PCB.Click, AddressOf PCB_click
End Sub
Public Sub PCB_click(ByVal sender As System.Object, ByVal e As System.EventArgs) MsgBox(sender.name)
Select Case sender.name
End Select
End Sub
End Class
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言