This guide shows how to use Gurux SMS component (GXSMS) in the Visual Studio .NET environment
using the C# language. Source code is available at sourceforge.net
Adding GXSMS component to the project
- Start Visual Studio .NET
- Open the project where you want to add the GXSMS component, or create a new one
- Open Toolbox
- Under Gurux tab, select the component, and drag and drop it on the form
- Allow the use of any version of the media component:
- Select the component.
- In Solution Explorer view, in the References folder, select Gurux.GuruxSMS.
- In Properties view, set Specific Version to False.
Note: If the Gurux tab is not shown in the Toolbox,
you can implement it manually
Using components with .NET
You can either drag the component to the form, or create it dynamically.
Creating component dynamically
To create component in runtime, select Add Reference in the Project menu.
The dialog shows available components, select the one(s) to use (in this case GXSMS component).
Click OK to finish. The following example shows how to create a GXSMS component.
using GuruxGuruxSMS; //Create new component GXSMS GXSMS1 = new GXSMSClass();
Setting Media Properties
You can either select appropriate settings in the Properties dialog or edit the settings
programmatically.
'Set receiver phone number
GXSMS1.ReceiverNumber = "Receiver phone number";
'Set port where Modem or phone is found
GXSMS1.Port = "Com1";
GXSMS1.Open();
GXSMS1.Send("Hello World", "");
Sending and receiving packets
All data is sent asynchronously. Response is received as a Received event message.
Receiving event messages
To receive event sent by the GXSMS component, select the component by right-clicking it on the
form and select Properties. Create handlers for OnError and OnReceived methods.
To create a handler, select an event and enter name for the handler. In the following example,
received data is converted to string and stored.
private void gxsms1_OnReceived(object sender, Gurux.GuruxSMS.GXSMSMessage SMSMessage, string SenderInfo)
{
//Update data to the edit box.
ReceiveDataText.Text += SMSMessage.Data;
//Update Sender Information to the edit box.
SenderText.Text = SenderInfo;
}
private void gxSMS1_OnError(object sender, string ErrorInfo)
{
MessageBox.Show(sender.ToString());
}
Error Handling
It's very important to consider error handling when sending and receiving data.
The connection can drop down, or something else unexpected may happen.
If something unwanted happens, GXSMS component returns error. The following
example shows how to handle errors in .NET.
try
{
object data = Convert.ChangeType("Hello", System.TypeCode.Object);
string receiver = null;
GXSMS1.Send(ref data, ref receiver);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
Using the components from development environment
Using from .NET | Using from Visual Basic | Using from C++