Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 13
  1. #1

    [C#]Chiudere un Form al caricamento di un altro

    Salve a tutti..
    Ho creato un progrmma che all'inizio esegue un'autenticazione, e se i dati sono corretti carica un secondo Form..
    Form2
    Vorrei che al caricamento del Form2 si chiudesse il Form1.. quello del Login..
    Codice PHP:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace 
    WindowsFormsApplication1
    {
        public 
    partial class Form2 Form
        
    {
            public 
    Form2()
            {
                
    InitializeComponent();
                
            }

            private 
    void Form2_Load(object senderEventArgs e)
            {
                
    Form1 frm = new Form1();
                
    frm.Close();
            }
        }

    In questo modo non funziona ovviamente...
    Avevo pensato anche di chiuderlo direttamente dallo stesso Form1 con
    Codice PHP:
    this.Close(); 
    Ma si chiude l'intero processo..

    Sapete dirmi come fare???

  2. #2
    Utente di HTML.it L'avatar di Stoicenko
    Registrato dal
    Feb 2004
    Messaggi
    2,254
    per fare quello che chiedi devi invertire i form.. il primo deve essere quello principale, nel load carichi il secondo che fa il login che poi si chiude e ritorna al primo..

  3. #3
    ok... come posso farlo?? devo aprire un sorgente?

  4. #4
    Utente di HTML.it L'avatar di Stoicenko
    Registrato dal
    Feb 2004
    Messaggi
    2,254
    codice:
    devo aprire un sorgente?
    e questo che vuol dire?

    fai cosė
    codice:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Form2 frm = new Form2();
                frm.ShowDialog();
            }
        }
    }
    e nel form2 fai il login

  5. #5
    ok.. ho trovato il modo di avviare con il programma il form2 come principale, da esso richiamo il Form1...
    ma ora.. c'č un modo per nascondere il Form2 e bloccarlo fino a quando non lo richiamo con il form1?

  6. #6
    Utente di HTML.it L'avatar di 22kakā
    Registrato dal
    Feb 2008
    Messaggi
    160
    hai provato ad utilizzare la funzione Form2.Hide???

  7. #7
    Ok la maggior parte dei problemi li ho risolti.. vi posto i codici dei due Form:
    Form 1
    Codice PHP:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using MySQLDriverCS;
    using System.Configuration;


    namespace 
    WindowsFormsApplication1
    {
        public 
    partial class Form1 Form
        
    {
            public 
    Form1()
            {
                
    InitializeComponent();
            }

            private 
    void button2_Click(object senderEventArgs e)
            {            
                
    string host ConfigurationSettings.AppSettings["host"];
                
    string muser ConfigurationSettings.AppSettings["muser"];
                
    string mpsw ConfigurationSettings.AppSettings["mpsw"];
                
    string db ConfigurationSettings.AppSettings["db"];
                
    string port ConfigurationSettings.AppSettings["port"];
                
    MySQLConnection conn = new MySQLConnection("Location="+host+";User ID="+muser+";Password="+mpsw+";Data Source="+db+";Port="+port+"");
                
    string user textBox1.Text;
                
    string psw textBox2.Text;
                
    MySQLCommand cmd = new MySQLCommand("SELECT pwd FROM D_users WHERE ( user = \"" user "\" )"conn);
                
    string prova "";
                try
                {
                    
    conn.Open();
                    
    MySQLDataReader mread cmd.ExecuteReaderEx();
                    while (
    mread.Read())
                    {
                        
    prova mread.GetString(0);
                    }
                }
                catch (
    Exception g)
                {
                    
    MessageBox.Show("Errore : " "");
                }
                
                if (
    psw == "")
                {
                    
    MessageBox.Show("La password inserita non corrisponde all'username, prova a correggere i dati o contatta l'amministratore");
                }
                else if (
    psw == prova)
                {
                    
    Form2 frm = new Form2();
                    
    frm.ver true;
                    
    frm.controllo();
                    
    this.Close();                
                }
                else
                {
                    
    MessageBox.Show("La password inserita non corrisponde all'username, prova a correggere i dati o contatta l'amministratore");
                }
            }

            private 
    void button1_Click(object senderEventArgs e)
            {
                
    Form3 frm = new Form3();
                
    frm.Show();
            }
        }

    Form 2:
    Codice PHP:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using MySQLDriverCS;
    using System.Configuration;

    namespace 
    WindowsFormsApplication1
    {
        public 
    partial class Form2 Form
        
    {
            public 
    bool ver;
            public 
    void controllo()
            {
                if (
    ver == false)
                {
                    
    MessageBox.Show("Non hai effettuato il login" "\r\n" "Non hai i diritti necessari per effettuare una ricerca");
                    
    this.Close();
                }
            }

            public 
    Form2()
            {            
                
    InitializeComponent();           
            } 

            private 
    void Form2_Load(object senderEventArgs e)
            {
                
    Form1 frm1 = new Form1();
                
    frm1.ShowDialog();            
            }

            private 
    void button1_Click(object senderEventArgs e)
            {
                
    this.controllo();
                
    string host ConfigurationSettings.AppSettings["host"];
                
    string muser ConfigurationSettings.AppSettings["muser"];
                
    string mpsw ConfigurationSettings.AppSettings["mpsw"];
                
    string db ConfigurationSettings.AppSettings["db"];
                
    string port ConfigurationSettings.AppSettings["port"];
                
    MySQLConnection conn = new MySQLConnection("Location=" host ";User ID=" muser ";Password=" mpsw ";Data Source=" db ";Port=" port "");            
                
    decimal tmp numericUpDown1.Value;
                
    int dd Convert.ToInt32(tmp);
                
    tmp numericUpDown2.Value;
                
    int mm Convert.ToInt32(tmp);
                
    tmp numericUpDown3.Value;
                
    int aa Convert.ToInt32(tmp);
                
    string testo "";
                
    string temp;
                
    string riga "=================";            
                
    MySQLCommand cmd = new MySQLCommand("SELECT materia,compiti FROM d_compiti WHERE ( giorno = \"" aa "-" mm "-" dd "\" )"conn);
                try
                {
                    
    conn.Open();
                    
    MySQLDataReader mread cmd.ExecuteReaderEx();
                    while (
    mread.Read())
                    {
                        
    temp mread.GetString(0);
                        
    testo = (testo+"\r\n"+temp+":\r\n");
                        
    temp mread.GetString(1);
                        
    testo = (testo "\r\n" temp "\r\n" +riga );                    
                    }
                    
    textBox1.Text testo;
                }
                catch (
    Exception g)
                {
                    
    MessageBox.Show("Errore : " "");
                }


                
            }

            private 
    void numericUpDown2_ValueChanged(object senderEventArgs e)
            {

            }
        }

    Il problema č che, una volta effettuato il login il form2 , non imposta la bool ver su true..
    Probabilmente sbaglio su come si passa una variabile valorizzata da un form all'altro.. ma cercando su internet mi sembrava si facesse proprio cosė....

  8. #8
    Utente di HTML.it L'avatar di Stoicenko
    Registrato dal
    Feb 2004
    Messaggi
    2,254
    cosė dovrebbe essere giusto ma per sicurezza passala al costruttore

  9. #9
    Evidentemente se ho postato il codice č perchč non funziona

  10. #10
    Utente di HTML.it L'avatar di Stoicenko
    Registrato dal
    Feb 2004
    Messaggi
    2,254
    Hai provato a passare il valore tramite costruttore o direttamente nel metodo?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.