8 Nisan 2013 Pazartesi
UserControl örneği
USERCONTROL FORM ÖRNEĞİ
İNDİR
User Control, programcının kendi ihtiyaçları doğrultusunda oluşturabileceği özelleştirilmiş kontrollerdir. Mesela bir Form, textbox vb.. Daha geniş açıklama ve örnek için
Tıkla
Public Class Form1 Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click Dim UserCntrl As New UserControl1 Me.Controls.Add(UserCntrl) UserCntrl.BringToFront() End Sub Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click Dim UserCntrl As New UserControl2 Me.Controls.Add(UserCntrl) UserCntrl.BringToFront() End Sub Private Sub ToolStripButton3_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton3.Click Dim UserCntrl As New UserControl1(" ERDOĞAN", "ASLAN", "EMEKLİ") Me.Controls.Add(UserCntrl) UserCntrl.BringToFront() UserCntrl.Label1.Text = "BİRİNCİ USER KONTROL PARAMETRELİ" End Sub Private Sub ToolStripButton4_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton4.Click Dim UserCntrl As New UserControl3 Me.Controls.Add(UserCntrl) UserCntrl.BringToFront() End Sub Private Sub ToolStripButton6_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton6.Click UsrFrmKapat.PerformClick() End Sub Private Sub UsrFrmKapat_Click(sender As System.Object, e As System.EventArgs) Handles UsrFrmKapat.Click 'HER İKİ KOD DA ÇALIŞIYOR 'Dim ctrl = (From c As Control In Me.Controls _ ' Where TypeOf c Is UserControl1 _ ' OrElse TypeOf c Is UserControl2 _ ' OrElse TypeOf c Is UserControl3 Select c).ToList 'For Each uc In ctrl ' uc.Dispose() 'Next 'Kontrol sayısı çok ise FOR -NEXT döngüsü da kolay ve mantıklı For i = Me.Controls.Count - 1 To 0 Step -1 Dim cntrl As Control = Me.Controls(i) If (TypeOf cntrl Is UserControl) Then cntrl.Dispose() End If Next End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Static sy As Integer = 0 sy = sy + 1 Dim s As String = "ABCÇDEFGHIJKLMNOPQRSTUVWXYZ0123456789" Dim r As New Random Dim sb As New System.Text.StringBuilder For i As Integer = 1 To 20 Dim idx As Integer = r.Next(0, 35) sb.Append(s.Substring(idx, 1)) Next Label3.Text = sy.ToString TextBox1.Text += sb.ToString & vbCrLf End Sub End Class Public Class UserControl1 Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.BackColor = Color.NavajoWhite End Sub Public Sub New() InitializeComponent() 'bileşeni(component) başlat(initialize) Me.Dock = DockStyle.Fill End Sub Public Sub New(ByVal ad As String, ByVal soyad As String, ByVal meslek As String) InitializeComponent() TextBox1.Text = "ERDOĞAN" TextBox2.Text = "ASLAN" TextBox3.Text = "EMEKLİ" Me.Dock = DockStyle.Fill End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Dispose() End Sub End Class Public Class UserControl2 Private Sub UserControl2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.BackColor = Color.Bisque End Sub Public Sub New() 'Bu çağrı Windows Form Designer tarafından gereklidir. InitializeComponent() Me.Dock = DockStyle.Fill 'Initializecomponent () çağrısından sonra herhangi bir başlatma ekleyin. End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Dispose() End Sub End Class Public Class UserControl3 Public Sub New() InitializeComponent() Me.Dock = DockStyle.Fill End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click, MyBase.Click Me.Dispose() End Sub End Class Imports System Imports System.Windows.Forms Imports System.Drawing Imports System.ComponentModel Imports Microsoft.VisualBasic Namespace UserControls Public Class BilgiUserKontrol Inherits System.Windows.Forms.UserControl ' Create the controls. Private errorProvider1 As System.Windows.Forms.ErrorProvider Private AdıTtxt As System.Windows.Forms.TextBox Private AdresTxt As System.Windows.Forms.TextBox Private ŞehirTxt As System.Windows.Forms.TextBox Private iliTxt As System.Windows.Forms.TextBox Private PostaKoduTxt As System.Windows.Forms.TextBox Private ÜlkeTxt As System.Windows.Forms.TextBox Private WithEvents EpostaTxt As System.Windows.Forms.TextBox Private AdıLbl As System.Windows.Forms.Label Private AdresLbl As System.Windows.Forms.Label Private İliLbl As System.Windows.Forms.Label Private ÜlkeLbl As System.Windows.Forms.Label Private EpostaLbl As System.Windows.Forms.Label Private components As System.ComponentModel.IContainer Private _p1 As String Private _p2 As String Private _p3 As String ' Define the constructor. Public Sub New() InitializeComponent() Me.Dock = DockStyle.Fill End Sub Sub New(p1 As String, p2 As String, p3 As String) ' TODO: Complete member initialization _p1 = p1 _p2 = p2 _p3 = p3 End Sub ' Initialize the control elements. Public Sub InitializeComponent() Me.components = New System.ComponentModel.Container() Me.errorProvider1 = New System.Windows.Forms.ErrorProvider(Me.components) Me.AdıTtxt = New System.Windows.Forms.TextBox() Me.AdresTxt = New System.Windows.Forms.TextBox() Me.ŞehirTxt = New System.Windows.Forms.TextBox() Me.iliTxt = New System.Windows.Forms.TextBox() Me.PostaKoduTxt = New System.Windows.Forms.TextBox() Me.ÜlkeTxt = New System.Windows.Forms.TextBox() Me.EpostaTxt = New System.Windows.Forms.TextBox() Me.AdıLbl = New System.Windows.Forms.Label() Me.AdresLbl = New System.Windows.Forms.Label() Me.İliLbl = New System.Windows.Forms.Label() Me.ÜlkeLbl = New System.Windows.Forms.Label() Me.EpostaLbl = New System.Windows.Forms.Label() Me.Button1 = New System.Windows.Forms.Button() CType(Me.errorProvider1, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'errorProvider1 ' Me.errorProvider1.ContainerControl = Me ' 'AdıTtxt ' Me.AdıTtxt.Location = New System.Drawing.Point(83, 11) Me.AdıTtxt.Name = "AdıTtxt" Me.AdıTtxt.Size = New System.Drawing.Size(232, 20) Me.AdıTtxt.TabIndex = 0 ' 'AdresTxt ' Me.AdresTxt.Location = New System.Drawing.Point(83, 35) Me.AdresTxt.Name = "AdresTxt" Me.AdresTxt.Size = New System.Drawing.Size(232, 20) Me.AdresTxt.TabIndex = 1 ' 'ŞehirTxt ' Me.ŞehirTxt.Location = New System.Drawing.Point(83, 59) Me.ŞehirTxt.Name = "ŞehirTxt" Me.ŞehirTxt.Size = New System.Drawing.Size(96, 20) Me.ŞehirTxt.TabIndex = 2 ' 'iliTxt ' Me.iliTxt.Location = New System.Drawing.Point(179, 59) Me.iliTxt.Name = "iliTxt" Me.iliTxt.Size = New System.Drawing.Size(56, 20) Me.iliTxt.TabIndex = 3 ' 'PostaKoduTxt ' Me.PostaKoduTxt.Location = New System.Drawing.Point(235, 59) Me.PostaKoduTxt.Name = "PostaKoduTxt" Me.PostaKoduTxt.Size = New System.Drawing.Size(80, 20) Me.PostaKoduTxt.TabIndex = 4 ' 'ÜlkeTxt ' Me.ÜlkeTxt.Location = New System.Drawing.Point(83, 83) Me.ÜlkeTxt.Name = "ÜlkeTxt" Me.ÜlkeTxt.Size = New System.Drawing.Size(232, 20) Me.ÜlkeTxt.TabIndex = 5 ' 'EpostaTxt ' Me.EpostaTxt.Location = New System.Drawing.Point(83, 107) Me.EpostaTxt.Name = "EpostaTxt" Me.EpostaTxt.Size = New System.Drawing.Size(232, 20) Me.EpostaTxt.TabIndex = 6 ' 'AdıLbl ' Me.AdıLbl.Location = New System.Drawing.Point(8, 8) Me.AdıLbl.Name = "AdıLbl" Me.AdıLbl.Size = New System.Drawing.Size(78, 23) Me.AdıLbl.TabIndex = 0 Me.AdıLbl.Text = "Adı:" Me.AdıLbl.TextAlign = System.Drawing.ContentAlignment.MiddleLeft ' 'AdresLbl ' Me.AdresLbl.Location = New System.Drawing.Point(8, 32) Me.AdresLbl.Name = "AdresLbl" Me.AdresLbl.Size = New System.Drawing.Size(78, 23) Me.AdresLbl.TabIndex = 1 Me.AdresLbl.Text = "Adres" Me.AdresLbl.TextAlign = System.Drawing.ContentAlignment.MiddleLeft ' 'İliLbl ' Me.İliLbl.Location = New System.Drawing.Point(8, 56) Me.İliLbl.Name = "İliLbl" Me.İliLbl.Size = New System.Drawing.Size(78, 23) Me.İliLbl.TabIndex = 2 Me.İliLbl.Text = "İL / İlçe / Kod" Me.İliLbl.TextAlign = System.Drawing.ContentAlignment.MiddleLeft ' 'ÜlkeLbl ' Me.ÜlkeLbl.Location = New System.Drawing.Point(8, 80) Me.ÜlkeLbl.Name = "ÜlkeLbl" Me.ÜlkeLbl.Size = New System.Drawing.Size(78, 23) Me.ÜlkeLbl.TabIndex = 3 Me.ÜlkeLbl.Text = "Ülke / Bölge:" Me.ÜlkeLbl.TextAlign = System.Drawing.ContentAlignment.MiddleLeft ' 'EpostaLbl ' Me.EpostaLbl.Location = New System.Drawing.Point(8, 104) Me.EpostaLbl.Name = "EpostaLbl" Me.EpostaLbl.Size = New System.Drawing.Size(78, 23) Me.EpostaLbl.TabIndex = 4 Me.EpostaLbl.Text = "EPosta :" Me.EpostaLbl.TextAlign = System.Drawing.ContentAlignment.MiddleLeft ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(240, 133) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(75, 23) Me.Button1.TabIndex = 7 Me.Button1.Text = "Kapat" Me.Button1.UseVisualStyleBackColor = True ' 'BilgiUserKontrol ' Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.AdıLbl) Me.Controls.Add(Me.AdresLbl) Me.Controls.Add(Me.İliLbl) Me.Controls.Add(Me.ÜlkeLbl) Me.Controls.Add(Me.EpostaLbl) Me.Controls.Add(Me.AdıTtxt) Me.Controls.Add(Me.AdresTxt) Me.Controls.Add(Me.ŞehirTxt) Me.Controls.Add(Me.iliTxt) Me.Controls.Add(Me.PostaKoduTxt) Me.Controls.Add(Me.ÜlkeTxt) Me.Controls.Add(Me.EpostaTxt) Me.Name = "BilgiUserKontrol" Me.Size = New System.Drawing.Size(339, 167) CType(Me.errorProvider1, System.ComponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) Me.PerformLayout() End Sub Private Sub MyValidatingCode() 'Kontrolde metin yok onaylayın. If EpostaTxt.Text.Length = 0 Then Throw New Exception("E-posta adresi gerekli bir alandır") Else 'Bir "." ve e-posta adresini bir "@" olduğunu doğrulayın If EpostaTxt.Text.IndexOf(".") = -1 Or EpostaTxt.Text.IndexOf("@") = -1 Then Throw New Exception("E-posta adresi geçerli bir e-posta adresi biçimi olmalıdır." + _ Microsoft.VisualBasic.ControlChars.Cr + "Örneğin 'eraslancemil@gmail.com gibi'") End If End If End Sub 'TextEmail içine kullanıcı tarafından veri girişi onaylayın. Private Sub textEmail_Validating(sender As Object, _ e As System.ComponentModel.CancelEventArgs) Handles EpostaTxt.Validating Try MyValidatingCode() Catch ex As Exception 'kullanıcı tarafından düzeltilmesi için metni seçer ve Olayı iptal eder. e.Cancel = True EpostaTxt.Select(0, EpostaTxt.Text.Length) 'Görüntülemek için metin ile ErrorProvider hata ayarlayın. Me.errorProvider1.SetError(EpostaTxt, ex.Message) End Try End Sub Public Sub New(ByVal ad As String, ByVal soyad As String, ByVal meslek As String, ByVal priviance As String _ , ByVal kod As String, ByVal ülke As String, ByVal eposta As String) InitializeComponent() 'Parametreli User text formu AdıTtxt.Text = "ERDOĞAN" AdresTxt.Text = "ASLAN" ŞehirTxt.Text = "BORNOVA" iliTxt.Text = "İZMİR" PostaKoduTxt.Text = "35030" ÜlkeTxt.Text = "TÜRKİYE" EpostaTxt.Text = "eraslancemil@gmail.com" Me.Dock = DockStyle.Fill End Sub Private Sub textEmail_Validated(sender As Object, e As System.EventArgs) Handles EpostaTxt.Validated ' Tüm koşullar yerine getirilmiş ise, hataları temizleyin. errorProvider1.SetError(EpostaTxt, "") End Sub Friend WithEvents Button1 As System.Windows.Forms.Button Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Me.Dispose() End Sub End Class End Namespace
Sonraki Kayıt
Önceki Kayıt
Ana Sayfa