史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   [使用版本VB2008]使用DataGridView元件設計菜單 (http://forum.slime.com.tw/thread275370.html)

魔術王子 2014-01-24 10:20 PM

[使用版本VB2008]使用DataGridView元件設計菜單
 
https://imagizer.imageshack.us/v2/307x352q90/27/nldo.jpg
這次是MDI介面,所以需要2個Form
引用:

作者: Form1需要元件
Friend WithEvents StatusStrip1 As System.Windows.Forms.StatusStrip
Friend WithEvents MenuStrip1 As System.Windows.Forms.MenuStrip
Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
Friend WithEvents ToolStripMenuItem1 As System.Windows.Forms.ToolStripMenuItem
Friend WithEvents ToolStripMenuItem2 As System.Windows.Forms.ToolStripMenuItem

引用:

作者: Form1元件屬性
Private Sub InitializeComponent()
Me.StatusStrip1 = New System.Windows.Forms.StatusStrip
Me.MenuStrip1 = New System.Windows.Forms.MenuStrip
Me.TabControl1 = New System.Windows.Forms.TabControl
Me.TabPage1 = New System.Windows.Forms.TabPage
Me.TabPage2 = New System.Windows.Forms.TabPage
Me.ToolStripMenuItem1 = New System.Windows.Forms.ToolStripMenuItem
Me.ToolStripMenuItem2 = New System.Windows.Forms.ToolStripMenuItem
Me.MenuStrip1.SuspendLayout()
Me.TabControl1.SuspendLayout()
Me.SuspendLayout()
'
'StatusStrip1
'
Me.StatusStrip1.Location = New System.Drawing.Point(0, 292)
Me.StatusStrip1.Name = "StatusStrip1"
Me.StatusStrip1.Size = New System.Drawing.Size(291, 22)
Me.StatusStrip1.TabIndex = 1
Me.StatusStrip1.Text = "StatusStrip1"
'
'MenuStrip1
'
Me.MenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ToolStripMenuItem1})
Me.MenuStrip1.Location = New System.Drawing.Point(0, 0)
Me.MenuStrip1.Name = "MenuStrip1"
Me.MenuStrip1.Size = New System.Drawing.Size(291, 24)
Me.MenuStrip1.TabIndex = 3
Me.MenuStrip1.Text = "MenuStrip1"
'
'TabControl1
'
Me.TabControl1.Controls.Add(Me.TabPage1)
Me.TabControl1.Controls.Add(Me.TabPage2)
Me.TabControl1.Dock = System.Windows.Forms.DockStyle.Top
Me.TabControl1.Location = New System.Drawing.Point(0, 24)
Me.TabControl1.Name = "TabControl1"
Me.TabControl1.SelectedIndex = 0
Me.TabControl1.Size = New System.Drawing.Size(291, 21)
Me.TabControl1.TabIndex = 4
'
'TabPage1
'
Me.TabPage1.Location = New System.Drawing.Point(4, 22)
Me.TabPage1.Name = "TabPage1"
Me.TabPage1.Padding = New System.Windows.Forms.Padding(3)
Me.TabPage1.Size = New System.Drawing.Size(283, 0)
Me.TabPage1.TabIndex = 0
Me.TabPage1.Text = "菜單"
Me.TabPage1.UseVisualStyleBackColor = True
'
'TabPage2
'
Me.TabPage2.Location = New System.Drawing.Point(4, 22)
Me.TabPage2.Name = "TabPage2"
Me.TabPage2.Padding = New System.Windows.Forms.Padding(3)
Me.TabPage2.Size = New System.Drawing.Size(345, 0)
Me.TabPage2.TabIndex = 1
Me.TabPage2.Text = "新菜單*"
Me.TabPage2.UseVisualStyleBackColor = True
'
'ToolStripMenuItem1
'
Me.ToolStripMenuItem1.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ToolStripMenuItem2})
Me.ToolStripMenuItem1.Name = "ToolStripMenuItem1"
Me.ToolStripMenuItem1.Size = New System.Drawing.Size(44, 20)
Me.ToolStripMenuItem1.Text = "系統"
'
'ToolStripMenuItem2
'
Me.ToolStripMenuItem2.Name = "ToolStripMenuItem2"
Me.ToolStripMenuItem2.Size = New System.Drawing.Size(152, 22)
Me.ToolStripMenuItem2.Text = "離開"
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(291, 314)
Me.Controls.Add(Me.TabControl1)
Me.Controls.Add(Me.StatusStrip1)
Me.Controls.Add(Me.MenuStrip1)
Me.IsMdiContainer = True
Me.MainMenuStrip = Me.MenuStrip1
Me.MaximizeBox = False
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
Me.MenuStrip1.ResumeLayout(False)
Me.MenuStrip1.PerformLayout()
Me.TabControl1.ResumeLayout(False)
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub

引用:

作者: Form2需要元件
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents ToolStrip1 As System.Windows.Forms.ToolStrip
Friend WithEvents ToolStripLabel1 As System.Windows.Forms.ToolStripLabel
Friend WithEvents ToolStripComboBox1 As System.Windows.Forms.ToolStripComboBox
Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView
Friend WithEvents Column1 As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Column2 As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Column3 As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Column4 As System.Windows.Forms.DataGridViewTextBoxColumn

引用:

作者: Form2元件屬性
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.ToolStrip1 = New System.Windows.Forms.ToolStrip
Me.ToolStripLabel1 = New System.Windows.Forms.ToolStripLabel
Me.ToolStripComboBox1 = New System.Windows.Forms.ToolStripComboBox
Me.DataGridView1 = New System.Windows.Forms.DataGridView
Me.Column1 = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.Column2 = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.Column3 = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.Column4 = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.ToolStrip1.SuspendLayout()
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Dock = System.Windows.Forms.DockStyle.Bottom
Me.TextBox1.Location = New System.Drawing.Point(0, 292)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(296, 22)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = "0"
Me.TextBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
'
'ToolStrip1
'
Me.ToolStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ToolStripLabel1, Me.ToolStripComboBox1})
Me.ToolStrip1.Location = New System.Drawing.Point(0, 0)
Me.ToolStrip1.Name = "ToolStrip1"
Me.ToolStrip1.Size = New System.Drawing.Size(296, 25)
Me.ToolStrip1.TabIndex = 2
Me.ToolStrip1.Text = "ToolStrip1"
'
'ToolStripLabel1
'
Me.ToolStripLabel1.Name = "ToolStripLabel1"
Me.ToolStripLabel1.Size = New System.Drawing.Size(32, 22)
Me.ToolStripLabel1.Text = "號碼"
'
'ToolStripComboBox1
'
Me.ToolStripComboBox1.Items.AddRange(New Object() {"第1桌", "第2桌", "第3桌", "第4桌", "第5桌"})
Me.ToolStripComboBox1.Name = "ToolStripComboBox1"
Me.ToolStripComboBox1.Size = New System.Drawing.Size(121, 25)
'
'DataGridView1
'
Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.Column1, Me.Column2, Me.Column3, Me.Column4})
Me.DataGridView1.Dock = System.Windows.Forms.DockStyle.Fill
Me.DataGridView1.Location = New System.Drawing.Point(0, 25)
Me.DataGridView1.Name = "DataGridView1"
Me.DataGridView1.RowTemplate.Height = 24
Me.DataGridView1.Size = New System.Drawing.Size(296, 267)
Me.DataGridView1.TabIndex = 3
'
'Column1
'
Me.Column1.HeaderText = "名稱"
Me.Column1.Name = "Column1"
Me.Column1.Resizable = System.Windows.Forms.DataGridViewTriState.[True]
'
'Column2
'
Me.Column2.HeaderText = "價格"
Me.Column2.Name = "Column2"
Me.Column2.Width = 40
'
'Column3
'
Me.Column3.HeaderText = "數量"
Me.Column3.Name = "Column3"
Me.Column3.Width = 40
'
'Column4
'
Me.Column4.HeaderText = "小計"
Me.Column4.Name = "Column4"
Me.Column4.Width = 40
'
'Form2
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(296, 314)
Me.ControlBox = False
Me.Controls.Add(Me.DataGridView1)
Me.Controls.Add(Me.ToolStrip1)
Me.Controls.Add(Me.TextBox1)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form2"
Me.ShowIcon = False
Me.ShowInTaskbar = False
Me.Text = "菜單"
Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
Me.ToolStrip1.ResumeLayout(False)
Me.ToolStrip1.PerformLayout()
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub

因為元件屬性質設定有些複雜,所以只好將Designer內容完全貼出
引用:

作者: Form1程式碼
Public Class Form1

Dim PageNo As Single = 1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Cursor = Cursors.WaitCursor
Dim f2 As Form2
f2 = New Form2
f2.MdiParent = Me
f2.Show()
Me.Cursor = Cursors.Default
End Sub

Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedIndex + 1 = TabControl1.TabPages.Count Then
TabControl1.SelectedTab.Text = "菜單 - " + Str(PageNo)
TabControl1.SelectedTab.ImageIndex = 1
PageNo += 1
TabControl1.TabPages.Add("新菜單*")
TabControl1.TabPages(TabControl1.TabPages.Count - 1).ImageIndex = 2
Dim f2 As Form2
f2 = New Form2
f2.Text = TabControl1.SelectedTab.Text
f2.Tag = -1
f2.MdiParent = Me
f2.Show()
Else
Dim form As Form
For Each form In Me.MdiChildren
If form.Text = TabControl1.TabPages(TabControl1.SelectedIndex).Text Then form.Activate()
Next
End If
End Sub

Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
Close()
End Sub
End Class

引用:

作者: Form2程式碼
Public Class Form2

Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
Me.WindowState = FormWindowState.Maximized
End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
DataGridView1.Rows.Add("牛肉麵", "100", "0", "0")
DataGridView1.Rows.Add("炸醬麵", "60", "0", "0")
DataGridView1.Rows.Add("滷肉飯", "50", "0", "0")
DataGridView1.Rows.Add("水餃", "5", "0", "0")
DataGridView1.Rows.Add("貢丸湯", "30", "0", "0")
End Sub

Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
Dim i As Integer, n As Integer = 0
For i = 0 To DataGridView1.Rows.Count - 2
If Val(DataGridView1.Rows(i).Cells(1).Value) > 0 And Val(DataGridView1.Rows(i).Cells(2).Value) > 0 Then DataGridView1.Rows(i).Cells(3).Value = Val(DataGridView1.Rows(i).Cells(1).Value) * Val(DataGridView1.Rows(i).Cells(2).Value)
n = n + (Val(DataGridView1.Rows(i).Cells(1).Value) * Val(DataGridView1.Rows(i).Cells(2).Value))
Next
TextBox1.Text = Format(n, "NT$ #,###,###.#")
End Sub

Private Sub ToolStripComboBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripComboBox1.TextChanged
Dim i As Integer
For i = 0 To Form1.TabControl1.TabCount - 1
If Form1.TabControl1.TabPages(i).Text = Me.Text Then Form1.TabControl1.TabPages(i).Text = ToolStripComboBox1.Text : Me.Text = ToolStripComboBox1.Text : Exit For
Next
End Sub
End Class

會在Form1加入MenuStrip1的原因是為了讓Form2在Max狀態右上角不會出現三個系統按鈕
因為這是MDI的模式,所以在TabControl選擇時,其實是子視窗互換的效果


所有時間均為台北時間。現在的時間是 11:25 PM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2025, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1