4/03/2014

กำหนดให้ TextBox รับค่าจาก Keyboard เป็นตัวเลขเท่านั้น
 
Private Sub txt_price_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_price.KeyPress
        Select Case Asc(e.KeyChar)
            Case 48 To 57
                e.Handled = False
            Case 8, 3, 22   ' Backspace = 8, Enter = 13, Delete = 46 ,Ctrl+C=3,Ctrl+V=22
                e.Handled = False
            Case 13
                e.Handled = True 'SendKeys.Send("{TAB}")
            Case 46         'Delete = 46 ,.
                If txt_price.Text.IndexOf(".") <> -1 Then
                    e.Handled = True
                Else
                    e.Handled = False
                End If
            Case Else
                e.Handled = True
        End Select 
End Sub

Private Sub txt_price_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_price.Leave
        If txt_price.Text <> "" Then
            txt_price.Text = Convert.ToDouble(txt_price.Text).ToString("#,###.#0")
        End If
End Sub

Posted on Thursday, April 03, 2014 by nuyingnaja

Dim FtpHostname As String = '192.XX.XX.XXX'
Dim FtpHostname As String = "172.16.80.185"
Dim FtpUserName As String = "username"   
Dim FtpPassWord As String = "password"   
Dim FtpPort As String = "21"

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
       Dim response As Boolean

       Dim folderServer As String = '/Newfolder/'
       Dim pathFullName As String = 'D:\1234.jpg'
       Dim filename As String = '1234.jpg'
       Dim filenameNew As String = 'pic.jpg'


        Try 
            ' ====== Upload =======
            response = cSftp.FTPuploadFile(folderServer , pathFullName , filename )

            ' ====== Delete =======
            response = cSftp.FTPDeleteFile(folderServer & filename)

            ' ====== Rename =======
            response = cSftp.FTPuploadFile(folderServer & filename,  filenameNew )
 






        Catch ex As Exception
        End Try

End Sub

Function FTPuploadFile(ByVal folderServer As String, ByVal pathFullName As String, ByVal filename As String) As Boolean
        ' folderServer = '/Newfolder/'
        ' pathFullName = 'D:\1234.jpg'
        ' filename = '1234.jpg'
        Try
            Dim ftp As String = "ftp://" & FtpHostname & folderServer
            Dim Request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(ftp & filename), System.Net.FtpWebRequest)

            Request.Credentials = New System.Net.NetworkCredential(FtpUserName, FtpPassWord)
            Request.Method = System.Net.WebRequestMethods.Ftp.UploadFile

            'Read File
            Dim File() As Byte = System.IO.File.ReadAllBytes(pathFullName)

            'Upload
            Dim Strz As System.IO.Stream = Request.GetRequestStream()
            Strz.Write(File, 0, File.Length)
            Strz.Close()
            Strz.Dispose()
            Return True
        Catch ex As Exception
            'MessageBox.Show("เกิดความผิดพลาดของไฟล์: " & _pathName & vbNewLine & ex.Message, "Messsage")
            Return False
        End Try 
End Function
 
' Delete File in Server Past FTP
 Function FTPDeleteFile(ByVal folderServerFilename As String) As Boolean

        Try
            Dim filename As String = "ftp://" & FtpHostname & "/" & folderServerFilename
            Dim ftpReq As FtpWebRequest = WebRequest.Create(filename)
            ftpReq.Method = WebRequestMethods.Ftp.DeleteFile
            ftpReq.Credentials = New NetworkCredential(FtpUserName, FtpPassWord)

            Dim ftpResp As FtpWebResponse = ftpReq.GetResponse
            Return True
        Catch ex As Exception
            'MsgBox("Error " + ex.ToString)
            Return False
        End Try 
End Function

' Rename File in Server Past FTP
Private Sub RenameFileWord(ByVal NameOld As String, ByVal NameNew As String)
        Dim reqFTP As FtpWebRequest = Nothing
        Dim ftpStream As Stream = Nothing
        reqFTP = DirectCast(FtpWebRequest.Create(New Uri(FtpHostname & NameOld)), FtpWebRequest)
        reqFTP.Method = WebRequestMethods.Ftp.Rename
        reqFTP.RenameTo = NameNew
        reqFTP.UseBinary = True
        reqFTP.Credentials = New NetworkCredential(FtpUserName, FtpPassWord)
        Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
        ftpStream = response.GetResponseStream()
        ftpStream.Close()
        response.Close()
 End Sub

Posted on Thursday, April 03, 2014 by nuyingnaja

4/02/2014

Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click   
        Dim sContents As String = GetFileContents("C:\note.txt")
        If sContents = "" Then
            Return
        Else
            Me.txtUserName.Text = Mid(sContents, 1, InStr(sContents, "|") - 1)
            Me.txtPassword.Text = Mid(sContents, InStr(sContents, "|") + 1, 900)
        End If
 End Sub


Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click   
        SaveTextToFile(txtUserName.Text & "|" & Me.txtPassword.Text, "C:\note.txt")
 End Sub

Public Sub GetFileContents(ByVal FullPath As String) As String

        Dim strContents As String = ""
        Dim objReader As StreamReader
        Try
            objReader = New StreamReader(FullPath)
            strContents = objReader.ReadToEnd()
            objReader.Close()
            Return strContents
        Catch Ex As Exception
        End Try
        Return strContentsEnd Sub

Public Function SaveTextToFile(ByVal strData As String, ByVal FullPath As String) As Boolean

        Dim Contents As String = ""
        Dim bAns As Boolean = False
        Dim objReader As StreamWriter
        Try
            objReader = New StreamWriter(FullPath)
            objReader.Write(strData)
            objReader.Close()
            bAns = True
        Catch Ex As Exception
        End Try
        Return bAns

    End Function

Posted on Wednesday, April 02, 2014 by nuyingnaja