Tuesday, September 23, 2014

Photo Viewer using C# WPF

//Image Viewer
//create a folder named "images" under your application folder.
//then add an image viewer on your application and named it "Image_Viewer"
// next, add the code below under your browse image button or under MouseDown event of your Image_Viewer
//under Properties choose Common,Stretch=Uniform, StretchDirection=Both
//also add the following directives
//using System.IO;
//using Microsoft.Win32;

String AppPath = System.AppDomain.CurrentDomain.BaseDirectory;
OpenFileDialog dlg = new OpenFileDialog();


dlg.Filter = "(.jpg)|*.jpg";
dlg.DefaultExt = ".jpg";
dlg.ShowDialog();

try
    {
            if (System.IO.Directory.Exists(AppPath + "images") == false)
                {
                    System.IO.Directory.CreateDirectory(AppPath + "images");

                }

                //MessageBox.Show("@" + AppPath + "images\\" + System.IO.Path.GetFileName(dlg.FileName));
                File.Copy(dlg.FileName, AppPath + "images\\" + System.IO.Path.GetFileName(dlg.FileName), true);


                BitmapImage b = new BitmapImage();
                b.BeginInit();
                b.UriSource = new Uri(AppPath + "images\\" + System.IO.Path.GetFileName(dlg.FileName), UriKind.Absolute);
                b.CacheOption = BitmapCacheOption.OnLoad;
                b.EndInit();
               
                Image_Viewer.Source = b;

                //throw new Exception("");
        }
catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        }

Thursday, September 18, 2014

DateTimePicker for C# WPF

DateTimePicker dateTimePicker = new DateTimePicker{
     Format = DateTimePickerFormat.Time,
     ShowUpDown = true // Set to false if you don't want the up and down arrows to select the time 
};

DateTime.ToString() Guides

DateTime.ToString() Guides 

 

All the patterns:

0 MM/dd/yyyy 08/22/2006
1 dddd, dd MMMM yyyy Tuesday, 22 August 2006
2 dddd, dd MMMM yyyy HH:mm Tuesday, 22 August 2006 06:30
3 dddd, dd MMMM yyyy hh:mm tt Tuesday, 22 August 2006 06:30 AM
4 dddd, dd MMMM yyyy H:mm Tuesday, 22 August 2006 6:30
5 dddd, dd MMMM yyyy h:mm tt Tuesday, 22 August 2006 6:30 AM
6 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
7 MM/dd/yyyy HH:mm 08/22/2006 06:30
8 MM/dd/yyyy hh:mm tt 08/22/2006 06:30 AM
9 MM/dd/yyyy H:mm 08/22/2006 6:30
10 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
10 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
10 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
11 MM/dd/yyyy HH:mm:ss 08/22/2006 06:30:07
12 MMMM dd August 22
13 MMMM dd August 22
14 yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2006-08-22T06:30:07.7199222-04:00
15 yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2006-08-22T06:30:07.7199222-04:00
16 ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Tue, 22 Aug 2006 06:30:07 GMT
17 ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Tue, 22 Aug 2006 06:30:07 GMT
18 yyyy'-'MM'-'dd'T'HH':'mm':'ss 2006-08-22T06:30:07
19 HH:mm 06:30
20 hh:mm tt 06:30 AM
21 H:mm 6:30
22 h:mm tt 6:30 AM
23 HH:mm:ss 06:30:07
24 yyyy'-'MM'-'dd HH':'mm':'ss'Z' 2006-08-22 06:30:07Z
25 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
26 yyyy MMMM 2006 August
27 yyyy MMMM 2006 August

The patterns for DateTime.ToString ( 'd' ) :

0 MM/dd/yyyy 08/22/2006

The patterns for DateTime.ToString ( 'D' ) :

0 dddd, dd MMMM yyyy Tuesday, 22 August 2006

The patterns for DateTime.ToString ( 'f' ) :

0 dddd, dd MMMM yyyy HH:mm Tuesday, 22 August 2006 06:30
1 dddd, dd MMMM yyyy hh:mm tt Tuesday, 22 August 2006 06:30 AM
2 dddd, dd MMMM yyyy H:mm Tuesday, 22 August 2006 6:30
3 dddd, dd MMMM yyyy h:mm tt Tuesday, 22 August 2006 6:30 AM

The patterns for DateTime.ToString ( 'F' ) :

0 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07

The patterns for DateTime.ToString ( 'g' ) :

0 MM/dd/yyyy HH:mm 08/22/2006 06:30
1 MM/dd/yyyy hh:mm tt 08/22/2006 06:30 AM
2 MM/dd/yyyy H:mm 08/22/2006 6:30
3 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM

The patterns for DateTime.ToString ( 'G' ) :

0 MM/dd/yyyy HH:mm:ss 08/22/2006 06:30:07

The patterns for DateTime.ToString ( 'm' ) :

0 MMMM dd August 22

The patterns for DateTime.ToString ( 'r' ) :

0 ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Tue, 22 Aug 2006 06:30:07 GMT

The patterns for DateTime.ToString ( 's' ) :

0 yyyy'-'MM'-'dd'T'HH':'mm':'ss 2006-08-22T06:30:07

The patterns for DateTime.ToString ( 'u' ) :

0 yyyy'-'MM'-'dd HH':'mm':'ss'Z' 2006-08-22 06:30:07Z

The patterns for DateTime.ToString ( 'U' ) :

0 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07

The patterns for DateTime.ToString ( 'y' ) :

0 yyyy MMMM 2006 August

Building a custom DateTime.ToString Patterns

The following details the meaning of each pattern character. Note the K and z character.
d Represents the day of the month as a number from 1 through 31. A single-digit day is formatted without a leading zero
dd Represents the day of the month as a number from 01 through 31. A single-digit day is formatted with a leading zero
ddd Represents the abbreviated name of the day of the week (Mon, Tues, Wed etc)
dddd Represents the full name of the day of the week (Monday, Tuesday etc)
h 12-hour clock hour (e.g. 7)
hh 12-hour clock, with a leading 0 (e.g. 07)
H 24-hour clock hour (e.g. 19)
HH 24-hour clock hour, with a leading 0 (e.g. 19)
m Minutes
mm Minutes with a leading zero
M Month number
MM Month number with leading zero
MMM Abbreviated Month Name (e.g. Dec)
MMMM Full month name (e.g. December)
s Seconds
ss Seconds with leading zero
t Abbreviated AM / PM (e.g. A or P)
tt AM / PM (e.g. AM or PM
y Year, no leading zero (e.g. 2001 would be 1)
yy Year, leadin zero (e.g. 2001 would be 01)
yyy Year, (e.g. 2001 would be 2001)
yyyy Year, (e.g. 2001 would be 2001)
K Represents the time zone information of a date and time value (e.g. +05:00)
z With DateTime values, represents the signed offset of the local operating system's time zone from Coordinated Universal Time (UTC), measured in hours. (e.g. +6)
zz As z but with leadin zero (e.g. +06)
zzz With DateTime values, represents the signed offset of the local operating system's time zone from UTC, measured in hours and minutes. (e.g. +06:00)
f Represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value.
ff Represents the two most significant digits of the seconds fraction; that is, it represents the hundredths of a second in a date and time value.
fff Represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value.
ffff Represents the four most significant digits of the seconds fraction; that is, it represents the ten thousandths of a second in a date and time value. While it is possible to display the ten thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.
fffff Represents the five most significant digits of the seconds fraction; that is, it represents the hundred thousandths of a second in a date and time value. While it is possible to display the hundred thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.
ffffff Represents the six most significant digits of the seconds fraction; that is, it represents the millionths of a second in a date and time value. While it is possible to display the millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.
fffffff Represents the seven most significant digits of the seconds fraction; that is, it represents the ten millionths of a second in a date and time value. While it is possible to display the ten millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.
F Represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value. Nothing is displayed if the digit is zero.
: Represents the time separator defined in the current DateTimeFormatInfo..::.TimeSeparator property. This separator is used to differentiate hours, minutes, and seconds.
/ Represents the date separator defined in the current DateTimeFormatInfo..::.DateSeparator property. This separator is used to differentiate years, months, and days.
" Represents a quoted string (quotation mark). Displays the literal value of any string between two quotation marks ("). Your application should precede each quotation mark with an escape character (\).
' Represents a quoted string (apostrophe). Displays the literal value of any string between two apostrophe (') characters.
%c Represents the result associated with a c custom format specifier, when the custom date and time format string consists solely of that custom format specifier. That is, to use the d, f, F, h, m, s, t, y, z, H, or M custom format specifier by itself, the application should specify %d, %f, %F, %h, %m, %s, %t, %y, %z, %H, or %M. For more information about using a single format specifier, see Using Single Custom Format Specifiers.
||\c || Represents the escape character, and displays the character "c" as a literal when that character is preceded by the escape character (\). To insert the backslash character itself in the result string, the application should use two escape characters ("\\").
Any other character copies any other character to the result string, without affecting formatting. ||

This guide is from http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

Sunday, April 13, 2014

Send and Receive SMS using VB6

 Send and Receive SMS using VB6 No Third Party Software Required


How to Use.

1. Connect your broadband device to your PC that support sending and receiving SMS or your phone.
2. Install driver for your device.
3. Then stop all programs associated with your device like device dashboard. If you will not stop your device dashboard, you can send SMS but you cannot received SMS directly from your program.
4. Run this program and choose your device listed on the combo box(cbo). If your device is not on the list. click refresh device list(cmdrefresh).
5. Click Test Connection(cmdconnect).
6. You may start sending SMS.
7. Tick Receive Message(chk)

Components

1. Microsoft Comm Control 6.0

Module Code


Public GSMPort As String
Public GSMDescription As String
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public OK As Boolean
Public Error As Boolean


Function gsmConnect(xMSComm As MSComm, xPort As String) As String


    On Error Resume Next
   
    With xMSComm
    .CommPort = xPort
    .Settings = "9600,N,8,1"
    .Handshaking = comRTS
    .RTSEnable = True
    .DTREnable = True
    .RThreshold = 1
    .SThreshold = 1
    .InputMode = comInputModeText
    .InputLen = 0
    .PortOpen = True
    End With
   
    If Err.Number = 0 Then
        gsmConnect = "Connection Successful"
    Else
        gsmConnect = "Cannot connect to" & GSMDescription & "." & Err.Description
       
        GSMCom = ""
        GSMDescription = ""
    End If
       
End Function

Sub Wait_For_Response()
Dim Start

   Start = Timer
   Do While Timer < Start + 8
      DoEvents
      If OK Then
        Exit Sub
      End If
      If Error Then
        Exit Sub
      End If
   Loop
End Sub

Form Code

Private Sub chk_Click()

Screen.MousePointer = vbDefault
    If chk.Value = 0 Then
        If MSComm1.PortOpen = True Then
            Timer1.Enabled = False
            MSComm1.PortOpen = False
            'StatusBar1.Panels(1).Text = "Auto responder is inactive..."
           
        End If
    ElseIf chk.Value = 1 Then

       ' StatusBar1.Panels(1).Text = "Waiting for incoming messages..."
       
        getComPorts
        Connect_Now
        Timer1_Timer
        Timer1.Enabled = True
        'received2
    End If

End Sub

Private Sub cmdclose_Click()

Unload Me

End Sub

Private Sub cmdconnect_Click()
   
    If Len(Trim(cbo.Text)) = 0 Then
        MsgBox "Please choose device to connect.", vbwarning, "Alert!"
            cbo.SetFocus
        Exit Sub
    End If
   
    On Error Resume Next
    Screen.MousePointer = vbHourglass
    MSComm1.PortOpen = False
    getComPorts
   
    MsgBox gsmConnect(MSComm1, GSMPort)
   
    Screen.MousePointer = vbDefault
    MSComm1.PortOpen = False
   
End Sub

Private Sub cmdrefresh_Click()


    'On Error Resume Next
        cbo.Clear
       
    Set WMIObjectSet = GetObject("winmgmts:\\.\root\CIMV2").ExecQuery("select * from Win32_PnPEntity")
   
    For Each wmiobject In WMIObjectSet
        If InStr(wmiobject.Name, "COM") Then
        If InStr(wmiobject.Name, "COM ") Then GoTo nope
            cbo.AddItem wmiobject.Name
           
nope: End If
      Next
        Set WMIObjectSet = Nothing
       
End Sub

Private Sub getComPorts()

    On Error Resume Next
   
    If Mid(cbo.Text, Len(Trim(cbo.Text)) - 2, 1) = "M" Then
    GSMPort = Mid(cbo.Text, Len(Trim(cbo.Text)) - 1, 1)
    Else
    GSMPort = Mid(cbo.Text, Len(Trim(cbo.Text)) - 2, 2)
    End If
    GSMDescription = cbo.Text

End Sub

Private Sub Connect_Now()


On Error GoTo errmali
If Len(Trim(GSMPort)) = 0 Then
    MsgBox "Please check your connection to the device."

Else
    With MSComm1
    .CommPort = GSMPort
    .Settings = "9600,N,8,1"
    .Handshaking = comRTS
    .RTSEnable = True
    .DTREnable = True
    .RThreshold = 1
    .SThreshold = 1
    .InputMode = comInputModeText
    .InputLen = 0
    .PortOpen = True
End With
End If
Exit Sub
errmali:
    MsgBox "Please check your connection to the device."
   
End Sub

Private Sub cmdsend_Click()


On Error Resume Next
    MSComm1.PortOpen = False
    Connect_Now
    MSComm1.Output = "AT" & vbCrLf
    Sleep 500
   
    MSComm1.Output = "AT+CMGF = 1" & vbCrLf
    Sleep 500
   
    MSComm1.Output = "AT+CMGS = " & Chr(34) & txtcontact.Text & Chr(34) & vbCrLf
    Sleep 1000
   
    MSComm1.Output = txtsms.Text & Chr(26)
    Sleep 2000

End Sub

Public Sub received2()


On Error Resume Next
Dim rawmsg, msgno, msgdate, mobile, msgreceived As String
'StatusBar1.Panels(1).Text = "new message received..."

MSComm1.Output = "AT+CMEE = 1" & Chr$(13)
Do
    DoEvents
    buffer$ = buffer$ & MSComm1.Input
Loop Until InStr(buffer$, "OK")
buffer$ = ""

StatusBar1.Panels(1).Text = "processing new message..."

MSComm1.Output = "AT+CMGF = 1" & Chr$(13)

Do
    DoEvents
    buffer$ = buffer$ & MSComm1.Input
Loop Until InStr(buffer$, "OK")
'List All Messages
buffer$ = ""

MSComm1.Output = "AT+CMGL = " & Chr(34) & "ALL" & Chr(34) & vbCrLf
Do
DoEvents
buffer$ = MSComm1.Input

If InStr(buffer$, "+CMGL:") Then
    'List2.AddItem Replace(Split(buffer$, ",")(2), Chr(34), "") & "-" & Replace(Split(buffer$, ",")(1), "REC", "") & "-" & Replace(Split(buffer$, ",")(4), Chr(34), "") & "-" & Replace(Split(buffer$, Chr(13))(2), Chr(10), "")
    rawmsg = Replace(Split(buffer$, ",")(2), Chr(34), "") & "-" & Replace(Split(buffer$, ",")(1), "REC", "") & "-" & Replace(Split(buffer$, ",")(4), Chr(34), "") & "-" & Replace(Split(buffer$, Chr(13))(2), Chr(10), "")
   
    msgno = Val(Replace(Split(buffer$, ",")(0), "+CMGL:", "")) 'Split(rawmsg, "-")(0) & vbCrLf
    msgno = Trim(msgno)
   
    msgdate = Split(rawmsg, "-")(2) & vbCrLf
    msgdate = Replace(Replace(msgdate, Chr(10), ""), Chr(13), "")
   
    mobile = Split(rawmsg, "-")(0) & vbCrLf
    mobile = Replace(Replace(Replace(mobile, "+63", ""), Chr(10), ""), Chr(13), "")
   
    msgreceived = Split(rawmsg, "-")(3) & vbCrLf
    msgreceived = Replace(Replace(Trim(msgreceived), Chr(10), ""), Chr(13), "")

    ' display received message in a listbox name List2
   
    'List2.AddItem "Msg. No : " & msgno & " Date: " & msgdate & " Mobile No. :" & mobile & " SMS : " & msgreceived
    'KEYWORD is for auto reply, remove the block of code if you do not want to create an auto reply of received message
   
    If UCase(msgreceived) = "KEYWORD" Then
            'Process received message here
        Screen.MousePointer = vbHourglass
                           
        MSComm1.Output = "AT" & vbCrLf
   
            Wait_For_Response
                                   
            MSComm1.Output = "AT+CMGF = 1" & vbCrLf
           
              'This line can be removed if your modem will always be in Text Mode...
           
            Wait_For_Response
           
            MSComm1.Output = "AT+CMGS = " & Chr(34) & "+63" & mobile & Chr(34) & vbCrLf
           
            'Replace this with your mobile Phone 's No.
           
            Wait_For_Response
                                   
            MSComm1.Output = "HI from SMS" & Chr(26)
                'Sleep 2000
            Wait_For_Response
           
                Screen.MousePointer = vbDefault
                                        
    End If
    'end of KEYWORD block of code
   
         List2.AddItem "Msg. No : " & msgno & " Date: " & msgdate & " Mobile No. : +63" & mobile & " SMS : " & msgreceived
            'Delete SMS after reading
            cmd = "AT+CMGD = " & msgno
            MSComm1.Output = cmd & vbCrLf
            Wait_For_Response
       
End If
Loop Until InStr(buffer$, "OK")
buffer$ = ""

End Sub

Private Sub Timer1_Timer()

received2
    'Timer1.Enabled = false
   ' StatusBar1.Panels(1).Text = "Waiting for incoming messages..."
    MSComm1.PortOpen = False
    MSComm1.PortOpen = True
End Sub