Wednesday, August 13, 2014

Wednesday, July 30, 2014

Scan Document using vb.net code

Private Sub btnScanPage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScanPage.Click
        btnScanPage.Text = "Scanning Page, Please wait..."
        Me.Cursor = Cursors.WaitCursor
        If scan() = True Then
            btnScanPage.Text = "Scan Page"
            UgcDetails.lstattach.Items.Add(fileName)
            Me.Cursor = Cursors.Default
            Me.Close()
            Me.Dispose()
        End If
    End Sub
-------------------------------------------------------------------
--------------------------------Function scan-------------------
Private Function scan() As Boolean
        Try
            Dim commonDialogClass As New CommonDialogClass()
            Dim scannerDevice As Device = commonDialogClass.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, False, False)
            If scannerDevice IsNot Nothing Then
                Dim scannnerItem As Item = scannerDevice.Items(1)
                AdjustScannerSettings(scannnerItem, 100, 0, 0, 850, 1150, 0, 0)
                Dim scanResult As Object = commonDialogClass.ShowTransfer(scannnerItem, WIA.FormatID.wiaFormatPNG, False)
                If scanResult IsNot Nothing Then
                    Dim image As ImageFile = DirectCast(scanResult, ImageFile)
                    Dim path1 As String = "D:\scan\" & Me.Tag & "\"
                    My.Computer.FileSystem.CreateDirectory(path1)
                    fileName = path1 & Now.ToString.Replace("/", "").Replace("-", "").Replace(":", "") & ".jpg"
                    If SaveImageToFile(image, fileName) = True Then
                        PictureBox1.ImageLocation = fileName
                        Return True
                    End If
                End If
            End If
        Catch ex As Exception
            Return False
        End Try
    End Function

Tuesday, July 29, 2014

Add multiply Files at List box

 OpenFileDialog1.Multiselect = True
            OpenFileDialog1.Title = "Attach File"
            OpenFileDialog1.Filter = "All Files|*.*"
            If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                For Each fname As String In OpenFileDialog1.FileNames
                    lstattach.Items.Add(fname)
                Next
            End If

Thursday, July 17, 2014

For SMS

If Len(smscelno) = 10 Then
                    Dim convid As String = txtstate.Tag & "-" & Me.Tag
                  Dim smstxt As String = "Your complaint has been successfully registered.Your complaint No is " & convid " &
                    WebBrowsersms.Navigate("http://?uname= & pass= & send= & dest=91" & smscelno & "&msg=" & smstxt)
                    Do Until WebBrowsersms.ReadyState = WebBrowserReadyState.Complete
                        Application.DoEvents()
                    Loop
                End If

Friday, July 4, 2014

How, can i get forms name from vb.net ?

private sub proshowformnames()
  For Each FRM As Form In Application.OpenForms
     MsgBox(FRM.Name)
  Next
end sub

Friday, April 4, 2014

Read & Write Txt File in vb.net

Add class form in vb.net project

Imports System
Imports System.IO
Imports System.Text
Imports System.Collections.Generic
Imports Scripting
Public Class Testwrite
    Public Sub DeleteFile(ByVal mydocpath As String, ByVal filename As String)
        Dim fso As FileSystemObject = New FileSystemObject
        If fso.FileExists(mydocpath + "\" & filename & ".set") = True Then
            fso.DeleteFile(mydocpath + "\" & filename & ".set", True)
        End If
    End Sub
    Public Sub WriteToFile(ByVal mydocpath As String, ByVal filename As String, ByVal datastring As String)
        Dim sb As New StringBuilder()
        sb.AppendLine(datastring)
        Using outfile As StreamWriter = New StreamWriter(mydocpath + "\" & filename & ".Txt", True)
            outfile.Write(sb.ToString())
        End Using
    End Sub
    Public Function ReadFromSetFile(ByVal mydocpath As String, ByVal filename As String, ByVal key As String) As String
        Dim txtbox As String = Nothing
        Try
            Using sr As StreamReader = New StreamReader(mydocpath + "\" & filename & ".set")
                While Not sr.EndOfStream
                    Dim line = sr.ReadLine()
                    Dim seppos As Integer = InStr(line, "|")
                    If seppos > 0 Then
                        If line Like key & "*" Then
                            txtbox = Trim(Mid(line, seppos + 1))
                            Exit While
                        End If
                    End If
                End While
            End Using
        Catch ex As Exception
            txtbox = ""
        End Try
        Return txtbox
    End Function
    Public Function ReadFromFile(ByVal mydocpath As String, ByVal title As Boolean) As String
        Dim line As String = Nothing
        Try
            Using sr As StreamReader = New StreamReader(mydocpath)
                If title = True Then
                    line = sr.ReadLine
                    line = Nothing
                End If
                While Not sr.EndOfStream
                    line = sr.ReadLine()
                End While
            End Using
        Catch
            line = ""
        End Try
        Return line
    End Function
End Class

Wednesday, April 2, 2014

Remove Last Character From String

 Dim str As String
        str = TextBox1.Text
        str = str.Remove(str.Length - 1)
        MsgBox(str)

Saturday, March 29, 2014

Check And Uncheck DataGridView Cell

If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "P" Then
                DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = ""
            Else
                DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.Font = New Font("Wingdings 2", 14, FontStyle.Bold)
                DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "P"
            End If

Monday, March 24, 2014

insert update using db class

Frist add --- app.config
and write the code under app.config------
------------------------------
    [appSettings ]
   [add key ="cont1" value ="server=.;trusted_connection=yes;database=stest" /]
 [/appSettings]
     ------------------------------
###############write the code under db class################
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Public Class db
    Public cont As String = ConfigurationSettings.AppSettings("cont1")

    Public Function getdata(ByVal myqry As String) As DataTable  'This function use getdata from db

        Dim cn As New SqlConnection(cont)
        Dim ds As New DataSet
        Dim td As New DataTable
        Try
            Dim da As New SqlDataAdapter(myqry, cn)
            da.Fill(ds)
            td = ds.Tables(0)
        Catch ex As Exception
        End Try
        Return td
    End Function

    Public Function excute(ByVal myqry As String) As Boolean  'This function insert,update data in to  db

        Dim cn As New SqlConnection(cont)
        Dim ds As New DataSet
        Try
            Dim da As New SqlDataAdapter(myqry, cn)
            da.Fill(ds)
        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        End Try
        Return True
    End Function
End Class
###############################################

Saturday, March 22, 2014

open image in picturebox in vb.net

 Dim open As New OpenFileDialog()
        open.Filter = "Image Files(*.png; *.jpg; *.bmp)|*.png; *.jpg; *.bmp"
        If open.ShowDialog() = DialogResult.OK Then
            Dim fileName As String = System.IO.Path.GetFullPath(open.FileName)
            PictureBox1.Image = New Bitmap(open.FileName)
            Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
        End If

Tuesday, March 11, 2014

click first column content in datagridview vb.net

  If gridshot.Columns(e.ColumnIndex).Name = "Select" Then

Do not Allow Sorting in Data Grid View

 Private Sub gridshot_ColumnAdded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles gridshot.ColumnAdded
        gridshot.Columns.Item(e.Column.Index).SortMode = DataGridViewColumnSortMode.NotSortable
    End Sub

Wednesday, February 12, 2014

Get Day of Week in SQL 2005/2008

select  DATENAME(dw,comtype) AS OrderDay from tblcom_type where s_no=6
   --Result  Friday

Friday, January 31, 2014

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

Thursday, December 26, 2013

Get Date in SQL Function

select day(GETDATE()-10)  Result -- 16
select month(GETDATE())   Result -- 12

select convert(varchar(11), getdate(),100)  Result -- Dec 26 2013
select convert(varchar(11), getdate(),110)  Result -- 12-26-2013
select convert(varchar(11), getdate(),120)  Result -- 2013-12-26

Friday, December 20, 2013

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

Thursday, December 19, 2013

If string was found in the RichTextBox, High Light ,Replace , Skip ..

Dim start As Integer = 0
    Dim indexOfSearchText As Integer = 0
'********************Replace Select Use Replace btn****************
        Dim startindex As Integer = 0

        If txt_Name_Changed.Text.Length > 0 Then
            startindex = FindMyText(txt_Name_Changed.Text.Trim(), start, RichTextBox1.Text.Length)
        End If
        ' If string was found in the RichTextBox, highlight it
        If startindex >= 0 Then
            ' Set the highlight color as red
            RichTextBox1.SelectionBackColor = Color.Plum
            RichTextBox1.SelectedText = lblpname.Text
            ' Find the end index. End Index = number of characters in textbox
            Dim endindex As Integer = txt_Name_Changed.Text.Length
            ' Highlight the search string
            RichTextBox1.Select(startindex, endindex)
            ' mark the start position after the position of last search string
            start = startindex + endindex
        End If

 '********************Skip ^^^ Use SKIP btn**************
        Dim startindex As Integer = 0
        If txt_Name_Changed.Text.Length > 0 Then
            startindex = FindMyText(txt_Name_Changed.Text.Trim(), start, RichTextBox1.Text.Length)
        End If
        If startindex >= 0 Then
            RichTextBox1.SelectionBackColor = Color.White
            Dim endindex As Integer = txt_Name_Changed.Text.Length
            RichTextBox1.Select(startindex, endindex)
            start = startindex + endindex
        End If

**************** Function For Replace And Skip ***********************
 Public Function FindMyText(ByVal txtToSearch As String, ByVal searchStart As Integer, ByVal searchEnd As Integer) As Integer
        ' Unselect the previously searched string
        ' ''If searchStart > 0 AndAlso searchEnd > 0 AndAlso indexOfSearchText >= 0 Then
        ' ''    RichTextBox1.Undo()
        ' ''End If
        ' Set the return value to -1 by default.
        Dim retVal As Integer = -1
        ' A valid starting index should be specified.
        ' if indexOfSearchText = -1, the end of search
        If searchStart >= 0 AndAlso indexOfSearchText >= 0 Then
            ' A valid ending index
            If searchEnd > searchStart OrElse searchEnd = -1 Then
                ' Find the position of search string in RichTextBox
                indexOfSearchText = RichTextBox1.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None)
                ' Determine whether the text was found in richTextBox1.
                If indexOfSearchText <> -1 Then
                    ' Return the index to the specified search text.
                    retVal = indexOfSearchText
                End If
            End If
        End If
        Return retVal
    End Function

**************write TextChanged  Event on Matching String Txt Box*******************
 Private Sub txt_Name_Changed_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_Name_Changed.TextChanged
        start = 0
        indexOfSearchText = 0
    End Sub

^^^^^^^^^^^^^^^^^^^If string was found in the RichTextBox, Then  High Light .^^^^^^^^^^^^^^^^^^^
 Sub FindTxtInRTB() ' HighLight Search txt In RTF
        Dim srchterm As String = txt_Name_Changed.Text
        Dim index As Int32 = 0
        While index <> -1
            index = RichTextBox1.Find(srchterm, index, RichTextBoxFinds.None)
            If index <> -1 Then

                RichTextBox1.SelectionBackColor = Color.Orange
                index += 1
            End If
        End While
        RichTextBox1.DeselectAll()
    End Sub

Wednesday, December 11, 2013

Find integer from string

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

Wednesday, December 4, 2013

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()