Pages

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 :-)

Friday, 23 April 2010

Using Log4Net

My search is to find simple way to use Log4Net in .NET application was futile. Finally, I decided to write sample code on my own :-)

Step 1: Add reference to log4net.dll in your project (Project -> Add Reference -> Browse)
Step 2: Add application configuration file (Project -> Resources -> Add new item -> app.config)
Step 3: Add below in your app.config file in tag


















The above example shows configuration to the RollingFileAppender to write to the file log.txt. The file written to will always be called log.txt because the StaticLogFileName param is specified. The file will be rolled based on a size constraint (RollingStyle). Up to 10 (MaxSizeRollBackups) old files of 100 KB each (MaximumFileSize) will be kept. These rolled files will be named: log.txt.1, log.txt.2, log.txt.3, etc..







Step 4: Initialize log object as below

dim log as log4Net.ILog

log = log4net.LogManager.GetLogger("Emailer")
log4net.Config.XmlConfigurator.Configure()
log.Debug("Debug message")

Simple, isn't it?

Thursday, 15 April 2010

Unable to find manifest signing certificate in the certificate store

While developing Softphone application using VS 2008, I moved a project from one computer to a different one. When I then tried to rebuild the solution I received the following error:

"Unable to find manifest signing certificate in the certificate store"

As I was sure that I wasn't using any certificate to sign the assembly I couldn't understand the reason for this error message. It turned out that I had to manually go into the *.vbproj file and remove the following four lines that were apparently left over from some past experiments with signing using a certificate:

<manifestcertificatethumbprint>...</manifestcertificatethumbprint>
<manifestkeyfile>...</manifestkeyfile>
<generatemanifests>...</generatemanifests>
<signmanifests>...</signmanifests>

After I had removed those lines I reloaded the project and the solution rebuilt just fine.