Friday, December 19, 2008

How to disable close button from window form using .NET application

How to disable window close button ("X") in .NET


VB.NET

                       

To disable close button from the window form using vb.net needs some Dlls from system library. Call DisableCloseButton(me.Handle) function from your window form button click event or from where you want to disable "x" button.

Code-1

 

Private Const MF_BYPOSITION As Int32 = &H400

Private Const MF_REMOVE As Int32 = &H1000


Private Declare Auto Function GetSystemMenu Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal bRevert As Int32) As IntPtr


Private Declare Auto Function GetMenuItemCount Lib "user32.dll" (ByVal hMenu As IntPtr) As Int32


Private Declare Function DrawMenuBar Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean


Private Declare Auto Function RemoveMenu Lib "user32.dll" (ByVal hMenu As IntPtr, ByVal nPosition As Int32, ByVal wFlags As Int32) As Int32


'This method will be used to disable the Form "X" button

Public Shared Sub DisableCloseButton(ByVal hwnd As IntPtr)

Dim hMenu As IntPtr, n As Int32

hMenu = GetSystemMenu(hwnd, 0)

If Not hMenu.Equals(IntPtr.Zero) Then

              n = GetMenuItemCount(hMenu)

     If n > 0 Then

                   RemoveMenu(hMenu, n - 1, MF_BYPOSITION Or MF_REMOVE)

         RemoveMenu(hMenu, n - 2, MF_BYPOSITION Or MF_REMOVE)

                   DrawMenuBar(hwnd)

     End If

End If

End Sub

Code-2

Another way to disable close button from window form , override  "CreateParams" property. You have to Include following code in your in your window form.

     Private Const CP_NOCLOSE_BUTTON As Integer = &H200

     Protected Overloads Overrides ReadOnly Property CreateParams() As    CreateParams

         Get

             Dim myCp As CreateParams = MyBase.CreateParams

    myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON

              Return myCp

End Get

     End Property

C#.NET

In C#.net its simple just override "CreateParams" property, that's all.

Including following code in your in your window form.

private const int CP_NOCLOSE_BUTTON = 0x200;

protected override CreateParams CreateParams

{

   get

   {

      CreateParams myCp = base.CreateParams;

      myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;

      return myCp;

    }

 }

Monday, December 15, 2008

How to play sound with .NET

Play different sound with .NET

You can make some noise in .NET application using VB/C# .NET code. There are multiple ways to do this, I am giving one of them; hope it's fulfill your requirement.

VB.NET

'You can add some more sound as per your requirement

Public Enum MessageBeepType

[Default] = -1

OK = &H0

[Error] = &H10

Question = &H20

Warning = &H30

Information = &H40

End Enum

' This allows you to 'Beep' the sounds declared in the MessageBeepType

<Runtime.InteropServices.DllImport("USER32.DLL", setlasterror:=True)> _

Public Shared Function MessageBeep(ByVal type As MessageBeepType) As Boolean

End Function

' Allowing you to specify the frequency and duration of the sound.
<Runtime.InteropServices.DllImport("KERNEL32.DLL")> _
Public Shared Function Beep(ByVal frequency As Integer, ByVal duration As Integer) As Boolean
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MessageBeep(MessageBeepType.Default)

End Sub

C#.NET

Public Enum MessageBeepType

{

Default = -1,

OK = 0x0,

Error = 0x10,

Question = 0x20,

Warning = 0x30,

Information = 0x40

}

//This allows you to 'Beep' the sounds declared in the MessageBeepType

[Runtime.InteropServices.DllImport("USER32.DLL", setlasterror = true)]

public static bool MessageBeep(MessageBeepType type)

{

}

' Allowing you to specify the frequency and duration of the sound.

[Runtime.InteropServices.DllImport("KERNEL32.DLL")]
public static bool Beep(int frequency, int duration)

{

}

Button1_Click(object sender, System.EventArgs e)

{

MessageBeep(MessageBeepType.Default);

}

Tuesday, December 02, 2008

How to add Image in the DatagridView using VB.NET

How to add image or icon in the DatagridView column using vb.net or c#.net

VB.NET

Dim dgImage As New DataGridViewImageColumn
dgImage.Image = Drawing.Image.FromFile("C:\FixedOption.ico")
dgImage.ReadOnly = True
dgImage.ImageLayout = DataGridViewImageCellLayout.Normal
dgDataGridView.Columns.Add(dgImage)

'After This you can bind the data grid with any dataset,if you want
dgDataGridView.DataSource = DataSet

C#.NET

dgDataGridView.Rows.Clear();

DataGridViewImageColumn dgImage = new DataGridViewImageColumn();

dgImage.Image = Drawing.Image.FromFile("C:\\IconLocation\\FixedOption.ico");

dgImage.ReadOnly = true;

dgImage.ImageLayout = DataGridViewImageCellLayout.Normal;

dgDataGridView.Columns.Add(dgImage);

//After This you can bind the data grid with any dataset

dgDataGridView.DataSource = BindDataSet;