Pages

Tuesday 29 June 2010

How to view line numbers in VB.NET

Remember line numbers ??? Long time ago, they were used to branch them with a ‘Go To’ statement. And it is still available with Visual Studio 2008 and pretty useful for error handling too…

For example, a lot of error messages give you specific line numbers in your code to use to find the errors. Trying to count down the page manually can be pretty tedious.

To enable line numbers, go to

Tools menu item > Options > Text Editor > Basic > General

Click the checkbox in front of "Line Numbers". It should do it…

Friday 25 June 2010

Stopping a form closing using vb.net

Quick and useful tip..

 

Private Sub MyForm_Closing(ByVal sender As System.Object, ByVal e As _
            System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Try
   Select Case MessageBox.Show("Are you sure you want to close this window?", _
      "Your application", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    Case MsgBoxResult.Yes
     'Do Nothing
    Case MsgBoxResult.No
     e.Cancel = True
   End Select
Catch ee As Exception
  Throw ee
End Try
End Sub

Delegates and Events

Oh…spent lot of time trying to pass values between Parent and Child forms. It is much needed for my SoftPhone application to keep track of call states across windows. Finally, I was able to achieve it using delegates and events (interesting concepts, though)

To pass value from Parent (Form1) to Child Form (Form2)

Step 1: Create delegate for function

Public Delegate Sub PassData(ByVal Message As String)

Step 2: Create Object for deleage

Public frm2PassData As PassData

Step 3: Create public procedure in child form with same signature as delegate

Public Sub GetData(ByVal Message As String)
    TextBox1.Text = Message
End Sub

Step 4: Link Delegate with frm2 function

frm = New Form2
frm2PassData = New PassData(AddressOf frm.GetData)

frm.show

 

Step 5: To pass data from Parent Form to child form, just call delegate object as below

if frm2passdata isnot nothing then

frm2passdata(“Hello”)

end if

To pass value from Child Form to Parent Form (use Events)

Step 1: Declare Public event in Child Form

Public Event Notification(ByVal Message As String)

Step 2: RaiseEvent in Child form, as applicable

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
     RaiseEvent Notification(TextBox1.Text)
End Sub

Step 3: Create procedure in Parent form with same signature as Event

Public Sub Getdata(ByVal Message As String)
     Me.Text = Message
End Sub

Step 4: Add event handler for child form

AddHandler frm.Notification, AddressOf Getdata

Done..easy isn’t it?

 

Thursday 24 June 2010

Iterating through a Dictionary Object's Items in VB.NET

Good example for iterating through dictionary object…

Dim DictObj As New Dictionary(Of Integer, String) 

DictObj.Add(1, "ABC") 

DictObj.Add(2, "DEF") 

DictObj.Add(3, "GHI") 

DictObj.Add(4, "JKL") 

For Each kvp As KeyValuePair(Of Integer, String) In DictObj 

Dim v1 As Integer = kvp.Key 

Dim v2 As String = kvp.Value 

     Debug.WriteLine("Key: " + v1.ToString _ 

            + " Value: " + v2) 

Next

For more information, please refer to http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

Wednesday 9 June 2010

How to attach Call Start time in the Routing strategy

This is only an example. You will need to test this to see if it satisfies your requirements...

All variables in the example below are of type "Integer" (not Float) except for the "StartTime" variable in the example below which is of type String.

A Multi-Assign object in the IR Designer could be used as follows to obtain the timestamp in the format you are looking for (hh:mm:ss):

Variable name: Value:

TimeStarted => TimeStamp[] / 1000
TimeHour => TimeStarted / 3600
TimeMinute => (TimeStarted - (TimeHour * 3600)) / 60
TimeSecond => TimeStarted - (TimeHour * 3600) - (TimeMinute * 60)
StartTime => Cat[Cat[Cat[Cat[TimeHour,':'],TimeMinute],':'],TimeSecond]

 

Update (attach) the value of the StartTime variable to the call as userdata.

Thursday 3 June 2010

How to detect socket disconnection in .NET?

After spending few days, finally able to figure socket disconnect detection in .NET :-)

 

Public Shared Function IsConnected(ByVal socket As Socket) As Boolean
        Try
            Return Not (socket.Poll(1, SelectMode.SelectRead) AndAlso socket.Available = 0)
        Catch generatedExceptionName As SocketException
            Return False
        End Try
End Function

In Timer_tick event, keep checking for connection status.  If return value is ‘false’, we can say that socket is disconnected now :-)