Me.Controls("Textbox" & (1).ToString).Text = "something"
Saturday, 22 September 2012
Tuesday, 31 July 2012
Saturday, 21 July 2012
Create dynamic Form by string with parameters
1- Add 3 forms (form1,form2,form3) to a project, Add one button (button1) to form1
2- In form2, create Sub New as Below
4- In form1, in button1_click add this
5- The function of GetFormByStringWithParameter is as below (you can put it in form1,or module)
2- In form2, create Sub New as Below
Public Class Form2 Sub New(p1,p2,p3) ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. MsgBox(p1.ToString) End Sub
End Class
3- In form3, create Sub New as Below
Public Class Form3 Sub New(p1,p2) ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. MsgBox(p2.ToString) End Sub End Class
4- In form1, in button1_click add this
Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim x = GetFormByStringWithParameter("form2",{1,2,3}) x.Show Dim y = GetFormByStringWithParameter("form3",{22,33}) y.Show End Sub
5- The function of GetFormByStringWithParameter is as below (you can put it in form1,or module)
Function GetFormByStringWithParameter(ByVal form_name As String,byval form_parameter() As Object) As Form Dim FullTypeName = Application.ProductName & "." & form_name Dim FormInstanceType = Type.GetType(FullTypeName, True, True) Dim objForm = CType(Activator.CreateInstance(FormInstanceType,form_parameter),Form) Return objForm End Function
Thursday, 12 July 2012
Use Dynamic field in Linq WHERE clause
Sometimes you need to do something like :
SELECT * FROM table WHERE sFieldName = sValue
Here is how to translate the above to LINQ
SELECT * FROM table WHERE sFieldName = sValue
Here is how to translate the above to LINQ
Dim db As New northwindDataContext Dim sFieldName = "City" Dim sFieldValue = "Berlin"
Dim q = From t In db.Customers.AsEnumerable. Where(Function(f) f.GetType.GetProperty(sFieldName).GetValue(f,Nothing) = sFieldValue) Select t
Tuesday, 26 June 2012
Fill listview by datatable
Sub FillListview_by_datatable(ByVal lvw As ListView,ByVal dt As DataTable) lvw.Clear For Each col As DataColumn In dt.Columns lvw.Columns.Add(col.ColumnName) Next For i = 0 to dt.Rows.Count -1 Dim lvwitem As ListViewItem = lvw.Items.Add(dt.Rows(i)(0)) For j = 1 to dt.Columns.Count -1 lvwitem.SubItems.Add(dt.Rows(i)(j).ToString) Next Next End Sub
Fill listview by linq result
Here is how to fill listview (winform) (vb.net) from any linq result without the need to knowing
the fields name or count, just pass query_result.ToArray to the function
Enjoy!!
Dim db As New northwindDataContext Dim query = From t In db.Customers Select t FillListview_by_Linqresult(query.ToArray,lvwListView1)
Sub FillListview_by_Linqresult (ByVal linq_result As Array,ByVal lvw As ListView) Dim col_count = linq_result(0).GetType.GetProperties().Count ' Add listview headers For i = 0 to col_count -1 Dim col_name = linq_result(0).GetType.GetProperties()(i).Name.ToString lvw.Columns.Add(col_name) Next i ' Fill listview with values For i = 0 to linq_result.Length - 1 Dim lvwitem as ListViewItem = lvw.Items.Add(linq_result(i).GetType.GetProperties()(0).GetValue(linq_result(i),Nothing)) For j = 1 to col_count -1 Dim sub_item = linq_result(i).GetType.GetProperties()(j).GetValue(linq_result(i),Nothing) If sub_item is Nothing then sub_item = "" sub_item = sub_item.ToString.Trim lvwitem.SubItems.Add(sub_item) Next j Next i End Sub
Subscribe to:
Posts (Atom)