Public Shared Sub clearSTDTextBoxText(ByVal f As Form)
GetAllControl(f, GetType(System.Windows.Forms.TextBox)).ToList().ForEach(Sub(c As TextBox)
If c.Text.EndsWith(".00") Then 'คาดคะเนว่าเป็นตัวเลข 1,234.00 เป็นต้น
c.Text = "0.00"
Else
c.Text = String.Empty 'คาดคะเนว่าเป็นข้อความ
End If
End Sub)
End Sub
Public Class TestTextInput
Protected Actions As String = "E" 'A=เพิ่ม, E=แก้ไขข้อมูล เป็นต้น
Dim lstData As IList
Private Sub TestTextInput_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call InitialDataSet()
Call InitialComboBox()
Call InitialDataGridView()
Call DisplayData(lstData)
End Sub
Private Sub InitialDataSet()
'Demo Data
lstData = {New With {.FirstName = "ชื่อ A", .LastName = "นามสกุล A", .Salary = 2000},
New With {.FirstName = "ชื่อ B", .LastName = "นามสกุล B", .Salary = 3000}
}.ToList()
End Sub
Private Sub InitialComboBox()
'TODO
End Sub
Private Sub InitialDataGridView()
'TODO
End Sub
Private Sub DisplayData(ByVal curRecord As IList)
Dim txtBoxs = GetAllTextBox(Me).ToList()
If Actions = "E" Then 'แก้ไขข้อมูล
Try
Dim yObject = curRecord(1) 'สมมุติให้เป็น ระเบียนที่ 2
Dim columns() As System.Reflection.PropertyInfo = yObject.GetType().GetProperties()
If txtBoxs IsNot Nothing Then
txtBoxs.ForEach(Sub(x)
Dim txtName = If(x.Name.Length > 3, x.Name.Substring(3), x.Name)
Dim mapingName = columns.FirstOrDefault(Function(a) a.Name = txtName)
If mapingName IsNot Nothing Then
If x.GetType().ToString() = "WL_Controls.usrCtrlTextBox" Then
Dim txt = DirectCast(x, WL_Controls.usrCtrlTextBox)
txt.DataBindings.Add("Text", yObject, mapingName.Name)
If txt.DisplayManager = WL_Controls.usrCtrlTextBox.TextBoxDisplayManager.TextNumber Then
txt.Text = txt.FormatValue(CDbl(txt.Text)) 'ถ้าเป็นจำนวนเงินให้จัดรูปแบบแสดงผลด้วย
End If
Else
'Standard TextBox
x.DataBindings.Add("Text", yObject, mapingName.Name)
End If
End If
End Sub)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else 'เพิ่มข้อมูล
'Clear All Text.
If txtBoxs IsNot Nothing Then
txtBoxs.ForEach(Sub(x)
If x.GetType().ToString() = "WL_Controls.usrCtrlTextBox" Then
Dim txt = DirectCast(x, WL_Controls.usrCtrlTextBox)
If txt.DisplayManager = WL_Controls.usrCtrlTextBox.TextBoxDisplayManager.TextNumber Then
txt.Text = txt.FormatValue(0.0)
End If
Else
'Standard TextBox
x.Text = String.Empty
End If
End Sub)
End If
End If
End Sub
Private Sub DisplayReports()
'TODO
End Sub
Private Sub SaveData2RDBMS()
'TODO
End Sub
#Region " Utils"
Private Function GetAllTextBox(ByVal f As Control) As IEnumerable(Of Control)
Return GetAllControl(f, GetType(System.Windows.Forms.TextBox)).Union(GetAllControl(f, GetType(WL_Controls.usrCtrlTextBox))).ToList()
End Function
Private Function GetAllControl(ByVal ctrl As Control, t As Type) As IEnumerable(Of Control)
Dim _ctrl = ctrl.Controls.Cast(Of Control)()
Return _ctrl.SelectMany(Function(x) GetAllControl(x, t)).Concat(_ctrl).Where(Function(y) y.GetType() = t)
End Function
#End Region
End Class
Dim monthNames As String() = CultureInfo.CreateSpecificCulture(GetENUMDescriptions(_myComboBox.SetCulture)).DateTimeFormat.MonthNames
'Fills months combobox
For i As Integer = 0 To 11
monthList.Rows.Add(monthList.NewRow())
monthList.Rows(i)(0) = monthNames(i)
monthList.Rows(i)(1) = i + 1
Next
(สิ่งที่ได้ตามมาก็คือเทคนิคการใช้ Control DataGridView)
Code (VB.NET)
Dim dayNames As String() = CultureInfo.CreateSpecificCulture(GetENUMDescriptions(Me._myComboBox.SetCulture)).DateTimeFormat.AbbreviatedDayNames 'อ., จ., อ., พ หรือ Su, Mo, ... เป็นต้น
For i As Integer = 0 To dayNames.Count() - 1
dgv_Dates.Columns.Add("col" + i.ToString(), If(dayNames(i).Length > 2, dayNames(i).Substring(0, 2), dayNames(i)))
dgv_Dates.Columns(i).Width = 37
Next
Date :
2016-11-29 12:59:36
By :
หน้าฮี
No. 10
Guest
Code
The life must be learning all time and not finish. We never know that what will we met when we will have learning.
Even though, we perhaps meet good and bad person in the same time. Maybe this situation will be awaked us for continued learning by never ending.
[ Private _SelectedValue As String
<System.ComponentModel.DefaultValue(GetType(CultureAvaiable), "None"), System.ComponentModel.Category("Appearance")> _
Public Property SelectedValue() As CultureAvaiable
Get
Return _SelectedValue
End Get
Set(ByVal Value As CultureAvaiable)
_SelectedValue = Value
End Set
End Property
3./4. ใช้ button กดปุ๊บ มีอีกฟอร์มเด้งมาปั๊บ มี textbox สำหรับค้นหา เลือกปั๊บ binding ใน textbox
ก็กำหนด Property มารับค่าตามที่เห็นสมควร
อทิเช่น
Code (VB.NET)
[vb]Public Class frmDataSearch
Public txtBoxHost As TextBox 'your User Control TextฺBox 'กำหนดค่าตามสามัญสำนึก
Public lstDataSource As IList 'ค้นหาใน Memory/Databases
Public strColumnName As String() = {"", "", "", ""} 'สมมุติให้มี 4 Columns
Public curSelectedValue As string 'ค่าเดิมที่ถูกเลือก PK
Private bs As New BindingSource
Private Sub frmDataSearch_Load(sender As Object, e As EventArgs) Handles MyBase.Load
bs.DataSource = lstDataSource 'หมายความว่าคุณส่งมาพร้อมข้อมูล
InitialDataGridView(bs)
End Sub
Private Sub InitialDataGridView(ByVal bs As BindingSource)
dgvXXX.AutoGenerateColumns = False
dgvXXX.DataSource = bs
For i As Integer = 0 To strColumnName.Count - 1
If Not String.IsNullOrEmpty(strColumnName(i)) Then
dgvXXX.Columns(i).DataPropertyName = strColumnName(i)
End If
Next
If Not String.IsNullOrEmpty(curSelectedValue) Then
Dim index = lstDataSource
.Cast(Of Object).ToList().FindIndex(Function(c) GetPropertyValue(c, strColumnName(0)) = curSelectedValue)
If index <> -1 Then
bs.Position = index
dgvXXX.FirstDisplayedScrollingRowIndex = True
End If
Else
dgvXXX.ClearSelection()
End If
End Sub
End Class
ผมให้เอาไปคิดเป็นการบ้าน
การแชร์ Variable Value (ข้อมูลของตัวแปร) ข้ามเครื่อง Clients ทำอย่างไร?
Date :
2016-12-01 10:43:47
By :
หน้าฮี
No. 25
Guest
จาก #NO 23 ผมลืมให้ตัวนี้ไป
Code (VB.NET)
Public Shared Function GetPropertyValue(ByVal obj As Object, ByVal PropertyName As String) As Object
Return obj.GetType().GetProperty(PropertyName).GetValue(obj, Nothing)
End Function
Dim _Ass As Assembly = LoadAssembly(Application.StartupPath & "\Your_DLL.dll")
Dim _Type As Type = LoadType(_Ass, "Data_Table_Model.TableName")
Dim mi As MethodInfo = GetMethodInfo(_Type2, "GetUsers", New Type() {GetType(Integer), GetType(EnumManager.SearchOption)})
Dim lstObj = InvokeMethod(mi, New Object() {1234, EnumManager.SearchOption.StartWith})