.:: Jasa Membuat Aplikasi Website,Desktop,Android Order Now..!! | | Order Now..!! Jasa Membuat Project Arduino,Robotic,Print 3D ::.

Add Control in your form dynamically

0 komentar


بِسْــــــــــــــــمِ اﷲِالرَّحْمَنِ اارَّحِيم
bismillaahirrahmaanirrahiim

السَّلاَمُ عَلَيْكُمْ وَرَحْمَةُ اللهِ وَبَرَكَاتُهُ
Assalamu'alaikum warahmatullahi wabarakatuh


Add Control in your form dynamically



The form designer is very fun to use and useful to create form and usercontrol. Each time you add a control in your form, you have to think about the position of each control in that form. So is a pain in the butt.


I�ll make a little sample on adding control dynamically in a form. I�m too lazy to make something complicated. I�ll make a simple table.


  'adding controls to make a table
        Dim oTextbox As TextBox
        For index1 As Integer = 0 To 3 Step 1
            For index2 As Integer = 0 To 3 Step 1
                oTextbox = New TextBox()
                With oTextbox
                    .Name = "TextBox" & index1.ToString & index2.ToString
                    .Text = index1.ToString & index2.ToString
                    .Width = Me.Width / 4
                    .Left = index1 * (Me.Width / 4)
                    '.Height
                    .Top = index2 * .Height
                End With
                oForm.Controls.Add(oTextbox)
            Next index2
        Next index1



If you use the show function to display your dynamically created form, you could simply put it before you call the show function or after. If you use ShowDialog, your control will not be added in your form because your form is modal.



    Private Sub Button1_Click(sender As System.Object, e As System.EventArgsHandles Button1.Click

        Dim oForm As Form
        oForm = New Form
        oForm.Show()

        'adding controls to make a table
        Dim oTextbox As TextBox
        For index1 As Integer = 0 To 3 Step 1
            For index2 As Integer = 0 To 3 Step 1
                oTextbox = New TextBox()
                With oTextbox
                    .Name = "TextBox" & index1.ToString & index2.ToString
                    .Text = index1.ToString & index2.ToString
                    .Width = Me.Width / 4
                    .Left = index1 * (Me.Width / 4)
                    '.Height
                    .Top = index2 * .Height
                End With
                oForm.Controls.Add(oTextbox)
            Next index2
        Next index1
    End Sub




When you run the program, your new form will look like this:


Table created dynamically



I don�t want to make big post. That is the reason I�ll stop here. There are many things we could do here.



You could �improve� your code using a try and catch to control any program crash. This make you program better in quality. I also put the dim outside the try-catch. If something goes wrong in the loop, at least, it will display the index1 that cause the problem.




    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim oForm As Form
        Dim oTextbox As TextBox
        Dim index1 As Integer
        Dim index2 As Integer
        Try
            oForm = New Form
            oForm.Text = "TABLE crfeated dynamically"
            oForm.Show()

            'adding controls to make a table

            For index1 = 0 To 3 Step 1
                For index2 = 0 To 3 Step 1
                    oTextbox = New TextBox()
                    With oTextbox
                        .Name = "TextBox" & index1.ToString & index2.ToString
                        .Text = index1.ToString & index2.ToString
                        .Width = Me.Width / 4
                        .Left = index1 * (Me.Width / 4)
                        '.Height
                        .Top = index2 * .Height
                    End With
                    oForm.Controls.Add(oTextbox)
                Next index2
            Next index1
        Catch ex As Exception
            MsgBox(ex.StackTrace & vbCrLf & "index1= " & index1 & vbCrLf & "index2= " & index2)
        End Try
    End Sub



If you like this post, leave a comment or share it.

You could also visit my web site at http://checktechno.ca




Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
NB :: Bila Sobat tertarik Ingin membuat software, membeli software, membeli source code, membeli hardware elektronika untuk kepentingan Perusahaan maupun Tugas Akhir (TA/SKRIPSI), Insyaallah Saya siap membantu, untuk Respon Cepat dapat menghubungi kami, melalui :

No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com


atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :

ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِين
Alhamdulilah hirobil alamin

وَ السَّلاَمُ عَلَيْكُمْ وَرَحْمَةُ اللهِ وَبَرَكَاتُهُ
wassalamualaikum warahmatullahi wabarakatuh


Artikel Add Control in your form dynamically, Diterbitkan oleh scodeaplikasi pada Senin, 26 November 2012. Semoga artikel ini dapat menambah wawasan Anda. Website ini dipost dari beberapa sumber, bisa cek disini sumber, Sobat diperbolehkan mengcopy paste / menyebar luaskan artikel ini, karena segala yang dipost di public adalah milik public. Bila Sobat tertarik Ingin membuat software, membeli software, membeli source code ,Dengan Cara menghubungi saya Ke Email: Fajarudinsidik@gmail.com, atau No Hp/WA : (fajar) : 085369237896, (puat) : 085267792168.

Tawk.to