Removing only the X from a windows form

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Solutions ArchitectCommented:
That isn't, in no way, easy to do... considering it's possible...
That is an OS imposition... it will be quite dificult to overcome that.

My advise...
1. Create an inherited System.Windows.Forms BaseForm.
2. Set it to have no boerder at all.
3. Draw the border and the buttons like you want.
          If all the forms on the application inherit from this BaseForm the "look" will be consistent.

This is the closest I can get to your will...

Alex :p
agree with above, just a little comment.
even if you can't hide this X,  you still can do a littl trick... Implement your on Closing event for your form and cancel the operations:

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
      e.Cancel = true;

then, if you don't want to use X for closing, I'd assume you would have a button probably, so just in the button handler:

private void button1_Click(object sender, System.EventArgs e)

the two above will not interfere with each other.
You can not remove 'X' button but yes you can disable the 'X' button so user wouldn't be able to click it. If that's what you are looking for then let me know.

Here is a VB.Net example (sorry, I don't have a C# one handy) that does what you want...

    Const MF_BYPOSITION As Integer = &H400
    Const MF_DISABLED As Integer = 2
    Const MF_GRAYED As Integer = 1
    Private Declare Function GetSystemMenu Lib "User32" (ByVal hWnd As IntPtr, ByVal bRevert As Integer) As IntPtr
    Private Declare Function GetMenuItemCount Lib "User32" (ByVal hMenu As IntPtr) As Integer
    Private Declare Auto Sub GetMenuString Lib "User32" (ByVal hMenu As IntPtr, ByVal uIDItem As Integer, ByVal lpString As String, ByVal nMaxCount As Integer, ByVal uFlag As Integer)
    Private Declare Sub DrawMenuBar Lib "User32" (ByVal hMenu As IntPtr)
    Private Declare Sub EnableMenuItem Lib "User32" (ByVal hMenu As IntPtr, ByVal uIDEnableItem As Integer, ByVal uEnable As Integer)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim hWnd, hMenu As IntPtr
        Dim buf As String
        Dim i, c As Integer

        hWnd = Me.Handle
        hMenu = GetSystemMenu(hWnd, False)
        c = GetMenuItemCount(hMenu)

        buf = Space(256)
        For i = 0 To c - 1
            GetMenuString(hMenu, i, buf, 255, MF_BYPOSITION)
            If buf.StartsWith("&Close") Then
                EnableMenuItem(hMenu, i, MF_BYPOSITION Or MF_DISABLED Or MF_GRAYED)
                Exit For
            End If
    End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oops, that example will just disable the X... to remove it you'd use

             RemoveMenu(hMenu, i, MF_BYPOSITION)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.