i can not close the main form?
-
Dienstag, 22. Dezember 2009 09:05hi friends
In my application, i have two forms loginform and form2.if login is successful the form2 will be display otherwise login failed.after form2 loaded the loginform should be closed but i can not close the log in form. if i close the login form entire application is close. the main entry is login form.
i need to close login form after form2 displayes. please any one give me the solution
thank you
faisal
Alle Antworten
-
Dienstag, 22. Dezember 2009 09:25hi,
There are two ways to make.........one is through MDI parent child releationship. where you have third form which acts as a parent and login form and form2 acts as achild form....
MianForm:
public static LoginForm loginForm;
private void Mainform_Load(oobject sender,Eventargs e)
{
loginForm =new LoginForm();
loginForm.Owner= this;
loginForm.Parent =this;
loginForm.Show();
}
LoginForm:
private void btnLogin(object sender, Eventargs e)
{
//your code to validae user
if(login =="Success")
{
form2 frm=new form2();
frm.Show();
}
}
Form2:
private void form2_Load(oobject sender,Eventargs e)
{
MainForm.loginForm.Close();
}
////Other way is with out MDI
LoginForm:
public static LoginForm loginForm;
private void btnLogin(object sender, Eventargs e)
{
//your code to validae user
if(login =="Success")
{
loginForm = this;
form2 frm = new form2();
frm.Show();
}
}
Form2:
private void form2_Load(oobject sender,Eventargs e)
{
LoginForm.loginForm.Close();
}
Nagarjuna Dilip -
Mittwoch, 23. Dezember 2009 07:12thanks for ur reply
can i disable the closebox in form. -
Mittwoch, 23. Dezember 2009 07:22hi,
yes, you can set closebox to false..............
Nagarjuna Dilip -
Mittwoch, 23. Dezember 2009 07:36how can i set close box to false.
please explain me -
Mittwoch, 23. Dezember 2009 07:44
here is the sample that shows how to disable Close button in window form, it also disable the close in Window Context Menuusing System.Runtime.InteropServices; namespace WindowsApplication1 { public partial class Form2 : Form { const int MF_BYPOSITION = 0x400; [DllImport("User32")] private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags); [DllImport("User32")] private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); [DllImport("User32")] private static extern int GetMenuItemCount(IntPtr hWnd); public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { IntPtr hMenu = GetSystemMenu(this.Handle, false); int menuItemCount = GetMenuItemCount(hMenu); RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION); } } }hope this helped...
Narayanan Dayalan - Zeetaa Business Solutions ------- Please "Mark As Answer", if my answer works well with ur Query- Bearbeitet Narayanan Dayalan Mittwoch, 23. Dezember 2009 09:35
- Als Antwort vorgeschlagen Michael Sun [MSFT]Microsoft Employee, Moderator Mittwoch, 23. Dezember 2009 15:13
-
Mittwoch, 23. Dezember 2009 08:43this.ControlBox = false;
Nagarjuna Dilip -
Mittwoch, 23. Dezember 2009 08:53thank you for your reply
it shows following error
Error 1 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?) D:\VC#.Net\datavicoding\datavicoding\Form2.cs 16 10 datavicoding -
Mittwoch, 23. Dezember 2009 08:56
for the above post error this is the solution...........
using System.Runtime.InteropServices;
but why do you use.............dat.....try this,........... this.ControlBox = false;
Nagarjuna Dilip- Als Antwort markiert faisal abdul Mittwoch, 23. Dezember 2009 11:16
-
Mittwoch, 23. Dezember 2009 11:07
Hi faisal abdul,
If you just want to disable the CloseBox and leave the MaximizeBox and MinimizeBox enabled, you can refer
to Narayanan Dayalan's sample code, or you can try the following code:using System.Runtime.InteropServices;
public partial class Form1 : Form
{[DllImport("USER32.DLL")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, UInt32 bRevert);
[DllImport("USER32.DLL")]
private static extern UInt32 RemoveMenu(IntPtr hMenu, UInt32 nPosition, UInt32 wFlags);
private const UInt32 SC_CLOSE = 0x0000F060;
private const UInt32 MF_BYCOMMAND = 0x00000000;
public Form1()
{
InitializeComponent();IntPtr hMenu = GetSystemMenu(this.Handle, 0);
RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
}
}If you want to make all the MaximizeBox, MinimizeBox and the CloseBox disabled(actually disappeared), then you can follow Nagarjuna Dilip's suggestion, set ControlBox property as false.
Hope this helps.
Best regards,
winco_pipo- Als Antwort vorgeschlagen Michael Sun [MSFT]Microsoft Employee, Moderator Mittwoch, 23. Dezember 2009 15:13
-
Mittwoch, 23. Dezember 2009 11:17thankyou
it is working. -
Samstag, 18. August 2012 18:57in the proprties of the login form make control box to false

