Saturday, 22 February 2014

Select/UnSelect all items in checkboxlist in vb.net

Select/UnSelect all items in checkboxlist in vb.net

 Private Sub chk_crimeName_ItemCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles chk_crimeName.ItemCheck
     

 If e.Index = 0 Then
            If e.NewValue = CheckState.Checked Then
                For i As Integer = 1 To chk_crimeName.Items.Count - 1
                    chk_crimeName.SetItemCheckState(i, CheckState.Checked)
                Next
            ElseIf e.NewValue = CheckState.Unchecked Then
                For i As Integer = 1 To chk_crimeName.Items.Count - 1
                    chk_crimeName.SetItemCheckState(i, CheckState.Unchecked)
                Next
            End If
        End If

    End Sub

Select/UnSelect item of Listbox select On checkboxlist
Dim item1 As String = chk_district.SelectedIndex
            If e.NewValue = CheckState.Checked Then
                ListBox1.SelectedIndex = e.Index
            Else
                ListBox1.SetSelected(e.Index, False)
            End If

Retrieve the ValueMember of the DisplayMember items in checklistbox

Retrieve the ValueMember of the DisplayMember items in checklistbox

Public Sub chklistbox()

        Dim dr As DataTable = objdb.getdata("select district_name,district_id from district_master where state_id='" & "9" & "'")
        If dr.Rows.Count > 0 Then
            CheckedListBox1.DataSource = dr
            CheckedListBox1.DisplayMember = "district_name"
            CheckedListBox1.ValueMember = "district_id"
            CheckedListBox1.Text = Nothing
            dr = Nothing
        Else
            CheckedListBox1.DataSource = Nothing
            CheckedListBox1.Text = Nothing
        End If
    End Sub

when you assign properties value member and display member you can retrieve its value using :

 Label9.Text = CheckBoxList1.SelectedItem.ToString()
        Label8.Text = CheckedListBox1.SelectedValue.ToString()

Find integer from string

Find integer from string

 Dim Res As String
Dim str As String = "Faheem786Ahmad"
        For Each c As Char In str
            If IsNumeric(c) Then
                Res = Res & c
            End If
        Next
        MessageBox.Show(Res)

Create Excel File in vb.net

Create Excel File in vb.net

Private Sub cmdexcel_Click()
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet

Dim row As Long
Set xlapp = New Excel.Application
Set xlbook = xlapp.Workbooks.Add
Set xlsheet = xlbook.Worksheets(1)
row = 4
With xlsheet

If optdatewise.Value = True Then
.Cells(1, 1) = "Report of Complaints"
.Cells(2, 1) = "Date : " & Format(date1.Value, "dd/MMM/yyyy") & "  To : " & Format(date2.Value, "dd/MMM/yyyy")
.Rows(2).Font.Bold = True
.Rows(2).Font.Size = 12
Else
.Cells(1, 1) = "Report of Complaints Received Till  '" & Now() & "'"
End If
.Rows(1).Font.Bold = True
.Rows(1).Font.Size = 18

.Cells(3, 1) = "Total Number Of Complaints: "
.Rows(3).Font.Bold = True
.Rows(3).Font.Size = 18

.Columns(1).ColumnWidth = 9
.Columns(2).ColumnWidth = 12
.Columns(3).ColumnWidth = 11
.Columns(4).ColumnWidth = 11
.Columns(5).ColumnWidth = 15
.Columns(6).ColumnWidth = 12
.Columns(7).ColumnWidth = 12
.Columns(8).ColumnWidth = 15
.Columns(9).ColumnWidth = 18
.Columns(10).ColumnWidth = 21

.Rows(row).Font.Bold = True
For i = 0 To grid.Rows - 1
For j = 1 To grid.Cols - 1
If j = 2 And i > 0 Then
.Cells(row, j).Select
 xlapp.ActiveSheet.Hyperlinks.Add Anchor:=xlapp.Selection, Address:=mpath & "\Log Files\" & grid.TextMatrix(i, j) & "\Log.doc", TextToDisplay:=grid.TextMatrix(i, j)
Else
.Cells(row, j) = grid.TextMatrix(i, j)
End If
.Rows.Font.Name = "Centaur"
.Rows(row).WrapText = True
Next
row = row + 1
Next
.Cells(3, 1) = .Cells(3, 1) & row - 5

.Rows(4).AutoFilter
.Rows(5).Activate
xlapp.ActiveWindow.FreezePanes = True
.PageSetup.LeftMargin = 1
.PageSetup.RightMargin = 1
.PageSetup.PrintGridlines = True
.PageSetup.Orientation = xlLandscape
.PageSetup.Zoom = 90
.PageSetup.PrintTitleRows = "$4:$4"
.PageSetup.CenterFooter = "&P of &N"

xlapp.Visible = True
End With
End Sub

Not allowed special character in vb.net

Not allowed special character in vb.net

 Private Sub txtto_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtto.KeyPress
        Dim ValidInputChar = "#$%^&*,;()[]+{}:'`<>?|~!\/"
        If ValidInputChar.Contains(e.KeyChar) Then
            e.KeyChar = Nothing
        End If
    End Sub

Monday, 2 December 2013

How to Create a Datagridview using Code

How to Create a Datagridview using Code

        DataGridView1.Columns.Clear()
        DataGridView1.Rows.Clear()

        DataGridView1.ColumnHeadersDefaultCellStyle.Font = New System.Drawing.Font("Verdana", 10.0F, System.Drawing.FontStyle.Bold) 'For Bold DGV HEADER
        DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White
        DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(192, 192, 0)
        DataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None
        DataGridView1.AllowUserToAddRows = False
        DataGridView1.RowHeadersVisible = False
        DataGridView1.RowHeadersWidth = 70
        DataGridView1.ReadOnly = True
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing

        DataGridView1.ColumnHeadersHeight = 30
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridView1.RowsDefaultCellStyle.Font = New System.Drawing.Font("Verdana", 10.0F, System.Drawing.FontStyle.Regular)

        DataGridView1.Columns.Add("column name", "Column Header Name")
        DataGridView1.Columns("column name").Width = 120
        DataGridView1.Columns("column name").AutoSizeMode =                        DataGridViewAutoSizeColumnMode.DisplayedCells
        DataGridView1.Columns("column name").DefaultCellStyle.WrapMode = DataGridViewTriState.True

        DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells


Pass COLUMN NAME AND Column Header Name in columns.add method

Thursday, 28 November 2013

how to merge cells in datagridview using vb.net

'e.g. Vertically merge the same cells of the fourth column

provide column index in below code to merge the cells

 
  Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting

        If e.ColumnIndex = 3 AndAlso e.RowIndex <> -1 Then

            Using gridBrush As Brush = New SolidBrush(Me.DataGridView1.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)

                Using gridLinePen As Pen = New Pen(gridBrush)
                    ' Clear cell
                    e.Graphics.FillRectangle(backColorBrush, e.CellBounds)

                    ' Draw line (bottom border and right border of current cell)
                    'If next row cell has different content, only draw bottom border line of current cell
                    If e.RowIndex < DataGridView1.Rows.Count - 2 AndAlso DataGridView1.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value.ToString() <> e.Value.ToString() Then
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
                    End If

                    ' Draw right border line of current cell
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)

                    ' draw/fill content in current cell, and fill only one cell of multiple same cells
                    If Not e.Value Is Nothing Then
                        If e.RowIndex > 0 AndAlso DataGridView1.Rows(e.RowIndex - 1).Cells(e.ColumnIndex).Value.ToString() = e.Value.ToString() Then
                        Else
                            e.Graphics.DrawString(CType(e.Value, String), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault)
                        End If
                    End If

                    e.Handled = True
                End Using
            End Using
        End If
    End Sub