 |
|
ทดสอบคลาสสำหรับ แก้ไข้ข้อความใน ListView ที่มีแสดงรายการเป็นแบบ Details |
|
 |
|
|
 |
 |
|
ผมสร้างคลาสสำหรับ แก้ไข้ข้อความใน ListView พึ่งเสร็จ จึงอยากลองให้ชาว thaicreate ที่สนใจลองทดสอบดู
หากเป็นการตั้งกระทู้ไม่ถูกต้องหรือผิดกฏอะไรก็ต้องขอโทษมาในที่นี้ด้วยครับ คืออยากได้จะได้รับการแลกเปลี่ยนความคิดเห็นจากนักพัฒนาโปรแกรมด้วยกัน เพื่ออาจได้แชร์ข้อมูลที่เป็นประโยชน์ให้กัน
Class นี้แก้ไขได้อย่างเดียวครับ หากต้องการเชื่อมข้อมูลกับฐานข้อมูลหรือตัวแปลอื่นก็จะลองสร้าง Property มาภายหลัง แต่ในวิธีนี้หากต้องการส่งกลับข้อมูลไปที่อ็อปเจคอื่น คงต้องใช้เนื้อหาใน ListView.Items ไปก่อน
อยากให้ลองใช้ครับดูครับเพื่อใครอาจจะพบบัค หรือมีไอเดียอื่นๆ
ที่ผมทำแบบนี้เพราะอยากใช้ ListView มากกว่า GridView จริงๆ ฮาๆ
วิธีใช้: ใช้ประกาศในฟอร์ครับ
เช่นใน Event Load
Ex.: new(byRef ตัวแปร ListView ที่ต้องการให้แก้ไขได้)
Code (VB.NET)
Class EditPanel
Inherits Panel
Private WithEvents LB As ListView
Private UbC As Integer, Txb() As TextBox, Activate As Boolean
Sub New(ByRef Listbox As ListView)
LB = Listbox
Me.Hide()
LB.Controls.Add(Me)
End Sub
Private Sub ChangeWidth(sender As Object, e As ColumnWidthChangingEventArgs) Handles LB.ColumnWidthChanging
Dim SLf As Integer
If Activate Then
Application.DoEvents()
For I As Integer = 0 To e.ColumnIndex - 1
SLf += LB.Columns(I).Width
Next
For I As Integer = e.ColumnIndex To UbC
With Txb(I)
.Left = SLf
If I >= e.ColumnIndex Then
.Width = LB.Columns(I).Width
End If
End With
SLf += LB.Columns(I).Width
Next
Me.Width = LB.Width
End If
End Sub
Private Function MouseIsOverControl(ByVal c As Control) As Boolean
Return c.ClientRectangle.Contains(c.PointToClient(Control.MousePosition))
End Function
Private Sub ShowMe(sender As Object, e As EventArgs) Handles LB.DoubleClick
Dim rc As Rectangle = LB.FocusedItem.GetBounds(ItemBoundsPortion.Entire)
Dim Lf, Tp, Ht As Integer
Tp = rc.Top
Lf = LB.Left + rc.Left
Ht = rc.Height
UbC = LB.Columns.Count - 1
With Me
.Controls.Clear()
Dim SLf As Integer
ReDim Txb(UbC)
For i As Integer = 0 To UbC
Txb(i) = New TextBox
With Txb(i)
.Text = LB.FocusedItem.SubItems(i).Text
.Left = SLf
.Width = LB.Columns(i).Width
SLf += LB.Columns(i).Width
End With
.Controls.Add(Txb(i))
Next
.Left = Lf
.Top = Tp
.Height = Ht + 1
.Width = LB.Width
Activate = True
.Show()
Dim Txtb As TextBox = Nothing
For Each ctrl As Control In .Controls
If TypeOf ctrl Is TextBox Then
Txtb = DirectCast(ctrl, TextBox)
AddHandler Txtb.KeyPress, AddressOf Me.EnterValue
If MouseIsOverControl(ctrl) Then ctrl.Select()
End If
Next
End With
End Sub
Sub EnterValue(sender As TextBox, e As KeyPressEventArgs)
If e.KeyChar = vbCr Then
LB.FocusedItem.SubItems(sender.TabIndex).Text = sender.Text
HideMe(sender, e)
End If
End Sub
Private Sub HideMe(sender As Object, e As EventArgs) Handles Me.LostFocus, LB.ItemSelectionChanged
Me.Hide()
Activate = False
End Sub
End Class
Tag : VB.NET, Windows
|
|
 |
 |
 |
 |
Date :
2016-07-07 22:55:29 |
By :
nk4ever |
View :
1112 |
Reply :
7 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มีภาพประกอบจะน่าสนใจมากกว่านี้เยอะเลยครับ
|
 |
 |
 |
 |
Date :
2016-07-08 11:29:06 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Class EditPanel
Inherits Panel
Private WithEvents LB As ListView
Private UbC As Integer, Txb() As TextBox, Activate As Boolean, sortColumn As Integer = -1
Sub New(ByRef Listbox As ListView)
LB = Listbox
Me.Hide()
With LB
.Controls.Add(Me)
.GridLines = True
.FullRowSelect = True
End With
End Sub
Private Sub ChangeWidth(sender As Object, e As ColumnWidthChangingEventArgs) Handles LB.ColumnWidthChanging
Dim SLf As Integer
If Activate Then
Application.DoEvents()
For I As Integer = 0 To e.ColumnIndex - 1
SLf += LB.Columns(I).Width
Next
For I As Integer = e.ColumnIndex To UbC
With Txb(I)
.Left = SLf
.Width = LB.Columns(I).Width
End With
SLf += LB.Columns(I).Width
Next
Me.Width = LB.Width
End If
End Sub
Private Function MouseIsOverControl(ByVal c As Control) As Boolean
Return c.ClientRectangle.Contains(c.PointToClient(Control.MousePosition))
End Function
Private Sub ShowMe(sender As Object, e As EventArgs) Handles LB.DoubleClick
Dim rc As Rectangle = LB.FocusedItem.GetBounds(ItemBoundsPortion.Entire)
Dim Lf, Tp, Ht As Integer
Tp = rc.Top
Lf = LB.Left + rc.Left
Ht = rc.Height
UbC = LB.Columns.Count - 1
With Me
.Controls.Clear()
Dim SLf As Integer
ReDim Txb(UbC)
For i As Integer = 0 To UbC
Txb(i) = New TextBox
With Txb(i)
.Text = LB.FocusedItem.SubItems(i).Text
.Left = SLf
.Width = LB.Columns(i).Width
SLf += LB.Columns(i).Width
End With
.Controls.Add(Txb(i))
Next
.Left = Lf
.Top = Tp
.Height = Ht + 1
.Width = LB.Width
Activate = True
.Show()
Dim Txtb As TextBox = Nothing
For Each ctrl As Control In .Controls
If TypeOf ctrl Is TextBox Then
Txtb = DirectCast(ctrl, TextBox)
AddHandler Txtb.KeyPress, AddressOf Me.EnterValue
If MouseIsOverControl(ctrl) Then ctrl.Select()
End If
Next
End With
End Sub
Private Sub LB_ColumnClick(sender As Object, e As ColumnClickEventArgs) Handles LB.ColumnClick
If e.Column <> sortColumn Then
sortColumn = e.Column
LB.Sorting = SortOrder.Ascending
Else
If LB.Sorting = SortOrder.Ascending Then
LB.Sorting = SortOrder.Descending
Else
LB.Sorting = SortOrder.Ascending
End If
End If
LB.Sort()
LB.ListViewItemSorter = New ListViewItemComparer(e.Column, LB.Sorting)
End Sub
Private Sub EnterValue(sender As TextBox, e As KeyPressEventArgs)
If e.KeyChar = vbCr Then
LB.FocusedItem.SubItems(sender.TabIndex).Text = sender.Text
HideMe(sender, e)
ElseIf e.KeyChar = Chr(27) Then
HideMe(sender, e)
End If
End Sub
Private Sub HideMe(sender As Object, e As EventArgs) Handles Me.Leave
Me.Hide()
Activate = False
End Sub
End Class
ครับ
นี่เป็น Class ที่ผมกับการปรับปรุงเพิ่มเติมครับ
สิ่งที่เพิ่มคือ ฟอร์มแมตมุมมองให้เป็น Details + GridLine + FullRowSelect เมื่อประกาศ Class EditPanel ด้วย New
เพื่อไม่ต้องเขียนคำสั่งภายนอกคลาส
Code (VB.NET)
Sub New(ByRef Listbox As ListView)
LB = Listbox
Me.Hide()
With LB
.Controls.Add(Me)
.GridLines = True
.FullRowSelect = True
End With
End Sub
คลาสจะทำการรับ Listview เข้ามาโดยโดยอ้างอิงแบบ Reference แล้วเพิ่มตัวมันเองเข้าไปใน ListView นั้น ซึ่งอาจดูเป็นการประกาศ Control ที่ดูผิดวิธีไปนิดหนึ่ง แต่ที่ทำแบบนี้เพราะ ต้องการให้ผู้ใช้สามารถวาด Listview ในหน้าต่าง Designแบบตามใจชอบก่อน แล้วค่อยเพิ่มความสามารถให้กับมัน คือ Sort Column กับ แก้ไขข้อความ

เพิ่มเติมนะครับ คือมันจะอัพเดตค่าเมื่อกด Enter เท่านั้น ไม่ได้ Handle event Leave เข้าไป
|
ประวัติการแก้ไข 2016-07-08 13:43:41
 |
 |
 |
 |
Date :
2016-07-08 13:40:09 |
By :
nk4ever |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอถามสักนิดน่ะครับ
ทำไมถึงไม่ใช่ Gridview เหรอครับ
|
 |
 |
 |
 |
Date :
2016-07-08 13:42:20 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
วิธีนี้เสียเวลาครับ ลักไก่แบบนี้ใช้ tools ผิดประเภท แต่ก็ไม่ผิดที่จะใช้ครับ
|
 |
 |
 |
 |
Date :
2016-07-08 15:56:54 |
By :
อ่อลืม |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จาก #no 6 ผมมีเรื่อจริงที่จะเล่าให้คุณฟัง
ผมมีรุ่นน้องอยู่คนหนึ่ง : ผมเป็นคนทีร่จะขายความฝัน
ผม: แย้งไปว่า ในความฝันนั้นฯ จะฝันมากฝันน้อย มันไม่สำคัญหรอก?
รุ่นน้องของผม :รบกวนอธิบายให้ผมฟังหน่อย
ผม : ผมเวทนา เลยบอกไปว่า ทำมากทำน้อยมันไม่สำคัญหรอก
ขึ้นอยู่่กับว่าสิ่งที่ทำ ณ. เวลานั้นฯ สิ่งไหนทำประโยชน์ได้มากกว่ากัน
...
...
...
เล่าให้ฟังเฉยฯฯ
|
 |
 |
 |
 |
Date :
2016-07-08 21:49:30 |
By :
หน้าฮี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|