Monday, May 4, 2009

Sending & Recieving SMS From VB.Net

For reading sms:

 Dim comSerial As New System.IO.Ports.SerialPort
        Dim atcCommand As String
        Dim sphnPhoneNo As String
        Dim cescQuote As Char
        Dim s As String


        With comSerial
            .PortName = "COM1"
            .BaudRate = "9600"
            .StopBits = IO.Ports.StopBits.One
            .DataBits = 8
            .Parity = IO.Ports.Parity.None
            .ReadBufferSize = 10000
            .ReadTimeout = 1000
            .WriteBufferSize = 10000
            .WriteTimeout = 10000
            .RtsEnable = True

            .Open()
            .DiscardOutBuffer()
            'for reading sms

            'atcCommand = "AT+CMGR=10"        'to read from inbox
            '.Write(atcCommand + vbCrLf)     'execute command on buffer
            'TextBox1.Text = .ReadExisting()



For sending sms:

 'for writing sms
            '.Write("AT+CMGF=1" + vbCrLf)

            
            cescQuote = Char.ConvertFromUtf32(34)
            sphnPhoneNo = "+914567352312"
            atcCommand = "AT+CMGS=" + cescQuote + sphnPhoneNo + cescQuote + vbCrLf + "WORLD" + Char.ConvertFromUtf32(26)
            '  .Write(atcCommand + vbCrLf)
            .WriteLine(atcCommand + vbCrLf)

            TextBox1.Text = .ReadExisting()
            .DiscardOutBuffer()
            .Close()

Send Email From ASP.NET

This tutorial will show you how to send a simple email message using ASP.NET 2.0 and VB.NET 

Sending a email using ASP.NET 2.0 and VB.NET is actually very simple. 

First, you will need to import the System.Net.Mail namespace.

The System.Net.Mail namespace contains the SmtpClient and MailMessage Classes that we need in order to send the email.

Imports System.Net.Mail

We use the btnSubmit_Click event to do the work.

We then call the emailClient.Send to send the message using the variables from our ASP.NET coded page.

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
Dim message As New MailMessage(txtTo.Text, txtFrom.Text, txtSubject.Text, txtBody.Text)
Dim emailClient As New SmtpClient(txtSMTPServer.Text)
emailClient.Send(message)
litStatus.Text = "Message Sent"
Catch ex As Exception
litStatus.Text = ex.ToString()
End Try
End Sub




WPF Custom Control Dependency Property

Let's say you have a custom WPF control called SearchTextBox. It has a textbox and a button labeled "search". Simple enough, you reuse it in your application when you want to provide search. 

Then one day, you decide you need this control needs to be bindable. So you expose a public property Text and map it to textSearch just like you would in WinForms.

Well, that doesn't work, so you google around and stumble upon Dependency Properties and learn how to create your own (VS snippet shortcut propdb) and create a Text DP.

Now you spend 30 minutes trying to map your Text DP to your textSearch.Text until you finally figure out that your DP snippet lead you astray and there is one more step that didn't get included in the shortcut. In the UIPropertyMetaData, you need to specify a function to call when the property changes - so you can set textSearch.Text.

The function looks like this:

static void textChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args) 
{ SearchTextBox searchTextBox = (SearchTextBox)property; searchTextBox.textSearch.Text= (string)args.NewValue; 
}



And the rest of the DP looks like this:


public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } public static readonly DependencyProperty TextProperty = DependencyProperty.Register( "Text", typeof(string), typeof(SearchTextBox), new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack)));


The important part here is what wasn't created by the VS snippet :



new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))




Now you are binding to your custom control and all is good.