ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 361|ตอบกลับ: 0

[VB.NET] การประยุกต์ใช้งาน User Control เพื่อแสดงผลปุ่มรายการสินค้า

[คัดลอกลิงก์]

319

กระทู้

511

โพสต์

6400

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6400
















ต่อจากตอนที่แล้วใน การสร้าง User Control แบบพื้นฐานขึ้นมาใช้งานเอง คราวนี้จะนำมาใช้งานจริงๆกันล่ะครับ (ให้ดูวิธีการสร้าง User Control ได้จาก Youtube)


มาดูโค้ดในส่วนของ Class Widget หรือการสร้างคุณสมบัติ (Properties) ให้กับ User Control ...
  1. Public Class Widget
  2.     '// เพิ่ม Properties สำหรับตั้งค่ารายการต่างๆ
  3.     '// ชื่อสินค้า
  4.     Public Property WidgetName As String
  5.         Get
  6.             Return lblWidgetName.Text
  7.         End Get
  8.         Set(value As String)
  9.             lblWidgetName.Text = value
  10.         End Set
  11.     End Property

  12.     '// สีฟอนต์ Label ชื่อสินค้า
  13.     Public Property WidgetNameForeColor As Color
  14.         Get
  15.             Return lblWidgetName.ForeColor
  16.         End Get
  17.         Set(value As Color)
  18.             lblWidgetName.ForeColor = value
  19.         End Set
  20.     End Property

  21.     '// สีแบ็คกราวด์ Label ชื่อสินค้า
  22.     Public Property WidgetNameBackColor As Color
  23.         Get
  24.             Return lblWidgetName.BackColor
  25.         End Get
  26.         Set(value As Color)
  27.             lblWidgetName.BackColor = value
  28.         End Set
  29.     End Property

  30.     '// Label ราคาสินค้า
  31.     Public Property WidgetPrice As String
  32.         Get
  33.             Return lblWidgetPrice.Text
  34.         End Get
  35.         Set(value As String)
  36.             lblWidgetPrice.Text = value
  37.         End Set
  38.     End Property

  39.     '// สีฟอนต์ Label ราคาสินค้า
  40.     Public Property WidgetPriceForeColor As Color
  41.         Get
  42.             Return lblWidgetPrice.ForeColor
  43.         End Get
  44.         Set(value As Color)
  45.             lblWidgetPrice.ForeColor = value
  46.         End Set
  47.     End Property

  48.     '// สีแบ็คกราวด์ Label ราคาสินค้า
  49.     Public Property WidgetPriceBackColor As Color
  50.         Get
  51.             Return lblWidgetPrice.BackColor
  52.         End Get
  53.         Set(value As Color)
  54.             lblWidgetPrice.BackColor = value
  55.         End Set
  56.     End Property

  57.     '// ตัวอักษรปุ่มคำสั่ง
  58.     Public Property WidgetButtonText As String
  59.         Get
  60.             Return btnWidgetName.Text
  61.         End Get
  62.         Set(value As String)
  63.             btnWidgetName.Text = value
  64.         End Set
  65.     End Property

  66.     '// แสดงผลรูปภาพบนปุ่มคำสั่ง (Button)
  67.     Public Property WidgetButtonImage As Image
  68.         Get
  69.             Return btnWidgetName.BackgroundImage
  70.         End Get
  71.         Set(value As Image)
  72.             btnWidgetName.BackgroundImage = value
  73.             btnWidgetName.BackgroundImageLayout = ImageLayout.Stretch
  74.         End Set
  75.     End Property

  76. End Class
คัดลอกไปที่คลิปบอร์ด


มาดูโค้ดฉบับเต็มกันเถอะ ...
  1. Public Class frmFoodDrinkUserControl

  2.     '// S T A R T - H E R E //
  3.     Private Sub frmFoodDrinkUserControl_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  4.         Call CreateUserControl()
  5.     End Sub

  6.     Sub CreateUserControl()
  7.         '// ข้อมูลรายการสินค้า (ID, ชื่อสินค้า, ราคาสินค้า, ชื่อไฟล์รูปภาพ)
  8.         Dim products As List(Of Tuple(Of String, String, String, String)) = New List(Of Tuple(Of String, String, String, String)) From {
  9.             Tuple.Create("01", "กาแฟร้อน", "50.00", "Coffee.jpg"),
  10.             Tuple.Create("02", "กาแฟเย็น", "60.00", "Coffee4.png"),
  11.             Tuple.Create("03", "คาปูชิโน่", "75.00", "Cappuccino.jpg"),
  12.             Tuple.Create("04", "คาปูชิโน่ - ลาเต้", "80.00", "CappuccinoLatte.jpg"),
  13.             Tuple.Create("05", "เอ็กซ์เพรสโซ่", "90.00", "Expresso.jpg"),
  14.             Tuple.Create("11", "Classic Chicken", "20.00", "BurgerChicken.png"),
  15.             Tuple.Create("12", "Mexicana", "25.00", "BurgerMexicana.png"),
  16.             Tuple.Create("13", "Lemon Shrimp", "30.00", "BurgerLemonShrimp.png"),
  17.             Tuple.Create("14", "Bacon", "40.00", "BurgerBacon.png"),
  18.             Tuple.Create("15", "Spicy Shrimp", "45.00", "BurgerSpicyShrimp.png"),
  19.             Tuple.Create("16", "Tex Supreme", "50.00", "BurgerTexSupreme.png"),
  20.             Tuple.Create("17", "Fish", "55.00", "BurgerFish.png"),
  21.             Tuple.Create("18", "ส้มตำเบอเกอร์", "155.00", "SomtumBurger.png"),
  22.             Tuple.Create("21", "Pepsi Can", "20.00", "PepsiCan.png"),
  23.             Tuple.Create("22", "Coke Can", "20.00", "CokeCan.png"),
  24.             Tuple.Create("23", "7Up Can", "20.00", "7upCan.png"),
  25.             Tuple.Create("24", "Pepsi 2 ลิตร", "50.00", "Pepsi2L.jpg"),
  26.             Tuple.Create("25", "Coke 2 ลิตร", "50.00", "Coke2L.jpg"),
  27.             Tuple.Create("26", "น้ำเปล่า", "15.00", "Water.jpg"),
  28.             Tuple.Create("41", "วิสกี้ (เพียว)", "100.00", "Whisky.jpg"),
  29.             Tuple.Create("42", "เหล้าขาว (เป๊ก)", "40.00", "Pek.png"),
  30.             Tuple.Create("43", "ม้ากระทืบโรง (เป๊ก)", "50.00", "Horse.jpg"),
  31.             Tuple.Create("44", "เบียร์สิงห์ (กระป๋อง)", "80.00", "SinghaCan.jpg"),
  32.             Tuple.Create("45", "เบียร์ลีโอ (กระป๋อง)", "70.00", "LeoCan.jpg"),
  33.             Tuple.Create("46", "เบียร์ช้าง (กระป๋อง)", "70.00", "ChangCan.jpg"),
  34.             Tuple.Create("47", "Spy Wine Cooler", "50.00", "Spy.jpg"),
  35.             Tuple.Create("48", "โซจู (สตรอเบอรี่)", "90.00", "Soju.jpg")
  36.         }
  37.         '// กำหนดจำนวนหลักที่ต้องการ
  38.         Dim columns As Integer = 3
  39.         '// Size and spacing of UserControl.
  40.         Dim controlWidth As Integer = 140   '// ความกว้าง
  41.         Dim controlHeight As Integer = 207  '// ความสูง
  42.         Dim spacing As Integer = 1  '// ระยะรอยต่อ
  43.         '// สร้าง TabControl แบบไดนามิค
  44.         Dim tabControl As New TabControl()
  45.         With tabControl
  46.             .Size = New Size(columns * controlWidth + 31, Me.ClientSize.Height)
  47.             .Location = New Point(4, 1)
  48.             '// ตั้งค่า Anchor สำหรับ TabControl
  49.             .Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left
  50.             .SizeMode = TabSizeMode.FillToRight
  51.             .Font = New Font("Tahoma", 11, FontStyle.Bold)
  52.         End With
  53.         '// เพิ่ม TabControl ลงใน Form
  54.         Me.Controls.Add(tabControl)

  55.         '// เพิ่ม TabPage หนึ่งหน้า
  56.         Dim tabPageMain As New TabPage("รายการสินค้า")
  57.         tabControl.TabPages.Add(tabPageMain)
  58.         '// สร้าง Panel ภายใน TabPage
  59.         Dim panel1 As New Panel()
  60.         With panel1
  61.             .Location = New System.Drawing.Point(1, 1)
  62.             .Size = New System.Drawing.Size(tabControl.Width - 10, tabControl.Height - 30)
  63.             .BackColor = Color.Moccasin
  64.             .AutoScroll = True
  65.             .Anchor = AnchorStyles.Bottom + AnchorStyles.Top
  66.         End With
  67.         '// เพิ่ม Panel ลงใน TabPage
  68.         tabPageMain.Controls.Add(panel1)

  69.         '// วางรายการสินค้าแต่ละรายการบนฟอร์ม (อยู่ภายใน Panel1)
  70.         For iCol As Integer = 0 To products.Count - 1
  71.             Dim product As Tuple(Of String, String, String, String) = products(iCol)
  72.             '// Instance Name ของ UserControl (Widget)
  73.             Dim widget As New Widget
  74.             Dim ImagePath As String = MyPath(Application.StartupPath & "Picture")
  75.             '// ตั้งค่ารายการสินค้าตามข้อมูลตัวอย่าง.
  76.             With widget
  77.                 .WidgetName = product.Item2.ToString
  78.                 .WidgetPrice = product.Item3.ToString
  79.                 '// แสดงผลภาพลงบนปุ่ม
  80.                 Dim imgFile As Image = Image.FromFile(ImagePath & product.Item4)
  81.                 .WidgetButtonImage = imgFile
  82.                 .WidgetButtonText = product.Item1
  83.             End With
  84.             '// Calculate row and column.
  85.             Dim row As Integer = iCol \ columns '\\ การหารตัดเศษ
  86.             Dim column As Integer = iCol Mod columns    '\\ การหารเอาเศษ
  87.             '// ตั้งค่าตำแหน่งและขนาดของ UserControl
  88.             widget.Location = New Point(column * (controlWidth + spacing), row * (controlHeight + spacing))
  89.             widget.Size = New Size(controlWidth, controlHeight)
  90.             '// การหารเอาเศษด้วย 2 ผลลัพธ์ 0 คือเลขคู่ ผลลัพธ์ 1 คือเลขคี่
  91.             '// เปลี่ยนสีฟอนต์ (ForeColor)
  92.             If iCol Mod 2 = 0 Then
  93.                 widget.WidgetPriceForeColor = Color.Yellow
  94.             Else
  95.                 widget.WidgetPriceForeColor = Color.White
  96.             End If
  97.             '// เพิ่มเติม: การสลับสีพื้นหลังของ Label 2 ตัว.
  98.             If (row Mod 2) = 0 Then
  99.                 'widget.WidgetNameBackColor = Color.SeaGreen
  100.                 'widget.WidgetPriceBackColor = Color.Orange
  101.             Else
  102.                 'widget.WidgetNameBackColor = Color.Orange
  103.                 'widget.WidgetPriceBackColor = Color.SeaGreen
  104.             End If
  105.             '// เพิ่ม User Control ลงไปใน Panel1
  106.             panel1.Controls.Add(widget)

  107.             '// Add click event handler.
  108.             AddHandler widget.btnWidgetName.Click, AddressOf WidgetName_Click
  109.         Next
  110.     End Sub

  111.     Private Sub WidgetName_Click(sender As Object, e As EventArgs)
  112.         '// รับ Control ที่ถูกคลิก
  113.         Dim ClickedWidget As Control = CType(sender, Control)
  114.         '// รับ UserControl ที่เป็นเจ้าของปุ่มนั้น
  115.         Dim ParentControl As Widget = CType(ClickedWidget.Parent, Widget)
  116.         '// แสดงข้อความที่มีชื่อและราคาสินค้า
  117.         MessageBox.Show("ID : " & ParentControl.btnWidgetName.Text & vbCrLf & "Product : " & ParentControl.WidgetName & vbCrLf & "Price : " & ParentControl.WidgetPrice)
  118.     End Sub

  119. #Region "FUNCTION"
  120.     Function MyPath(ByVal AppPath As String) As String
  121.         '/ Return Value
  122.         MyPath = AppPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "").Replace("\bin\x86\release", "")
  123.         '// If not found folder then put the \ (BackSlash) at the end.
  124.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  125.     End Function
  126. #End Region
  127. End Class
คัดลอกไปที่คลิปบอร์ด


ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...


ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-9-8 08:07 , Processed in 0.214059 second(s), 5 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้