Wednesday, May 9, 2012

Sample Code Using ListBox with VB.NET


Public Class MyListBox
    '1. Copy All Items From ListBOx1 to ListBox2
    Public Shared Sub CopyAllItems(ByRef ListBoxName1 As ListBox, ByRef ListBoxName2 As ListBox)
        If ListBoxName1.Items.Count > 0 Then
            For i As UShort = 0 To ListBoxName1.Items.Count - 1
                ListBoxName2.Items.Add(ListBoxName1.Items.Item(i))
            Next
        End If
    End Sub

    '2. Copy Selcted Items From ListBOx1 to ListBox2
    Public Shared Sub CopySelectedItems(ByRef ListBoxName1 As ListBox, ByRef ListBoxName2 As ListBox)
        If ListBoxName1.SelectedItems.Count > 0 Then
            For i As UShort = 0 To ListBoxName1.SelectedItems.Count - 1
                ListBoxName2.Items.Add(ListBoxName1.SelectedItems.Item(i))
            Next
        End If
    End Sub

    '3. Delete All Items in ListBox
    Public Shared Sub DeleteAllItems(ByRef ListBoxName As ListBox)
        ListBoxName.Items.Clear()
    End Sub

    '4. Delete All SelectedItems
    Public Shared Sub DeleteSelectedItems(ByRef ListBoxName As ListBox)
        Do Until ListBoxName.SelectedIndices.Count = 0
            ListBoxName.Items.RemoveAt(ListBoxName.SelectedIndex)
        Loop
    End Sub

    '5. Back Up ListBox to Object Collection
    Public Shared Sub Backup(ByRef ListBoxName As ListBox, ByRef ObjCol As Collection)
        'Clear the old value in ObjCol Collection.
        ObjCol.Clear()
        'Copy all items in Items collection of ListBoxName to ObjCol
        If ListBoxName.Items.Count > 0 Then
            For i As UShort = 0 To ListBoxName.Items.Count - 1
                ObjCol.Add(ListBoxName.Items.Item(i))
            Next
        End If
    End Sub

    '6. Restore ListBox from Object Collection
    Public Shared Function Restore(ByRef ListBoxName As ListBox, ByRef ObjCol As Collection) As Boolean
        If ObjCol IsNot Nothing Then
            'Delete All Item in ListBox
            DeleteAllItems(ListBoxName)
            'Restore all items in ListBoxName from ObjCol
            If ObjCol.Count > 0 Then
                For i As UShort = 1 To ObjCol.Count
                    ListBoxName.Items.Add(ObjCol.Item(i))
                Next
            End If
            Return True
        Else
            Return False
        End If
    End Function
End Class

Sample Code for Notepad with vb.net


Imports System.Drawing.Printing
Imports System.IO
Public Class frmNotepad
    Dim filename As String = ""
    Sub wordwrap()
        'If mnuwordwrap.Checked = True Then
        '    TxtEditor.WordWrap = True
        'Else
        '    TxtEditor.WordWrap = False
        'End If
        If TxtEditor.WordWrap = False Then
            Me.mnuwordwrap.Checked = True
            TxtEditor.WordWrap = True
        Else
            Me.mnuwordwrap.Checked = False
            TxtEditor.WordWrap = False
        End If
    End Sub
    Private Sub mnuwordwrap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuwordwrap.Click
        wordwrap()
    End Sub
    Sub mnuOpen_Text()
        OpenFileDialog1.CheckFileExists = True
        OpenFileDialog1.CheckPathExists = True
        OpenFileDialog1.DefaultExt = "txt"
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        OpenFileDialog1.Multiselect = False
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            TxtEditor.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
        End If
    End Sub
    Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpen.Click
        mnuOpen_Text()
    End Sub
    Sub SaveTool()
        SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        SaveFileDialog1.FilterIndex = 1
        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim myStream As New StreamWriter(SaveFileDialog1.FileName, True)
            myStream.Write(TxtEditor.Text)
            myStream.Close()
        End If
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        SaveTool()

        'Dim myStream As Stream
        'Dim saveFileDialog1 As New SaveFileDialog()

        'saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        'saveFileDialog1.FilterIndex = 2
        'saveFileDialog1.RestoreDirectory = True

        'If saveFileDialog1.ShowDialog() = DialogResult.OK Then
        '    myStream = saveFileDialog1.OpenFile()
        '    If (myStream IsNot Nothing) Then
        '        myStream.Close()
        '    End If
        'End If
    End Sub
    Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
        FontDialog1.ShowDialog()
        TxtEditor.Font = FontDialog1.Font
    End Sub
    Sub UndoRedo()
        If UndoToolStripMenuItem.Text = "Undo" Then
            TxtEditor.Undo()
            UndoToolStripMenuItem.Text = "Redo"
            UndoToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.Z
        Else
            TxtEditor.Undo()
            UndoToolStripMenuItem.Text = "Undo"
            UndoToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.Y
        End If
    End Sub
    Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click
        UndoRedo()
    End Sub
    Sub CutToll()
        Clipboard.SetText(TxtEditor.SelectedText)
        CutToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.X
        TxtEditor.SelectedText = ""
    End Sub
    Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
        CutToll()
    End Sub
    Sub CopyTool()
        If TxtEditor.SelectionLength > 0 Then
            Clipboard.SetText(TxtEditor.SelectedText)
            CopyToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.C
        End If
    End Sub
    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        CopyTool()
    End Sub
    Sub PastTool()
        If Clipboard.ContainsText Then
            TxtEditor.SelectedText = Clipboard.GetText
            PastToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.V
        End If
    End Sub
    Private Sub PastToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PastToolStripMenuItem.Click
        PastTool()
    End Sub
    Sub FiindTool()
        Dim frm As New frmFind
        FiindToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.F
        frm.Show()
    End Sub
    Private Sub FiindToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FiindToolStripMenuItem.Click
        FiindTool()
    End Sub
    Sub SelectAllTool()
        TxtEditor.SelectionStart = 0
        TxtEditor.SelectionLength = Len(TxtEditor.Text)
        SelectAllToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.A
    End Sub
    Private Sub SelectAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAllToolStripMenuItem.Click
        SelectAllTool()
    End Sub
    Sub DateTimeTool()
        TxtEditor.Text = Now.ToString("f") 'DateTime.Now.ToString("hh:mm, dd-MMMM-yyyy")
        DateTimeToolStripMenuItem.ShortcutKeys = Keys.F5
    End Sub
    Private Sub DateTimeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimeToolStripMenuItem.Click
        DateTimeTool()
    End Sub

    Private Sub StatusBarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StatusBarToolStripMenuItem.Click
        Me.HorizontalScroll.Visible = Me.StatusBarToolStripMenuItem.Checked
    End Sub
    Sub CloseTool()
        End
    End Sub
    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        CloseTool()
    End Sub
    Sub PageSetupTool()
        PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
        If PageSetupDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
        End If
    End Sub
    Private Sub PageSetupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PageSetupToolStripMenuItem.Click
        PageSetupTool()
    End Sub

    Sub PrintTool()
        PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
        PrintToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.P
        If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
        End If
    End Sub

    Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
        PrintTool()
    End Sub
    Sub NewTool()
        Select Case MessageBox.Show("Do you want too save your file before?", "Save", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
            Case Windows.Forms.DialogResult.OK
                TxtEditor.Text = Nothing
            Case Windows.Forms.DialogResult.No
                TxtEditor.Text = Nothing
        End Select
    End Sub
    Private Sub mnuNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuNew.Click
        NewTool()
    End Sub

    Private Sub HelpTopicToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpTopicToolStripMenuItem.Click
        MsgBox("I do not design yet!", MsgBoxStyle.MsgBoxHelp)
    End Sub
    Sub SaveAsTool()
        SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        SaveFileDialog1.FilterIndex = 1
        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim myStream As New StreamWriter(SaveFileDialog1.FileName, True)
            myStream.Write(TxtEditor.Text)
            File.AppendAllText(filename, TxtEditor.Text)
            myStream.Close()
        End If
    End Sub
    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        SaveAsTool()
    End Sub
End Class

Sample TreeView


Public Class TreeViewSample

#Region "Even Procedure"

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim index As Int16 = CheckRootNode(TreeView1, cboGroup.Text)
        With TreeView1
            If index = -1 Then
                'Add Root Node.
                .Nodes.Add(cboGroup.Text)
                'Add First Sub Node.
                If txtName.Text.Trim <> "" Then .Nodes(.Nodes.Count - 1).Nodes.Add(txtName.Text.Trim)
                .ExpandAll()
            Else
                'Add Sub Node.
                If txtName.Text.Trim <> "" Then
                    If CheckNode(.Nodes(index), txtName.Text) = -1 Then _
                    .Nodes(index).Nodes.Add(txtName.Text.Trim)
                End If

                .ExpandAll()
            End If
        End With

        'Set Default
        setDefault()
    End Sub

    Private Sub btnRmove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRmove.Click

    End Sub

#End Region

   
#Region "sub procedure"

    Function CheckRootNode(ByVal TV As TreeView, _
                           ByVal NodeText As String) As _
                           Integer
        'Using For i
        'For i As Integer = 0 To TV.Nodes.Count - 1
        '    If TV.Nodes(i).Text = NodeText Then
        '        Return i
        '    End If
        'Next
        'Return -1

        'Using For each
        Dim i As Int16 = 0
        For Each N As TreeNode In TV.Nodes
            If N.Text = NodeText Then
                Return i
            End If
            i += 1
        Next
        Return -1
    End Function

    Function CheckNode(ByVal N As TreeNode, _
                          ByVal NodeText As String) As _
                          Integer
        'Using For i
        'For i As Integer = 0 To TV.Nodes.Count - 1
        '    If TV.Nodes(i).Text = NodeText Then
        '        Return i
        '    End If
        'Next
        'Return -1

        'Using For each
        Dim i As Int16 = 0
        For Each sN As TreeNode In N.Nodes
            If sN.Text = NodeText Then
                Return i
            End If
            i += 1
        Next
        Return -1
    End Function

    Sub setDefault()
        'set default causor
        cboGroup.Focus()

        For Each obj As Object In Me.Controls
            If TypeOf obj Is TextBox Or _
            TypeOf obj Is ComboBox Then
                obj.Text = ""
            ElseIf TypeOf obj Is DateTimePicker Then
                obj.value = Now.Date
            End If
        Next
    End Sub




#End Region

 
    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        'Set default screen
        setDefault()

        With TreeView1
            'If Node is "Root Node", the parent is nothing.
            If .SelectedNode.Parent Is Nothing Then
                cboGroup.Text = .SelectedNode.Text
            Else 'Mean click on sub node
                cboGroup.Text = .SelectedNode.Parent.Text
                txtName.Text = .SelectedNode.Text
            End If

        End With

    End Sub
End Class

context menu for datagridview cell, rowheader and columnheader


Here's an example MouseDown event that does this. To try the sample drop a DataGridView and three ContentMenuStrips on a form. Name the ContentMenuStrips mnuCell, mnuRow and mnuColumn.

Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
    If e.Button = Windows.Forms.MouseButtons.Right Then
        Dim ht As DataGridView.HitTestInfo
        ht = Me.DataGridView1.HitTest(e.X, e.Y)
        If ht.Type = DataGridViewHitTestType.Cell Then
            DataGridView1.ContextMenuStrip = mnuCell
            mnuCell.Items(0).Text = String.Format("This is the cell at {0}, {1}", ht.ColumnIndex, ht.RowIndex)
        ElseIf ht.Type = DataGridViewHitTestType.RowHeader Then
            DataGridView1.ContextMenuStrip = mnuRow
            mnuRow.Items(0).Text = "This is row " + ht.RowIndex.ToString()
        ElseIf ht.Type = DataGridViewHitTestType.ColumnHeader Then
            DataGridView1.ContextMenuStrip = mnuColumn
            mnuColumn.Items(0).Text = "This is col " + ht.ColumnIndex.ToString()
        End If
    End If
End Sub