User Control ve XML ile Örnek Bir Çalışma

Önceki yıllarda çekmiş olduğum 5 bölümlük bu seri ile; User Control oluşturma, oluşturulan user controlleri projemizde kullanma, user controllerden XML oluşturma ve XML ile çalışma hakkında sizi tecrübe sahibi yapacak bir User Control ve XML örnek projesi video serisidir.

Reklamlar

VB – Yanıp Sönen Etiket (Flashing Label) Kontrolü Oluşturma

Bu yazımızda yanıp sönen ve kullanıcının ilgilini çeken bir etiket(Label) kontrolü geliştireceğiz. Projelerimde çoğu zaman böyle bir ifadeye, kullanıcının mutlaka okuması gereken, dikkat etmesi gereken yazılara ihtiyaç duyarım. Çoğu uygulamada da bu şekilde ifadeler mevcuttur. Yaygın olarak bu ifadeler uygulama içinde “Kalın(Bold)” ve “Altı Çizili(Underline)” olarak ayarlanırlar. Ya da kırmızı olarak renklendirilirler. Fakat yaptığım projelerde karşılaştığım kadarı ile bu belirginleştirme, kullanıcıların farketmesini sağlamıyor. Ya da kullanıcıların alışkanlıklarından ötürü dikkatlerinin oraya çekilmesini sağlamıyor. Bu konuda düşünürken, bu konuda parlak bir fikir aklıma geldi. 🙂 Evettt gerçekten parlak, hatta yanıp sönen :)) Sonunda yanıp sönen bir etiket(Label) kontrolü geliştirmeye karar verdim. Sonunda nasıl birşey mi çıktı. Buyrun aşağıdadır.

kmb-udemy-reklam

HighlightLabel.vb
Public Class HighlightLabel
Inherits Label

#Region “Initialize”

Private components As System.ComponentModel.IContainer
Friend WithEvents tmrRepater As System.Windows.Forms.Timer

Public Sub New()
Me.InitializeComponent()
End Sub

Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.tmrRepater = New System.Windows.Forms.Timer(Me.components)
Me.SuspendLayout()
‘tmrRepater        
Me.tmrRepater.Interval = 5
‘        ‘HighlightLabel      
Me.ResumeLayout(False)

End Sub

#End Region

#Region “Events”

Public Event HighlightSpeedChanged(ByVal value As Integer)

#End Region

#Region “Properties”

Private _HighlightSpeed As Integer = 5
”’ I??kland?rman?n h?z?n? belirler.    
”’ (Minimum : 1 Maksimum : 50)  
”’   
Public Property HighlightSpeed() As Integer
Get
Return _HighlightSpeed
End Get
Set(ByVal value As Integer)

If (value < 1) Then value = 1
If (value > 50) Then value = 50

_HighlightSpeed = value

RaiseEvent HighlightSpeedChanged(value)

End Set
End Property

Private _IsHighlightEnabled As Boolean = True
Public Property IsHighlightEnabled() As Boolean
Get
Return _IsHighlightEnabled
End Get
Set(ByVal value As Boolean)
_IsHighlightEnabled = value

If (Not _IsHighlightEnabled) Then
Me.StopLabelHighlight()
End If

End Set
End Property

#End Region

#Region “Local Variables”

Dim red As Integer = 0
Dim green As Integer = 0
Dim blue As Integer = 0
Dim feeder As Integer = Me.HighlightSpeed

#End Region

#Region “Public Methods”

”’
”’ E?er IsHighlightEnabled = True ise ???kland?rmay? ba?lat?r.  
”’     
Public Sub StartLabelHighlight()

If (Me.IsHighlightEnabled) Then
Me.tmrRepater.Enabled = True
Me.tmrRepater.Start()
End If

End Sub

”’ I??kland?rmay? durdurur.      
Public Sub StopLabelHighlight()

Me.tmrRepater.Enabled = False
Me.tmrRepater.Stop()

End Sub

#End Region

#Region “Event Handlers”

Private Sub tmrRepater_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrRepater.Tick

If (red + feeder >= 255) Then
feeder = (-1) * Me.HighlightSpeed
red = 255
ElseIf (red + feeder <= 0) Then
feeder = (1) * Me.HighlightSpeed
red = 0
End If

red += feeder

Me.ForeColor = Color.FromArgb(red, green, blue)
End Sub

#End Region

End Class

image Kontrolü test etmek için gerekli test formu

 

Form3.Designer.vb
Partial Class Form3
Inherits System.Windows.Forms.Form

‘Form overrides dispose to clean up the component list.     
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub

‘Required by the Windows Form Designer    
Private components As System.ComponentModel.IContainer

‘NOTE: The following procedure is required by the Windows Form Designer    ‘It can be modified using the Windows Form Designer.      ‘Do not modify it using the code editor.     _    
Private Sub InitializeComponent()
Me.btnHizlandir = New System.Windows.Forms.Button()
Me.btnYavaslat = New System.Windows.Forms.Button()
Me.Label1 = New System.Windows.Forms.Label()
Me.HighlightLabel1 = New WindowsApplication1.HighlightLabel()
Me.SuspendLayout()

‘btnHizlandir       

Me.btnHizlandir.Location = New System.Drawing.Point(301, 97)
Me.btnHizlandir.Name = “btnHizlandir”
Me.btnHizlandir.Size = New System.Drawing.Size(75, 23)
Me.btnHizlandir.TabIndex = 1
Me.btnHizlandir.Text = “H?zland?r”
Me.btnHizlandir.UseVisualStyleBackColor = True

‘btnYavaslat        ‘        
Me.btnYavaslat.Location = New System.Drawing.Point(220, 97)
Me.btnYavaslat.Name = “btnYavaslat”
Me.btnYavaslat.Size = New System.Drawing.Size(75, 23)
Me.btnYavaslat.TabIndex = 2
Me.btnYavaslat.Text = “Yava?lat”
Me.btnYavaslat.UseVisualStyleBackColor = True        ‘        
‘Label1        ‘        
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(166, 102)
Me.Label1.Name = “Label1”
Me.Label1.Size = New System.Drawing.Size(31, 13)
Me.Label1.TabIndex = 3
Me.Label1.Text = “H?z : “        ‘        
‘HighlightLabel1        ‘        
Me.HighlightLabel1.AutoSize = True
Me.HighlightLabel1.Font = New System.Drawing.Font(“Microsoft Sans Serif”, 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(162, Byte))
Me.HighlightLabel1.HighlightSpeed = 5
Me.HighlightLabel1.IsHighlightEnabled = True
Me.HighlightLabel1.Location = New System.Drawing.Point(12, 32)
Me.HighlightLabel1.Name = “HighlightLabel1”
Me.HighlightLabel1.Size = New System.Drawing.Size(372, 29)
Me.HighlightLabel1.TabIndex = 0
Me.HighlightLabel1.Text = “Bu bir yan?p s?nen uyar? etiketidir.”        ‘        ‘Form3        ‘        
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(388, 132)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.btnYavaslat)
Me.Controls.Add(Me.btnHizlandir)
Me.Controls.Add(Me.HighlightLabel1)
Me.Name = “Form3”
Me.Text = “Form3”
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub
Friend WithEvents HighlightLabel1 As WindowsApplication1.HighlightLabel
Friend WithEvents btnHizlandir As System.Windows.Forms.Button
Friend WithEvents btnYavaslat As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
End Class

 

Form3.vb
Public Class Form3

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.HighlightLabel1.StartLabelHighlight()

‘Me.HighlightLabel1.StopLabelHighlight()

‘Me.HighlightLabel1.IsHighlightEnabled = True        
‘Me.HighlightLabel1.HighlightSpeed = 10

AddHandler Me.HighlightLabel1.HighlightSpeedChanged, AddressOf HighlightLabel1_HighlightSpeedChanged

Me.Label1.Text = “H?z : “ & Me.HighlightLabel1.HighlightSpeed.ToString()

End Sub

Private Sub btnHizlandir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHizlandir.Click
Me.HighlightLabel1.HighlightSpeed += 5
End Sub

Private Sub btnYavaslat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYavaslat.Click
Me.HighlightLabel1.HighlightSpeed -= 5
End Sub

Private Sub HighlightLabel1_HighlightSpeedChanged(ByVal value As Integer)
Me.Label1.Text = “H?z : “ & value.ToString()
End Sub

End Class

Sonuç :

HighlightSpeed” özelliği ile hızlandırabilir ya da yavaşlatabilirsiniz..

IsHighlightEnabled” özelliği ile etkisizleştirebilirsiniz..

HighlightSpeedChanged” olayını yönetebilirsiniz..

 Resim 1Resim 1  Resim 2Resim 2
 Resim 3Resim 3 Resim 4Resim 4
 Resim 5Resim 5

VB – Creating My RadGridView User Control

RadGridView kontrolünü kullanarak birçok işlemi yapabiliyoruz.. Bu işlemler;

Excel Export..
Kolon başlığı verme, kolon tipi verme, kolon boyutu, hizalama..
Kayıt Sayısı bilgisi..
Alternatif row ayarı, gruplama..

gibi işlemleri daha pratik yapabileceğim bir user control geliştirdim. Aşağıda bu kontrol ile ilgili bilgileri ve kodları bulabilir sizde kullanabilirsiniz..

Tabii bu kontrolü oluşturmak için Telerik firmasına ait WinControls setine sahip olmanız gerekmektedir.. İlgili referansları eklemeyi unutmayınız..

image

image

ctrlMyRadGridView.Designer.cs

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Partial Class ctrlMyRadGridViewInherits System.Windows.Forms.UserControl    

    'UserControl overrides dispose to clean up the component list.     _    
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer    

    Private components As System.ComponentModel.IContainer
    'NOTE: The following procedure is required by the Windows Form Designer    'It can be modified using the Windows Form Designer.      
    'Do not modify it using the code editor.     _    
    Private Sub InitializeComponent()
        Me.pnlRoot = New Telerik.WinControls.UI.RadPanel
        Me.pnlBody = New System.Windows.Forms.Panel
        Me.grvGridView = New Telerik.WinControls.UI.RadGridView
        Me.rssStatusBar = New Telerik.WinControls.UI.RadStatusStrip
        Me.lblGridViewRowCountInfo = New Telerik.WinControls.UI.RadLabelElement
        Me.pnlHead = New System.Windows.Forms.Panel
        Me.cmdExcelExport = New Telerik.WinControls.UI.RadButton
        CType(Me.pnlRoot, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.pnlRoot.SuspendLayout()
        Me.pnlBody.SuspendLayout()
        CType(Me.grvGridView, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.grvGridView.MasterGridViewTemplate, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.rssStatusBar, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.pnlHead.SuspendLayout()
        CType(Me.cmdExcelExport, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()        '        

        'pnlRoot        '        
        Me.pnlRoot.Controls.Add(Me.pnlBody)
        Me.pnlRoot.Controls.Add(Me.pnlHead)
        Me.pnlRoot.Dock = System.Windows.Forms.DockStyle.Fill
        Me.pnlRoot.Location = New System.Drawing.Point(0, 0)
        Me.pnlRoot.Name = "pnlRoot"
        Me.pnlRoot.Size = New System.Drawing.Size(289, 242)
        Me.pnlRoot.TabIndex = 0
        Me.pnlRoot.ThemeName = "Office2007Blue"        '        

        'pnlBody        '        
        Me.pnlBody.Controls.Add(Me.grvGridView)
        Me.pnlBody.Controls.Add(Me.rssStatusBar)
        Me.pnlBody.Dock = System.Windows.Forms.DockStyle.Fill
Me.pnlBody.Location = New System.Drawing.Point(0, 26)        Me.pnlBody.Name = "pnlBody"        
        Me.pnlBody.Size = New System.Drawing.Size(289, 216)

        Me.pnlBody.TabIndex = 3        '        

        'grvGridView        '        
        Me.grvGridView.Dock = System.Windows.Forms.DockStyle.Fill
        Me.grvGridView.EnableAlternatingRowColor = True
        Me.grvGridView.EnableFastScrolling = True
        Me.grvGridView.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(162, Byte))
        Me.grvGridView.Location = New System.Drawing.Point(0, 0)        '        '        '        
        Me.grvGridView.MasterGridViewTemplate.AllowAddNewRow = False
        Me.grvGridView.MasterGridViewTemplate.AllowDeleteRow = False
        Me.grvGridView.MasterGridViewTemplate.EnableFiltering = True
        Me.grvGridView.Name = "grvGridView"
        Me.grvGridView.Size = New System.Drawing.Size(289, 192)
        Me.grvGridView.TabIndex = 1

        'rssStatusBar        '        
        Me.rssStatusBar.AutoSize = True
        Me.rssStatusBar.Items.AddRange(New Telerik.WinControls.RadItem() {Me.lblGridViewRowCountInfo})
        Me.rssStatusBar.LayoutStyle = Telerik.WinControls.UI.RadStatusBarLayoutStyle.Stack
        Me.rssStatusBar.Location = New System.Drawing.Point(0, 192)
        Me.rssStatusBar.Name = "rssStatusBar"
        Me.rssStatusBar.Size = New System.Drawing.Size(289, 24)
        Me.rssStatusBar.SizingGrip = False
        Me.rssStatusBar.TabIndex = 0
        Me.rssStatusBar.Text = "RadStatusStrip1"        '        

        'lblGridViewRowCountInfo        '        
        Me.lblGridViewRowCountInfo.Margin = New System.Windows.Forms.Padding(1)
        Me.lblGridViewRowCountInfo.Name = "lblGridViewRowCountInfo"
        Me.rssStatusBar.SetSpring(Me.lblGridViewRowCountInfo, False)

        Me.lblGridViewRowCountInfo.Tag = "Kay?t Say?s? : {0}"

        Me.lblGridViewRowCountInfo.Text = "Kay?t Say?s? : {0}"

        Me.lblGridViewRowCountInfo.TextWrap = True

        'pnlHead        '        
        Me.pnlHead.Controls.Add(Me.cmdExcelExport)
        Me.pnlHead.Dock = System.Windows.Forms.DockStyle.Top
        Me.pnlHead.Location = New System.Drawing.Point(0, 0)
        Me.pnlHead.Name = "pnlHead"
        Me.pnlHead.Padding = New System.Windows.Forms.Padding(3, 3, 2, 3)
        Me.pnlHead.Size = New System.Drawing.Size(289, 26)
        Me.pnlHead.TabIndex = 2

        'cmdExcelExport    
        Me.cmdExcelExport.Dock = System.Windows.Forms.DockStyle.Right
        Me.cmdExcelExport.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(162, Byte))
        Me.cmdExcelExport.Image = Nothing
        Me.cmdExcelExport.Location = New System.Drawing.Point(182, 3)
        Me.cmdExcelExport.Name = "cmdExcelExport"
        Me.cmdExcelExport.Padding = New System.Windows.Forms.Padding(3, 0, 0, 0)
        Me.cmdExcelExport.Size = New System.Drawing.Size(105, 20)
        Me.cmdExcelExport.TabIndex = 0
        Me.cmdExcelExport.Text = "Excel Export"

        'ctrlMyRadGridView        '        
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.Controls.Add(Me.pnlRoot)
        Me.Name = "ctrlKocRadGridView"
        Me.Size = New System.Drawing.Size(289, 242)
        CType(Me.pnlRoot, System.ComponentModel.ISupportInitialize).EndInit()
        Me.pnlRoot.ResumeLayout(False)
        Me.pnlBody.ResumeLayout(False)
        Me.pnlBody.PerformLayout()
        CType(Me.grvGridView.MasterGridViewTemplate, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.grvGridView, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.rssStatusBar, System.ComponentModel.ISupportInitialize).EndInit()
        Me.pnlHead.ResumeLayout(False)
        CType(Me.cmdExcelExport, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

    Public WithEvents pnlRoot As Telerik.WinControls.UI.RadPanel
    Public WithEvents pnlHead As System.Windows.Forms.Panel
    Public WithEvents cmdExcelExport As Telerik.WinControls.UI.RadButton
    Public WithEvents pnlBody As System.Windows.Forms.Panel
    Public WithEvents grvGridView As Telerik.WinControls.UI.RadGridView
    Public WithEvents rssStatusBar As Telerik.WinControls.UI.RadStatusStrip
    Public WithEvents lblGridViewRowCountInfo As Telerik.WinControls.UI.RadLabelElement
End Class

ctrlMyRadGridView.vb

Public Class ctrlMyRadGridView

#Region "Local Variables"

    Private Const STANDART_COLUMN_WIDTH As Integer = 120

#End Region

#Region "Properties"

    Private _EnabledApplyOptions As Boolean = False
    ''' <summary>
    ''' Options ayarlar?n?n uygulanmas?n? ya da uygulanmamas?n? sa?lar.
    ''' </summary>
    ''' <value>True, aktifle?tirilen option'lar?n uygulanmas?n? sa?lar.
    ''' False, option aktifli?ini etkilemeden ayarlar?n uygulanmamas?n? sa?lar.</value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property EnabledApplyOptions() As Boolean
        Get
            Return Me._EnabledApplyOptions
        End Get
        Set(ByVal value As Boolean)
            Me._EnabledApplyOptions = value
        End Set
    End Property

    Private _EnabledApplyAllOptions As Boolean = False
    ''' <summary>
    ''' T?m Options'lar?n aktifle?mesini ya da pasifle?mesini sa?lar.
    ''' (Enabled = True)
    ''' </summary>
    ''' <value>True, t?m option'lar?n "Enabled" ?zelli?ini aktifle?tirir.
    ''' False, t?m option'lar?n "Enabled" ?zelli?ini pasifle?tirir.</value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property EnabledApplyAllOptions() As Boolean
        Get
            Return Me._EnabledApplyAllOptions
        End Get
        Set(ByVal value As Boolean)
            Me._EnabledApplyAllOptions = value

            Me.EnabledPinnedColumnOption = value
            Me.EnabledTypedColumnOption = value
            Me.EnabledVisibilityColumnOption = value
            Me.EnabledHeaderColumnOption = value
            Me.EnabledMiddleAlignmentColumnOption = value
            Me.EnabledStandartColumnWidthOption = value
        End Set
    End Property

    Private _EnabledPinnedColumnOption As Boolean = False
    Public Property EnabledPinnedColumnOption() As Boolean
        Get
            Return Me._EnabledPinnedColumnOption
        End Get
        Set(ByVal value As Boolean)
            Me._EnabledPinnedColumnOption = value

            If (value AndAlso Me.PinnedColumnList Is Nothing) Then
                Me.PinnedColumnList = New List(Of PinnedColumnOptionObject)
            Else
                Me.PinnedColumnList = Nothing
            End If
        End Set
    End Property

    Private _EnabledTypedColumnOption As Boolean = False
    Public Property EnabledTypedColumnOption() As Boolean
        Get
            Return Me._EnabledTypedColumnOption
        End Get
        Set(ByVal value As Boolean)
            Me._EnabledTypedColumnOption = value

            If (value AndAlso Me.TypedColumnList Is Nothing) Then
                Me.TypedColumnList = New List(Of TypedColumnOptionObject)
            Else
                Me.TypedColumnList = Nothing
            End If
        End Set
    End Property

    Private _EnabledVisibilityColumnOption As Boolean = False
    Public Property EnabledVisibilityColumnOption() As Boolean
        Get
            Return Me._EnabledVisibilityColumnOption
        End Get
        Set(ByVal value As Boolean)
            Me._EnabledVisibilityColumnOption = value

            If (value AndAlso Me.VisibilityColumnList Is Nothing) Then
                Me.VisibilityColumnList = New List(Of VisibleColumnOptionObject)
            Else
                Me.VisibilityColumnList = Nothing
            End If
        End Set
    End Property

    Private _EnabledHeaderColumnOption As Boolean = False
    Public Property EnabledHeaderColumnOption() As Boolean
        Get
            Return Me._EnabledHeaderColumnOption
        End Get
        Set(ByVal value As Boolean)
            Me._EnabledHeaderColumnOption = value

            If (value AndAlso Me.HeaderColumnList Is Nothing) Then
                Me.HeaderColumnList = New List(Of HeaderColumnOptionObject)
            Else
                Me.HeaderColumnList = Nothing
            End If
        End Set
    End Property

    Private _EnabledMiddleAlignmentColumnOption As Boolean = False
    Public Property EnabledMiddleAlignmentColumnOption() As Boolean
        Get
            Return Me._EnabledMiddleAlignmentColumnOption
        End Get
        Set(ByVal value As Boolean)
            Me._EnabledMiddleAlignmentColumnOption = value
        End Set
    End Property

    Private _EnabledStandartColumnWidthOption As Boolean = False
    Public Property EnabledStandartColumnWidthOption() As Boolean
        Get
            Return Me._EnabledStandartColumnWidthOption
        End Get
        Set(ByVal value As Boolean)
            Me._EnabledStandartColumnWidthOption = value
        End Set
    End Property

    Private _PinnedColumnList As List(Of PinnedColumnOptionObject)
    Public Property PinnedColumnList() As List(Of PinnedColumnOptionObject)
        Get
            Return Me._PinnedColumnList
        End Get
        Set(ByVal value As List(Of PinnedColumnOptionObject))
            If (Me._PinnedColumnList Is Nothing) Then _
                Me._PinnedColumnList = New List(Of PinnedColumnOptionObject)

            Me._PinnedColumnList = value
        End Set
    End Property

    Private _TypedColumnList As List(Of TypedColumnOptionObject)
    Public Property TypedColumnList() As List(Of TypedColumnOptionObject)
        Get
            Return Me._TypedColumnList
        End Get
        Set(ByVal value As List(Of TypedColumnOptionObject))
            If (Me._TypedColumnList Is Nothing) Then _
                Me._TypedColumnList = New List(Of TypedColumnOptionObject)
            Me._TypedColumnList = value
        End Set
    End Property

    Private _VisibilityColumnList As List(Of VisibleColumnOptionObject)
    Public Property VisibilityColumnList() As List(Of VisibleColumnOptionObject)
        Get
            Return Me._VisibilityColumnList
        End Get
        Set(ByVal value As List(Of VisibleColumnOptionObject))
            If (Me._VisibilityColumnList Is Nothing) Then _
                Me._VisibilityColumnList = New List(Of VisibleColumnOptionObject)

            Me._VisibilityColumnList = value
        End Set
    End Property

    Private _HeaderColumnList As List(Of HeaderColumnOptionObject)
    Public Property HeaderColumnList() As List(Of HeaderColumnOptionObject)
        Get
            Return Me._HeaderColumnList
        End Get
        Set(ByVal value As List(Of HeaderColumnOptionObject))
            If (Me._HeaderColumnList Is Nothing) Then _
                Me._HeaderColumnList = New List(Of HeaderColumnOptionObject)

            Me._HeaderColumnList = value
        End Set
    End Property

    Private _CustomColumnWidth As Integer = -1
    Public Property CustomColumnWidth() As Integer
        Get
            Return Me._CustomColumnWidth
        End Get
        Set(ByVal value As Integer)
            Me._CustomColumnWidth = value
        End Set
    End Property

    Private _ExportExcelSheetName As String
    Public Property ExportExcelSheetName() As String
        Get
            Return _ExportExcelSheetName
        End Get
        Set(ByVal value As String)
            _ExportExcelSheetName = value
        End Set
    End Property

    Private _ExportExcelFileName As String
    Public Property ExportExcelFileName() As String
        Get
            Return _ExportExcelFileName
        End Get
        Set(ByVal value As String)
            _ExportExcelFileName = value
        End Set
    End Property

#End Region

#Region "Public Methods"
    Public Sub WriteGridViewRowCount()
        Try
            If (Me.grvGridView.DataSource IsNot Nothing) Then

                Dim rowCountText As String = _
                    String.Format(Me.lblGridViewRowCountInfo.Tag.ToString(), _
                    Me.grvGridView.RowCount.ToString())

                Me.lblGridViewRowCountInfo.Text = rowCountText

            Else

                Me.lblGridViewRowCountInfo.Text = String.Empty

            End If

        Catch ex As Exception
            MsgBox(ex.Messages)
        End Try
    End Sub
    Public Sub ExportExcel()
        Me.ExportExcel(Me.ExportExcelSheetName, Me.ExportExcelFileName, "Excel'e Aktar", False, Telerik.WinControls.UI.Export.HiddenOption.DoNotExport, Me.grvGridView)
    End Sub
    Public Sub ExportWord()
        '' Not Implemented..
    End Sub
#End Region

#Region "Private Methods"

    Private Sub ApplyPinnedColumnOption()
        If (Me.EnabledPinnedColumnOption AndAlso Me.PinnedColumnList IsNot Nothing) Then

            If (Me.grvGridView.DataSource IsNot Nothing AndAlso _
                    Me.grvGridView.MasterGridViewTemplate.Columns IsNot Nothing AndAlso _
                    Me.grvGridView.MasterGridViewTemplate.Columns.Count > 0) Then

                If (Me.PinnedColumnList IsNot Nothing) Then

                    For Each optionObj As PinnedColumnOptionObject In Me.PinnedColumnList
                        If (Me.grvGridView.MasterGridViewTemplate.Columns(optionObj.ColumnName) IsNot Nothing) Then _
                            Me.grvGridView.MasterGridViewTemplate.Columns(optionObj.ColumnName).IsPinned = optionObj.IsPinned
                    Next

                End If

            End If

        End If
    End Sub
    Private Sub ApplyTypedColumnOption()
        If (Me.EnabledTypedColumnOption AndAlso Me.TypedColumnList IsNot Nothing) Then

            If (Me.grvGridView.DataSource IsNot Nothing AndAlso _
                    Me.grvGridView.MasterGridViewTemplate.Columns IsNot Nothing AndAlso _
                    Me.grvGridView.MasterGridViewTemplate.Columns.Count > 0) Then

                If (Me.TypedColumnList IsNot Nothing) Then

                    For Each optionObj As TypedColumnOptionObject In Me.TypedColumnList
                        If (Me.grvGridView.MasterGridViewTemplate.Columns(optionObj.ColumnName) IsNot Nothing) Then _
                            Me.grvGridView.MasterGridViewTemplate.Columns(optionObj.ColumnName).DataType = optionObj.ColumnType
                    Next

                End If

            End If

        End If
    End Sub
    Private Sub ApplyVisibleColumnOption()
        If (Me.EnabledVisibilityColumnOption AndAlso Me.VisibilityColumnList IsNot Nothing) Then

            If (Me.grvGridView.DataSource IsNot Nothing AndAlso _
                    Me.grvGridView.MasterGridViewTemplate.Columns IsNot Nothing AndAlso _
                    Me.grvGridView.MasterGridViewTemplate.Columns.Count > 0) Then

                If (Me.VisibilityColumnList IsNot Nothing) Then

                    For Each optionObj As VisibleColumnOptionObject In Me.VisibilityColumnList
                        If (Me.grvGridView.MasterGridViewTemplate.Columns(optionObj.ColumnName) IsNot Nothing) Then _
                            Me.grvGridView.MasterGridViewTemplate.Columns(optionObj.ColumnName).IsVisible = optionObj.IsVisible
                    Next

                End If

            End If

        End If
    End Sub
    Private Sub ApplyHeaderColumnOption()
        If (Me.EnabledHeaderColumnOption AndAlso Me.HeaderColumnList IsNot Nothing) Then

            If (Me.grvGridView.DataSource IsNot Nothing AndAlso _
                    Me.grvGridView.MasterGridViewTemplate.Columns IsNot Nothing AndAlso _
                    Me.grvGridView.MasterGridViewTemplate.Columns.Count > 0) Then

                If (Me.HeaderColumnList IsNot Nothing) Then

                    For Each optionObj As HeaderColumnOptionObject In Me.HeaderColumnList
                        If (Me.grvGridView.MasterGridViewTemplate.Columns(optionObj.ColumnName) IsNot Nothing) Then _
                            Me.grvGridView.MasterGridViewTemplate.Columns(optionObj.ColumnName).HeaderText = optionObj.ColumnNewName
                    Next

                End If

            End If

        End If
    End Sub
    Private Sub ApplyMiddleAlignmentColumnOption()
        If (Me.EnabledMiddleAlignmentColumnOption) Then
            For Each dc As Telerik.WinControls.UI.GridViewDataColumn In Me.grvGridView.MasterGridViewTemplate.Columns
                dc.TextAlignment = Drawing.ContentAlignment.MiddleCenter
            Next
        End If
    End Sub
    Private Sub ApplyColumnWidthOption()
        If (Me.EnabledStandartColumnWidthOption) Then
            For Each dc As Telerik.WinControls.UI.GridViewDataColumn In Me.grvGridView.MasterGridViewTemplate.Columns
                dc.Width = STANDART_COLUMN_WIDTH
            Next
        Else
            If (Me.CustomColumnWidth <> -1) Then
                For Each dc As Telerik.WinControls.UI.GridViewDataColumn In Me.grvGridView.MasterGridViewTemplate.Columns
                    dc.Width = Me.CustomColumnWidth
                Next
            End If
        End If
    End Sub

    Private Sub ApplyOptions()
        If (Me.EnabledApplyOptions) Then
            Me.ApplyMiddleAlignmentColumnOption()
            Me.ApplyPinnedColumnOption()
            Me.ApplyVisibleColumnOption()
            Me.ApplyHeaderColumnOption()
            Me.ApplyColumnWidthOption()
            Me.ApplyTypedColumnOption()
        End If
    End Sub

    ''' <summary>
    ''' Excel Export Metodu.
    ''' </summary>
    ''' <param name="sheetName">Excel sheet ismi.</param>
    ''' <param name="fileName">Excel dosya ismi.</param>
    ''' <param name="saveFileDialogTitle">Dosya kaydetme kutucu?u ba?l???.</param>
    ''' <param name="exportVisualSettings">G?rsel ayarlar? uygula.</param>
    ''' <param name="gridView">Export edilecek RadGridView.</param>
    ''' <returns>True d?nerse, export ba?ar?l?; aksi halde export ba?ar?s?z.</returns>
    ''' <remarks></remarks>
    Private Function ExportExcel(ByVal sheetName As String, ByVal fileName As String, ByVal saveFileDialogTitle As String, ByVal exportVisualSettings As Boolean, ByVal hiddenOption As Telerik.WinControls.UI.Export.HiddenOption, ByVal gridView As Telerik.WinControls.UI.RadGridView) As Boolean

        Dim result As Boolean = False
        Dim exporter As Telerik.WinControls.UI.Export.ExportToExcelML

        Dim sfd As System.Windows.Forms.SaveFileDialog = _
           New System.Windows.Forms.SaveFileDialog()

        sfd.Title = saveFileDialogTitle
        sfd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        sfd.Filter = "Microsoft Office Excel 97-2003(*.xls)|*.xls|Microsoft Office Excel 2007(*.xlsx)|*.xlsx"
        sfd.FilterIndex = 1
        sfd.FileName = fileName

        Try
            If (sfd.ShowDialog() = Windows.Forms.DialogResult.OK) Then

                exporter = New Telerik.WinControls.UI.Export.ExportToExcelML(gridView)
                exporter.HiddenColumnOption = hiddenOption
                exporter.SheetName = sheetName
                exporter.ExportVisualSettings = exportVisualSettings
                exporter.SheetMaxRows = Telerik.WinControls.UI.Export.ExcelMaxRows._1048576
                exporter.RadGridViewToExport = gridView
                exporter.RunExport(sfd.FileName)

                System.Windows.Forms.MessageBox.Show("Excel Dosya Kaydedildi..", "Dosya Kaydedildi", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Information, Windows.Forms.MessageBoxDefaultButton.Button1)
                result = True
            End If

        Catch ex As Exception

            System.Windows.Forms.MessageBox.Show("Excel Dosya Kaydedilemedi..", "Dosya Kaydedilemedi", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Error, Windows.Forms.MessageBoxDefaultButton.Button1)

        End Try

        Return result
    End Function

#End Region

#Region "Events"

    Private Sub grvGridView_DataBindingComplete(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.GridViewBindingCompleteEventArgs) Handles grvGridView.DataBindingComplete

        Me.WriteGridViewRowCount()
        Me.ApplyOptions()

    End Sub
    Private Sub grvGridView_FilterChanged(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCollectionChangedEventArgs) Handles grvGridView.FilterChanged
        Me.WriteGridViewRowCount()
    End Sub
    Private Sub cmdExcelExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExcelExport.Click
        Me.ExportExcel()
    End Sub

#End Region

End Class

Public Class TypedColumnOptionObject

#Region "Local Variables"
    Private _ColumnName As String
    Private _ColumnType As System.Type
#End Region

#Region "Properties"
    Public Property ColumnName() As String
        Get
            Return Me._ColumnName
        End Get
        Set(ByVal value As String)
            Me._ColumnName = value
        End Set
    End Property
    Public Property ColumnType() As System.Type
        Get
            Return Me._ColumnType
        End Get
        Set(ByVal value As System.Type)
            Me._ColumnType = value
        End Set
    End Property
#End Region

#Region "Constructors"
    Public Sub New()

    End Sub
    Public Sub New(ByVal pColumnName As String, ByVal pColumnType As System.Type)
        Me.ColumnName = pColumnName
        Me.ColumnType = pColumnType
    End Sub
#End Region

#Region "Overrides Functions"
    Public Overrides Function ToString() As String
        Return Me.ColumnName
    End Function
#End Region

End Class
Public Class VisibleColumnOptionObject

#Region "Local Variables"
    Private _ColumnName As String
    Private _IsVisible As Boolean
#End Region

#Region "Properties"
    Public Property ColumnName() As String
        Get
            Return Me._ColumnName
        End Get
        Set(ByVal value As String)
            Me._ColumnName = value
        End Set
    End Property
    Public Property IsVisible() As Boolean
        Get
            Return Me._IsVisible
        End Get
        Set(ByVal value As Boolean)
            Me._IsVisible = value
        End Set
    End Property
#End Region

#Region "Constructors"
    Public Sub New()

    End Sub
    Public Sub New(ByVal pColumnName As String, ByVal pIsVisible As Boolean)
        Me.ColumnName = pColumnName
        Me.IsVisible = pIsVisible
    End Sub
#End Region

#Region "Overrides Functions"
    Public Overrides Function ToString() As String
        Return Me.ColumnName
    End Function
#End Region

End Class
Public Class HeaderColumnOptionObject

#Region "Local Variables"
    Private _ColumnName As String
    Private _ColumnNewName As String
#End Region

#Region "Properties"
    Public Property ColumnName() As String
        Get
            Return Me._ColumnName
        End Get
        Set(ByVal value As String)
            Me._ColumnName = value
        End Set
    End Property
    Public Property ColumnNewName() As String
        Get
            Return Me._ColumnNewName
        End Get
        Set(ByVal value As String)
            Me._ColumnNewName = value
        End Set
    End Property
#End Region

#Region "Constructors"
    Public Sub New()

    End Sub
    Public Sub New(ByVal pColumnName As String, ByVal pColumnNewName As String)
        Me.ColumnName = pColumnName
        Me.ColumnNewName = pColumnNewName
    End Sub
#End Region

#Region "Overrides Functions"
    Public Overrides Function ToString() As String
        Return Me.ColumnName
    End Function
#End Region

End Class
Public Class PinnedColumnOptionObject

#Region "Local Variables"
    Private _ColumnName As String
    Private _IsPinned As Boolean
#End Region

#Region "Properties"
    Public Property ColumnName() As String
        Get
            Return Me._ColumnName
        End Get
        Set(ByVal value As String)
            Me._ColumnName = value
        End Set
    End Property
    Public Property IsPinned() As String
        Get
            Return Me._IsPinned
        End Get
        Set(ByVal value As String)
            Me._IsPinned = value
        End Set
    End Property
#End Region

#Region "Constructors"
    Public Sub New()

    End Sub
    Public Sub New(ByVal pColumnName As String, ByVal pIsPinned As Boolean)
        Me.ColumnName = pColumnName
        Me.IsPinned = pIsPinned
    End Sub
#End Region

#Region "Overrides Functions"
    Public Overrides Function ToString() As String
        Return Me.ColumnName
    End Function
#End Region

End Class

Resim gördüğünüz buton için Excel ikonu.. Excel-16x16

Projenizi derledikten sonra aşağıdaki kodu örnek bir form’a kopyalarsanız bir test sürüşü yapabilirsiniz..

 

Public Class RadForm1

    Private Sub RadForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '' Creating UserConntrol..
        '' =====================================================================
        Dim ctrl As New ctrlKocRadGridView
        ctrl.Dock = DockStyle.Fill
        Me.Controls.Add(ctrl)

        '' Grid Settings..
        '' =====================================================================
        ctrl.EnabledApplyAllOptions = True
        ctrl.EnabledApplyOptions = True

        ''ctrl.EnabledApplyOptions = True 'A?a??daki se?enekleri topluca a?ar.. (Tek tek a??labilir de)
        ''=============================================================================================================
        ''
        'ctrl.EnabledStandartColumnWidthOption = True       'STANDART WIDTH : 120
        'ctrl.CustomColumnWidth = 150                       'CUSTOM olarak da de?er verilebilir.
        'ctrl.EnabledMiddleAlignmentColumnOption = True     'ALIGNMENT : MIDDLE
        'ctrl.EnabledHeaderColumnOption = True
        'ctrl.EnabledPinnedColumnOption = True
        'ctrl.EnabledVisibilityColumnOption = True
        'ctrl.EnabledTypedColumnOption = True

        'Pinned kolonlar set ediliyor..
        'If (ctrl.PinnedColumnList Is Nothing) Then ctrl.PinnedColumnList = New List(Of PinnedColumnOptionObject)
        'ctrl.PinnedColumnList.AddRange(New PinnedColumnOptionObject() _
        '{ _
        '    New PinnedColumnOptionObject("fld_Ad", True), _
        '    New PinnedColumnOptionObject("fld_Soyad", True) _
        '})

        'Invisible kolonlar set ediliyor..
        If (ctrl.VisibilityColumnList Is Nothing) Then ctrl.VisibilityColumnList = New List(Of VisibleColumnOptionObject)
        ctrl.VisibilityColumnList.AddRange(New VisibleColumnOptionObject() _
        { _
            New VisibleColumnOptionObject("Column1", True), _
            New VisibleColumnOptionObject("Column2", False) _
        })

        'Kolon ba?l?klar? set ediliyor..
        If (ctrl.HeaderColumnList Is Nothing) Then ctrl.HeaderColumnList = New List(Of HeaderColumnOptionObject)
        ctrl.HeaderColumnList.AddRange(New HeaderColumnOptionObject() _
        { _
            New HeaderColumnOptionObject("Column1", "Kolon – 1"), _
            New HeaderColumnOptionObject("Column2", "Kolon – 2") _
        })

        'Kolon tipi ayar? set ediliyor..
        If (ctrl.TypedColumnList Is Nothing) Then ctrl.TypedColumnList = New List(Of TypedColumnOptionObject)

        ctrl.TypedColumnList.Add( _
            New TypedColumnOptionObject( _
            "Column3", GetType(Integer)))

        '' DataTable Binding..
        '' =====================================================================
        Dim dt As New DataTable

        dt.Columns.AddRange( _
            New DataColumn() { _
                New DataColumn("Column1"), _
                New DataColumn("Column2"), _
                New DataColumn("Column3") _
                })

        Dim rnd As New Random

        For i As Integer = 1 To 10

            Dim dr As DataRow = dt.NewRow

            For k As Integer = 0 To 2

                dr(k) = rnd.Next(0, 100)

            Next

            dt.Rows.Add(dr)

        Next

        ctrl.grvGridView.DataSource = dt

    End Sub
End Class

C# – User Control To XML – Part – 1

Bu görsel derste;

Bir Container(Panel) içine kendi oluşturduğumuz UserControllerin Run-time ‘da dinamik olarak eklenmesini, ardından bir tıklama ile container içine eklediğimiz UserControllerin XML bir dosyaya kayedilmesini sağlayacağız. Ardından bu dosyadan okuyarak UserControlleri container içine tekrardan oluşturacağız.. Son olarak’da container içinde inşaa ettiğimiz UserControllerimizin container dışındaki bir kontrole nasıl erişbileceğini göstereceğim.

Tüm bu işlemleri yaparken, form arka planına kod yazımını en az düzeyde tutup, UserController ve container(Panel) içinde metod tanımlayarak bu kısımlarda kodu oluşturacağız. Böylece daha sade ve temiz bir kodlama ile modüler bir yapı oluşturmuş olacağız. Bu yapı sayesinde istediğimiz zaman projeye Usercontrol ekleyerek projenin daha temiz ve karmaşıklaşmadan genişlemesini sağlayacağız. Ayrıca modifikasyona açık bir yapı oluşturmuş olacağız.

3 bölüm şeklinde yayınlayacağım bu konuyu severek izleyeceğinizi umuyorum.. 🙂
3. bölümden sonraki makalemde pojenin kodunu bulabilirsiniz. Ayrıca projeyide indirebilirsiniz..

http://content.screencast.com/users/MaviSeffaf/folders/Default/media/6ce6c81f-d09c-4e40-9a0c-e9b4a21503c7/bootstrap.swf

Videoyu indir!

C# – User Control To XML – Part – 2

User Control To Xml görsel dersimizin 2. bölümünde;
Oluşturduğumuz user controlleri XML olarak kaydetmek ve XML’den okuyarak tekrar listelenmesini sağlıyor olacağız.. 1 saatlik bir video olduğu için 3 parça olarak yayınlıyorum . 🙂

http://content.screencast.com/users/MaviSeffaf/folders/Default/media/a7a0e6d3-ac64-447d-8f4e-760232cf20d1/UserControlToXml-Part2_1.avi
Videoyu indir!

http://content.screencast.com/users/MaviSeffaf/folders/Default/media/82b7435c-6399-4346-868b-4b395f2116d5/UserControlToXml-Part2_2.avi
Videoyu indir!

http://content.screencast.com/users/MaviSeffaf/folders/Default/media/f956c228-2184-4ebb-b7cb-a39250d5f178/UserControlToXml-Part2_3.avi
Videoyu indir!

C# – User Control To XML – Part – 3

UserControl To Xml Bölüm 3 görsel dersinde;
Run-time’da oluşturduğumuz UserControl’lerimizin içinde bulundukları Container Panel dışında bir kontrole erişimlerini nasıl sağladığımızı görebilirsiniz.. Mümkün oldukça Form.cs class’ına kodlama yapmadan erişim sağlamanın yöntemini geliştiriyoruz..

http://content.screencast.com/users/MaviSeffaf/folders/Default/media/3ddfadd9-ecc0-46bd-a6b5-402c0d6f53be/bootstrap.swf

Videoyu indir!

Proje dosyası :
UserControlToXml.rar

C# – User Control To XML – Kodu

UserControl To XML isimli Part – 1 , Part – 2 , Part – 3 bölümlerindeki projenin kodlarını aşağıda bulabilirsiniz.. Designer.cs class kodları ile beraber kullanabilirsiniz..

UserControl1.Designer.cs

UserControl1.Designer.cs


namespace UserControlsToXml.UserControls

{

    partial class UserControl1

    {

        ///

        /// Required designer variable.

        ///

        private System.ComponentModel.IContainer components = null;

 

        ///

        /// Clean up any resources being used.

        ///

        /// true if managed resources should be disposed; otherwise, false.

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

 

        #region Component Designer generated code

 

        ///

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        ///

        private void InitializeComponent()

        {

            this.button1 = new System.Windows.Forms.Button();

            this.label1 = new System.Windows.Forms.Label();

            this.textBox1 = new System.Windows.Forms.TextBox();

            this.SuspendLayout();

            //

            // button1

            //

            this.button1.Dock = System.Windows.Forms.DockStyle.Right;

            this.button1.Location = new System.Drawing.Point(301, 0);

            this.button1.Name = "button1";

            this.button1.Size = new System.Drawing.Size(75, 20);

            this.button1.TabIndex = 0;

            this.button1.Text = "button1";

            this.button1.UseVisualStyleBackColor = true;

            this.button1.Click += new System.EventHandler(this.button1_Click);

            //

            // label1

            //

            this.label1.Dock = System.Windows.Forms.DockStyle.Left;

            this.label1.Location = new System.Drawing.Point(0, 0);

            this.label1.Name = "label1";

            this.label1.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);

            this.label1.Size = new System.Drawing.Size(109, 20);

            this.label1.TabIndex = 1;

            this.label1.Text = "label1";

            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

            //

            // textBox1

            //

            this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;

            this.textBox1.Location = new System.Drawing.Point(109, 0);

            this.textBox1.Name = "textBox1";

            this.textBox1.Size = new System.Drawing.Size(192, 20);

            this.textBox1.TabIndex = 2;

            //

            // UserControl1

            //

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.Controls.Add(this.textBox1);

            this.Controls.Add(this.label1);

            this.Controls.Add(this.button1);

            this.Name = "UserControl1";

            this.Size = new System.Drawing.Size(376, 20);

            this.ResumeLayout(false);

            this.PerformLayout();

 

        }

 

        #endregion

 

        public System.Windows.Forms.Button button1;

        public System.Windows.Forms.Label label1;

        public System.Windows.Forms.TextBox textBox1;

 

    }

}


UserControl1.cs


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Xml.Linq;

 

namespace UserControlsToXml.UserControls

{

    public partial class UserControl1 : UserControl, IMyUserControl

    {

        private Random rnd = new Random();

        private ComboBox _MyCombobox = null;

 

        public UserControl1()

        {

            InitializeComponent();

 

            this.label1.Text = "Label-" + rnd.Next(0, 100).ToString();

            this.textBox1.Text = "Textbox-" + rnd.Next(0, 100).ToString();

            this.button1.Text = "Button-" + rnd.Next(0, 100).ToString();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            MessageBox.Show(this.textBox1.Text);

        }

        private void _MyCombobox_SelectedIndexChanged(object sender, EventArgs e)

        {

            if (this.MyCombobox.SelectedItem != null)

                this.textBox1.Text = this.MyCombobox.SelectedItem.ToString();

        }

 

        #region IMyUserControl Members

 

        public System.Xml.Linq.XElement GetXml()

        {

            XElement xe = new XElement("control",

                new XAttribute("label", this.label1.Text),

                new XAttribute("textbox", this.textBox1.Text),

                new XAttribute("button", this.button1.Text),

                new XAttribute("type", this.GetType().ToString()));

 

            return xe;

        }

        public Control GetControlInstance(XElement xe)

        {

            string labelStr = xe.Attribute("label").Value;

            string textboxStr = xe.Attribute("textbox").Value;

            string buttonStr = xe.Attribute("button").Value;

 

            this.label1.Text = labelStr;

            this.textBox1.Text = textboxStr;

            this.button1.Text = buttonStr;

 

            return this;

        }

        public ComboBox MyCombobox

        {

            get

            {

                return this._MyCombobox;

            }

            set

            {

                this._MyCombobox = value;

 

                if(this._MyCombobox != null)

                    this._MyCombobox.SelectedIndexChanged += new EventHandler(_MyCombobox_SelectedIndexChanged);

            }

        }

 

        #endregion

    }

}


UserControl2.Designer.cs


namespace UserControlsToXml.UserControls

{

    partial class UserControl2

    {

        ///

        /// Required designer variable.

        ///

        private System.ComponentModel.IContainer components = null;

 

        ///

        /// Clean up any resources being used.

        ///

        /// true if managed resources should be disposed; otherwise, false.

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

 

        #region Component Designer generated code

 

        ///

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        ///

        private void InitializeComponent()

        {

            this.label1 = new System.Windows.Forms.Label();

            this.button1 = new System.Windows.Forms.Button();

            this.SuspendLayout();

            //

            // label1

            //

            this.label1.Dock = System.Windows.Forms.DockStyle.Fill;

            this.label1.Location = new System.Drawing.Point(0, 0);

            this.label1.Name = "label1";

            this.label1.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);

            this.label1.Size = new System.Drawing.Size(301, 20);

            this.label1.TabIndex = 4;

            this.label1.Text = "label1";

            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

            //

            // button1

            //

            this.button1.Dock = System.Windows.Forms.DockStyle.Right;

            this.button1.Location = new System.Drawing.Point(301, 0);

            this.button1.Name = "button1";

            this.button1.Size = new System.Drawing.Size(75, 20);

            this.button1.TabIndex = 3;

            this.button1.Text = "button1";

            this.button1.UseVisualStyleBackColor = true;

            this.button1.Click += new System.EventHandler(this.button1_Click);

            //

            // UserControl2

            //

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.Controls.Add(this.label1);

            this.Controls.Add(this.button1);

            this.Name = "UserControl2";

            this.Size = new System.Drawing.Size(376, 20);

            this.ResumeLayout(false);

 

        }

 

        #endregion

 

        public System.Windows.Forms.Label label1;

        public System.Windows.Forms.Button button1;

 

    }

}


UserControl2.cs


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Xml.Linq;

 

namespace UserControlsToXml.UserControls

{

    public partial class UserControl2 : UserControl, IMyUserControl

    {

        private Random rnd = new Random();

        private ComboBox _MyCombobox = null;

 

        public UserControl2()

        {

            InitializeComponent();

 

            this.label1.Text = "Label-" + rnd.Next(0, 100).ToString();

            this.button1.Text = "Button-" + rnd.Next(0, 100).ToString();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            MessageBox.Show(this.label1.Text);

        }

        private void _MyCombobox_SelectedIndexChanged(object sender, EventArgs e)

        {

            if (this.MyCombobox.SelectedItem != null)

                this.label1.Text = this.MyCombobox.SelectedItem.ToString();

        }

 

 

        #region IMyUserControl Members

 

        public System.Xml.Linq.XElement GetXml()

        {

            XElement xe = new XElement("control",

                 new XAttribute("label", this.label1.Text),

                 new XAttribute("button", this.button1.Text),

                 new XAttribute("type", this.GetType().ToString()));

 

            return xe;

        }

        public Control GetControlInstance(XElement xe)

        {

            UserControl2 uc2 = new UserControl2();

 

            string labelStr = xe.Attribute("label").Value;

            string buttonStr = xe.Attribute("button").Value;

 

            uc2.label1.Text = labelStr;

            uc2.button1.Text = buttonStr;

 

            return uc2;

        }

        public ComboBox MyCombobox

        {

            get

            {

                return this._MyCombobox;

            }

            set

            {

                this._MyCombobox = value;

 

                if (this._MyCombobox != null)

                    this._MyCombobox.SelectedIndexChanged += new EventHandler(_MyCombobox_SelectedIndexChanged);

            }

        }

 

        #endregion

    }

}


MyContainerPanel.cs


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Xml.Linq;

 

namespace UserControlsToXml

{

    public class MyContainerPanel : System.Windows.Forms.Panel

    {

        private int y = 0;

        //public ComboBox CMB { get; set; }

 

 

        private object CreateObjectInstance(string typeName)

        {

            Type typ = Type.GetType(typeName);

            object obj = Activator.CreateInstance(typ);

 

            //Form'daki combobox nesnesi oluşturulan UserControl'e bağlanır..

            //((IMyUserControl)obj).MyCombobox = this.CMB;

 

            return obj;

        }

 

 

        public void AddMyUserControlToPanel(string typeName)

        {

            object obj = this.CreateObjectInstance(typeName);

            Control ctrl = (Control)obj;

 

            this.AddControlToPanel(ctrl);

        }

        public void AddControlToPanel(Control ctrl)

        {

            if (ctrl != null)

            {

                ctrl.Top = y;

                this.Controls.Add(ctrl);

                y += 20;

            }

        }

 

 

        public void CreateControlXmlFile(string path)

        {

            XDocument xDoc = new XDocument(

                    new XDeclaration("1.0", "ISO-8859-9", null));

 

            XElement root = new XElement("controls");

 

            foreach (Control ctrl in this.Controls)

            {

                XElement xe = ((IMyUserControl)ctrl).GetXml();

                root.Add(xe);

            }

 

            xDoc.Add(root);

            xDoc.Save(path);

            xDoc = null;

        }

        public void ReadControlXmlFile(string path)

        {

            XDocument xDoc = XDocument.Load(path);

            XElement root = xDoc.Root;

 

            this.Controls.Clear();

            this.y = 0;

 

            foreach (XElement xe in root.Elements())

            {

                string typeName = xe.Attribute("type").Value;

                object obj = this.CreateObjectInstance(typeName);

 

                Control ctrl = ((IMyUserControl)obj).GetControlInstance(xe);

 

                this.AddControlToPanel(ctrl);

            }

        }

    }

}


IMyUserControl.cs


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml.Linq;

using System.Windows.Forms;

 

namespace UserControlsToXml

{

    public interface IMyUserControl

    {

        XElement GetXml();

        Control GetControlInstance(XElement xe);

 

        ComboBox MyCombobox { get; set; }

    }

}


Form1.Designer.cs


namespace UserControlsToXml

{

    partial class Form1

    {

        ///

        /// Required designer variable.

        ///

        private System.ComponentModel.IContainer components = null;

 

        ///

        /// Clean up any resources being used.

        ///

        /// true if managed resources should be disposed; otherwise, false.

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

 

        #region Windows Form Designer generated code

 

        ///

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        ///

        private void InitializeComponent()

        {

            this.splitContainer1 = new System.Windows.Forms.SplitContainer();

            this.button2 = new System.Windows.Forms.Button();

            this.button1 = new System.Windows.Forms.Button();

            this.listBox1 = new System.Windows.Forms.ListBox();

            this.myContainerPanel1 = new UserControlsToXml.MyContainerPanel();

            this.comboBox1 = new System.Windows.Forms.ComboBox();

            this.splitContainer1.Panel1.SuspendLayout();

            this.splitContainer1.Panel2.SuspendLayout();

            this.splitContainer1.SuspendLayout();

            this.SuspendLayout();

            //

            // splitContainer1

            //

            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;

            this.splitContainer1.Location = new System.Drawing.Point(0, 0);

            this.splitContainer1.Name = "splitContainer1";

            //

            // splitContainer1.Panel1

            //

            this.splitContainer1.Panel1.Controls.Add(this.button2);

            this.splitContainer1.Panel1.Controls.Add(this.button1);

            this.splitContainer1.Panel1.Controls.Add(this.listBox1);

            //

            // splitContainer1.Panel2

            //

            this.splitContainer1.Panel2.Controls.Add(this.myContainerPanel1);

            this.splitContainer1.Panel2.Controls.Add(this.comboBox1);

            this.splitContainer1.Size = new System.Drawing.Size(570, 310);

            this.splitContainer1.SplitterDistance = 190;

            this.splitContainer1.TabIndex = 0;

            //

            // button2

            //

            this.button2.Location = new System.Drawing.Point(31, 275);

            this.button2.Name = "button2";

            this.button2.Size = new System.Drawing.Size(75, 23);

            this.button2.TabIndex = 2;

            this.button2.Text = "XML Oku";

            this.button2.UseVisualStyleBackColor = true;

            this.button2.Click += new System.EventHandler(this.button2_Click);

            //

            // button1

            //

            this.button1.Location = new System.Drawing.Point(112, 275);

            this.button1.Name = "button1";

            this.button1.Size = new System.Drawing.Size(75, 23);

            this.button1.TabIndex = 1;

            this.button1.Text = "XML Yaz";

            this.button1.UseVisualStyleBackColor = true;

            this.button1.Click += new System.EventHandler(this.button1_Click);

            //

            // listBox1

            //

            this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill;

            this.listBox1.FormattingEnabled = true;

            this.listBox1.Location = new System.Drawing.Point(0, 0);

            this.listBox1.Name = "listBox1";

            this.listBox1.Size = new System.Drawing.Size(190, 303);

            this.listBox1.TabIndex = 0;

            this.listBox1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDoubleClick);

            //

            // myContainerPanel1

            //

            this.myContainerPanel1.BackColor = System.Drawing.Color.Silver;

            this.myContainerPanel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

            this.myContainerPanel1.Dock = System.Windows.Forms.DockStyle.Fill;

            this.myContainerPanel1.Location = new System.Drawing.Point(0, 21);

            this.myContainerPanel1.Name = "myContainerPanel1";

            this.myContainerPanel1.Size = new System.Drawing.Size(376, 289);

            this.myContainerPanel1.TabIndex = 1;

            this.myContainerPanel1.ControlAdded += new System.Windows.Forms.ControlEventHandler(this.myContainerPanel1_ControlAdded);

            //

            // comboBox1

            //

            this.comboBox1.BackColor = System.Drawing.Color.LightCoral;

            this.comboBox1.Dock = System.Windows.Forms.DockStyle.Top;

            this.comboBox1.FormattingEnabled = true;

            this.comboBox1.Items.AddRange(new object[] {

            "Test1",

            "Test2",

            "Test3"});

            this.comboBox1.Location = new System.Drawing.Point(0, 0);

            this.comboBox1.Name = "comboBox1";

            this.comboBox1.Size = new System.Drawing.Size(376, 21);

            this.comboBox1.TabIndex = 0;

            //

            // Form1

            //

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(570, 310);

            this.Controls.Add(this.splitContainer1);

            this.Name = "Form1";

            this.Text = "Form1";

            this.Load += new System.EventHandler(this.Form1_Load);

            this.splitContainer1.Panel1.ResumeLayout(false);

            this.splitContainer1.Panel2.ResumeLayout(false);

            this.splitContainer1.ResumeLayout(false);

            this.ResumeLayout(false);

 

        }

 

        #endregion

 

        private System.Windows.Forms.SplitContainer splitContainer1;

        private System.Windows.Forms.ListBox listBox1;

        private System.Windows.Forms.Button button1;

        private System.Windows.Forms.Button button2;

        private MyContainerPanel myContainerPanel1;

        private System.Windows.Forms.ComboBox comboBox1;

    }

}

 


Form1.cs


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Xml.Linq;

 

namespace UserControlsToXml

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

 

        private void Form1_Load(object sender, EventArgs e)

        {

            //this.myContainerPanel1.CMB = this.comboBox1;

 

            string[] userControlTypeList =

                new string[2]

                {

                    "UserControlsToXml.UserControls.UserControl1",

                    "UserControlsToXml.UserControls.UserControl2"

                };

 

            this.listBox1.Items.Clear();

 

            foreach (string userControlType in userControlTypeList)

            {

                this.listBox1.Items.Add(userControlType);

            }

        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)

        {

            if (this.listBox1.SelectedItem != null)

            {

                string typeName = this.listBox1.SelectedItem.ToString();

                this.myContainerPanel1.AddMyUserControlToPanel(typeName);

            }

        }

        private void button1_Click(object sender, EventArgs e)

        {

            this.myContainerPanel1.CreateControlXmlFile(Application.StartupPath + "\\" + "Test.xml");

        }

        private void button2_Click(object sender, EventArgs e)

        {

            this.myContainerPanel1.ReadControlXmlFile(Application.StartupPath + "\\" + "Test.xml");

        }

 

        private void myContainerPanel1_ControlAdded(object sender, ControlEventArgs e)

        {

            ((IMyUserControl)e.Control).MyCombobox = this.comboBox1;

        }

    }

}