PDA

View Full Version : [C#]LoginForm e MainForm


RaouL_BennetH
13-10-2008, 17:32
Salve a tutti :)

Allora, ho questa situazione:

1 form di login
1 main form

Vorrei che dopo aver effettuato il login sull'apposito form, il main form venisse abilitato.

I tre sorgenti sono:


//Program.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace TestPSql
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}



//MainForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestPSql
{
public partial class MainForm : Form
{


public MainForm()
{
InitializeComponent();
}

private void MainForm_Load(object sender, EventArgs e)
{
this.Enabled = false;
LoginForm f = new LoginForm();
f.ShowDialog();

}
}
}



//LoginForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestPSql
{
public partial class LoginForm : Form
{

public LoginForm()
{
InitializeComponent();
}

private bool IsNotEmpty(TextBox t)
{

if (t.Text.Length == 0)
{
LoginError.SetError(t, "CAMPO OBBLIGATORIO");
return false;
}
else
{
LoginError.SetError(t, "");
return true;

}
}





private void btnLogin_Click(object sender, EventArgs e)
{
if (IsNotEmpty(txtUserName) && IsNotEmpty(txtPassword))
{
try
{
DbConnection db = new DbConnection();
using (Npgsql.NpgsqlConnection cn = new Npgsql.NpgsqlConnection(db.ConnectionString))
{
cn.Open();
Npgsql.NpgsqlParameter[] p = new Npgsql.NpgsqlParameter[2];
p[0] = new Npgsql.NpgsqlParameter("@user", DbType.String);
p[0].Value = txtUserName.Text;
p[1] = new Npgsql.NpgsqlParameter("@password", DbType.String);
p[1].Value = txtPassword.Text;
string loginString = "SELECT COUNT(id_operatore) FROM t_operatori WHERE username = @user AND password = md5(@password)";
Npgsql.NpgsqlCommand cmd = new Npgsql.NpgsqlCommand(loginString, cn);
foreach (Npgsql.NpgsqlParameter par in p)
{
cmd.Parameters.Add(par);
}
int i = Int32.Parse(cmd.ExecuteScalar().ToString());
if (i > 0)
{
LoginError.SetError(panError, "");
this.Close();


}
else
{
LoginError.SetError(panError, "AUTENTICAZIONE FALLITA");

}
}
}
catch (Npgsql.NpgsqlException ex)
{
LoginError.SetError(panError, ex.ErrorSql);

}
}

}



private void btnCancel_Click(object sender, EventArgs e)
{
Application.Exit();
}

}
}


Grazie a tutti :)

RaouL.

RaouL_BennetH
13-10-2008, 17:53
mmm.. al momento ho risolto cosė:



foreach(Form f in Application.OpenForms)
{
if(f.IsMdiContainer && !f.IsDisposed)
{
f.Enabled = true;
}
}



Ovvio che se qualcuno ha un metodo pių elegante/efficiente ben venga :)

RaouL.