Meilleur auteur de réponses
Ouvrir un formulaire à partir d'un autre

Question
-
Bonjour,
j'aimerais réaliser les opérations suivantes:
1- ouverture du formualire de connexion
2- si connexion OK alors ouverture du programme (Fentre du programm) et fermeture de la fentre de connexion
voila mon code
private void buttonconnexion_Click(object sender, EventArgs e) { if (txtLogin .Text.Trim().Length != 0 && txtpwd .Text.Trim().Length != 0) { var Return = new SqlParameter { ParameterName = "@RetunValue", SqlDbType = SqlDbType.Int , Direction = ParameterDirection.Output }; ExecuteRequete.ExecuteStoredProcedure("VerifConnexion", new SqlParameter("@login", txtLogin.Text.Trim()), new SqlParameter("@pwd", txtLogin.Text.Trim()), Return ); if (Return.Value.ToString()=="1" ) { UserCourant = this.txtLogin .Text.Trim(); Properties.Settings.Default.OperateurCourant = this.txtLogin.Text.Trim(); Properties.Settings.Default.Save(); // lance la page principale using (F_Main MaPage = new F_Main()) { MaPage.Show(); } this.Close(); } else { MessageBox.Show("Login et/ou mot de passe incorrect", "SAD-CSR", MessageBoxButtons.OK, MessageBoxIcon.Error); txtLogin.Focus(); } } else { MessageBox.Show("Login et/ou mot de passe non renseigné", "SAD-CSR", MessageBoxButtons.OK, MessageBoxIcon.Error); txtLogin.Focus(); return; } }
Seulement le formulaire Mapage ne s'affiche pas (en fait il y'a un bref splash du formul ire, puis le formulaire de connexion se ferme)
je dois certainement mal ecrire mes instructions mais je n'arrive pas à détecter l'erreur...
Merci
Marcelle NGOUNOU
- Modifié marclas mardi 4 septembre 2012 15:53
Réponses
-
bonjour,
après correction,
j'ai ceci :
1-modification du program principal
namespace Prj_RECLACSR { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { if (TestSiApplicationDejaLancee()) { MessageBox.Show("L'application est déjà ouverte!", "SAD-CSR", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } else { Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR",false ); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); FormConnexion fLogin = new FormConnexion(); if (fLogin.ShowDialog() == DialogResult.Yes) { if (fLogin.Reponse == 1) { Application.Run(new F_Main()); }else { fLogin.ShowDialog(); } } else { fLogin.ShowDialog(); } } } static bool TestSiApplicationDejaLancee() { // Récupération du processus courant System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess(); // Parcours de la liste des processus de la machine foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses()) { // Test si le processus courant est bien différent du processus de la liste et que les deux processus en question ont le même nom if (p.Id != currentProcess.Id && p.ProcessName.Equals(currentProcess.ProcessName) == true) return true; } return false; } } }
2- Mon bouton "connexion du formulaire devient
private void buttonconnexion_Click(object sender, EventArgs e) { if (txtLogin .Text.Trim().Length != 0 && txtpwd .Text.Trim().Length != 0) { var Return = new SqlParameter { ParameterName = "@RetunValue", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output }; ExecuteRequete.ExecuteStoredProcedure ("VerifConnexion", new SqlParameter("@login", txtLogin.Text.Trim()), new SqlParameter("@pwd", txtpwd.Text.Trim()), Return ); if (Return.Value .ToString() =="1" ) { UserCourant = this.txtLogin .Text.Trim(); Properties.Settings.Default.OperateurCourant = this.txtLogin.Text.Trim(); Properties.Settings.Default.Save(); Reponse = 1; } else { MessageBox.Show("Login et/ou mot de passe incorrect", "SAD-CSR", MessageBoxButtons.OK, MessageBoxIcon.Error); Reponse = 0; txtLogin.Focus(); } } else { MessageBox.Show("Login et/ou mot de passe non renseigné", "SAD-CSR", MessageBoxButtons.OK, MessageBoxIcon.Error); txtLogin.Focus(); return; }
Et tout est OK!
Merci
Marcelle NGOUNOU
- Marqué comme réponse marclas mercredi 5 septembre 2012 03:11
Toutes les réponses
-
bonjour
en remplacant
using (F_Main MaPage = new F_Main()) { MaPage.Show(); } this.Close();
par
using (F_Main MaPage = new F_Main()) { MaPage.ShowDialog(); } this.Close();
c'est bien Ok mais le formulaire de connection reste visible!???
Merci de votre aide
Marcelle NGOUNOU
-
Bonjour,
Vous devez tout simplement faire :
MaPage.Close();
Cordialement
Gilles TOURREAU - MVP C#
Architecte logiciel/Consultant/Formateur Freelance
Blog : http://gilles.tourreau.fr
- MCPD : Enterprise Developper / Windows Developper 3.5 / ASP .NET 3.5/4.0
- MCITP : SQL Server 2008 Developper
- MCTS : ADO .NET 3.5 / SQL Server 2008 Developper / Windows Forms 3.5 / ASP .NET 3.5/4.0 -
bonjour,
après correction,
j'ai ceci :
1-modification du program principal
namespace Prj_RECLACSR { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { if (TestSiApplicationDejaLancee()) { MessageBox.Show("L'application est déjà ouverte!", "SAD-CSR", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } else { Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR",false ); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); FormConnexion fLogin = new FormConnexion(); if (fLogin.ShowDialog() == DialogResult.Yes) { if (fLogin.Reponse == 1) { Application.Run(new F_Main()); }else { fLogin.ShowDialog(); } } else { fLogin.ShowDialog(); } } } static bool TestSiApplicationDejaLancee() { // Récupération du processus courant System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess(); // Parcours de la liste des processus de la machine foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses()) { // Test si le processus courant est bien différent du processus de la liste et que les deux processus en question ont le même nom if (p.Id != currentProcess.Id && p.ProcessName.Equals(currentProcess.ProcessName) == true) return true; } return false; } } }
2- Mon bouton "connexion du formulaire devient
private void buttonconnexion_Click(object sender, EventArgs e) { if (txtLogin .Text.Trim().Length != 0 && txtpwd .Text.Trim().Length != 0) { var Return = new SqlParameter { ParameterName = "@RetunValue", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output }; ExecuteRequete.ExecuteStoredProcedure ("VerifConnexion", new SqlParameter("@login", txtLogin.Text.Trim()), new SqlParameter("@pwd", txtpwd.Text.Trim()), Return ); if (Return.Value .ToString() =="1" ) { UserCourant = this.txtLogin .Text.Trim(); Properties.Settings.Default.OperateurCourant = this.txtLogin.Text.Trim(); Properties.Settings.Default.Save(); Reponse = 1; } else { MessageBox.Show("Login et/ou mot de passe incorrect", "SAD-CSR", MessageBoxButtons.OK, MessageBoxIcon.Error); Reponse = 0; txtLogin.Focus(); } } else { MessageBox.Show("Login et/ou mot de passe non renseigné", "SAD-CSR", MessageBoxButtons.OK, MessageBoxIcon.Error); txtLogin.Focus(); return; }
Et tout est OK!
Merci
Marcelle NGOUNOU
- Marqué comme réponse marclas mercredi 5 septembre 2012 03:11