this blog contains information for .net and sql stuffs. You can find various tips and tricks to overcome problem you may be facing in ...

Friday, November 28, 2008

How to disable PageTab?

Many times we have requirement that, we have more than one tab pages and we want to disable other tab untill we complete work on a tab.

To simple answer to this situation is NO as there is no property to disable tabpage.

You can use third party control to do this.

Other solution for this is to disable all controls on that tab page. We can achieve this with the help of follwoing code snippest ..

 For Each tab As Control In Me.tabMain.Controls
                Dim c As TabPage = TryCast(tab, TabPage)
                If Not c Is Nothing Then
                    tab.Enabled = False
                End If
            Next

Above code we can put on TabSelectedIndexChange event, and can disable or enable.
Please let you know that you can able to select tab but all control on that is disabled so you can not do anything.

Tuesday, November 18, 2008

ALTER TABLE Syntax

to change table column datatype, add new column, remove column you can use below syntax rather than to modify from design view of table.

--add new column
ALTER TABLE "Tablename"
ADD  "ColumnName" "datatyepe" "constraints"
 
 --alter table column
ALTER TABLE "TableName"
ALTER COLUMN "ColumnName" "datatype" "constraints"
 
 --drop/remove column from table
ALTER TABLE "TableName"
DROP COLUMN "ColumnName"

datatype can be any valid datatype of sql and constraints is any like not null, null, primary key etc..

Monday, November 17, 2008

display dialog box before confirm closing of form..

in some sitation we want to display dialog box to user before closing form. Either by clicking on some button or close by pressing Alt +F4 This can be done by writing code in formClosing event.

Private Sub frmMyForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If MessageBox.Show("Change has been made to this form. Do you want to continue?", "Are you sure?", MessageBoxButtons.YesNo) = System.Windows.Forms.DialogResult.Yes Then
            e.Cancel = True
        End If
    End Sub

At, that time we just need to cancle closing event. Nothing else otherwise form will be closed whatever you have write.

How to allow only Numeric value in textbox?

this code snippest allow you to enter only numeric value to text box...

Private Function TrapKey(ByVal KCode As String) As Boolean
   If (KCode >= 48 And KCode <= 57) Or KCode = 8 Then
       TrapKey = False
   Else
       TrapKey = True
    End If
End Function

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        e.Handled = TrapKey(Asc(e.KeyChar))
End Sub

Alternate you can write common function like

 Public Sub NumbericText(ByVal sender As System.Object, ByVal e As KeyPressEventArgs)
        Dim ch As Integer = Asc(e.KeyChar)

        If (ch >= 48 AndAlso ch <= 57) Or ch = 8 Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

then add eventhandler for text box,

suppose ,in textbox txtId you just want to allow numeric value then you can do 

 AddHandler txtId.KeyPress, AddressOf  NumbericText