Context menu for a bordless form

daemon

Member
Joined
Jan 18, 2007
Messages
10
Programming Experience
Beginner
Hello all!
I have created a bordless movable form and then I tried to create a context menu for it. The problem is that the context menu doesn't appear where it suppose to(where the cursor is)...This is an example:
X context menu bound:481
X mouse:243
As you can see, the menu is showing below and on the right.


Also, I have another problem...I'm starting my application with Sub Main().
In Sub Main() I run a form...frmLogin with Application.Run(frmLogin). After I insert my credentials in that form and hit Login, a new form should come up(frmAccounts).
If I try in frmLogin: frmAccounts.Show() and Me.Close, frmAccounts flashes for a sec and then the entire app ends.
I don't want to use frmAccounts.ShowDialog(). Also, I don't want to use the Hide method for frmLogin.
Application.Run(frmAccounts) is not an option because this is not working...

Can anyone help me with these please? I'm just a beginner.
 
Your Primary Platform is currently set to .NET 2.0 (VS2005). Are you indeed using .NET 2.0? I ask because in order to start from a Sub named Main in the 2.0 framework you must disable the standard application framework.

The way in which you would handle this would be very different depending on the framework you are using.

If you are using framework 1, you could call frmLogin using ShowDialog and handle the login validation directly in the Sub named Main. If the validation succeeds then show frmAccounts with code in the Sub named Main.
 
I'm using VS2005, and yes, I did disable the standard application framework because I need to start with Sub Main() in order to know what form should I load next. Is there another way to do this?
 
The last statement I posted above also applies to framework 2 with the standard application framework disabled.
Open frmLogin using ShowDialog; the user enters the login information and clicks the button to login which returns a dialog result of OK; in sub main get the information from frmLogin and do validation, if validation succeeds show frmAccounts if not show frmLogin again to let the user attempt to login again.
 
And here's example code for an alternate method. frmLogin has two textboxes named txtUserName and txtPassWord; a label named lblMsg to show error messages; a button named cmdCancel with Dialog result set to Cancel so the user can cancel the login; and another button named cmdOK.
VB.NET:
Public Class frmLogin

    Private Function ValidateCredentials() As Boolean
        If txtUserName.Text = "Paszt" AndAlso txtPassword.Text = "mypassword" Then
            Return True
        End If
        Return False
    End Function

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        If ValidateCredentials() Then
            Me.DialogResult = Windows.Forms.DialogResult.OK
        Else
            Me.lblMsg.Text = "invalid credentials"
        End If
    End Sub
End Class

Friend Class StartUp

    Public Shared Sub main()
        Dim login As New frmLogin
        If login.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim Accounts As New frmAccounts
            Accounts.ShowDialog()
        End If
    End Sub

End Class
Why don't you want to use frmAccounts.ShowDialog?
Application.Run(frmAccounts) is not an option because this is not working...
If something doesn't work, you will get beter assistance if you say why it doesn't work :).
 
Hello all!
I have created a bordless movable form and then I tried to create a context menu for it. The problem is that the context menu doesn't appear where it suppose to(where the cursor is)...This is an example:
X context menu bound:481
X mouse:243
As you can see, the menu is showing below and on the right.
.

When you show a context menu, you need to tell it where to appear.. usually you do this with some translation of the screen coordinates of the mouse. Show the code you use for showing your menu?
 
Here't the code for context menu:
cMenu.Show(Me, getCursor)
VB.NET:
Public Function getCursor() As System.Drawing.Point
        Dim curPoint As New Point(0, 0)
        curPoint.X = Windows.Forms.Cursor.Position.X
        curPoint.Y = Windows.Forms.Cursor.Position.Y
        Return curPoint
End Function
 
Last edited by a moderator:
Windows.Forms.Cursor.Position is screen coordinates. You want show the menu relative to Form location, so you use the Me.PointToClient function method to translate the screen point to client point.
 
Back
Top