Compartir una List entre dos formularios Windows Form C#
Publicado por Emanuel (4 intervenciones) el 23/06/2019 16:28:27
]Saludos. tengo un problema y es que estoy haciendo un programa con windows form (un login de registro y verificacion)en diferentes formularios Mi problema es que no puedo pasar mi list de Usuarios de un formulario a otro, puedo registrar pero no verificar , Es decir mi Que en un formulario estoy registrando a mis usuarios y en el otro verifico su existencia pero no puedo acceder a la list del otro.Gracias
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//form del login donde registro usuarios
namespace Inicio
{
public partial class Login : Form
{
List<Usuario> lista = new List<Usuario>();
public Login()
{
InitializeComponent();
}
public static byte[] GetHash(string imputString)
{
HashAlgorithm algoritm = SHA256.Create();
return algoritm.ComputeHash(Encoding.UTF8.GetBytes(imputString));
}
public static string getHashString(string inputString)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in GetHash(inputString))
sb.Append(b.ToString("X2"));
return sb.ToString();
}
private void IngresarDatos()
{
Usuario usuario = new Usuario();
usuario.login = txtUsuario.Text;
usuario.clave = getHashString(txtClave.Text.Trim());
lista.Add(usuario);
limpiar();
}
private void limpiar()
{
txtUsuario.Text = string.Empty;
txtClave.Text = string.Empty;
txtUsuario.Focus();
}
public bool autenticar()
{
foreach (var item in lista)
{
if (item.login == txtUsuario.Text.Trim() && item.clave == getHashString(txtClave.Text.Trim()))
{
return true;
}
}
return false;
}
private bool autenticar1()
{
var dato = lista.AsParallel().Where(x => x.login == txtUsuario.Text.Trim() && x.clave == getHashString(txtClave.Text.Trim())).FirstOrDefault();
if (dato != null) return true;
else return false;
}
private void btnRegistrar_Click(object sender, EventArgs e)
{
IngresarDatos();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//form donde deberia verificar la existencia de dichos usuarios son tres formulario en total ...un inicio --verificar y login
namespace Inicio
{
public partial class Verificar : Form
{
public Verificar()
{
InitializeComponent();
}
public static byte[] GetHash(string imputString)
{
HashAlgorithm algoritm = SHA256.Create();
return algoritm.ComputeHash(Encoding.UTF8.GetBytes(imputString));
}
public static string getHashString(string inputString)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in GetHash(inputString))
sb.Append(b.ToString("X2"));
return sb.ToString();
}
public bool autenticar()
{
foreach (var item in lista)
{
if (item.login == txtVUsuario.Text.Trim() && item.clave == getHashString(txtVClave.Text.Trim()))
{
return true;
}
}
return false;
}
private void btnVerificar_Click(object sender, EventArgs e)
{
if (autenticar())
{
MessageBox.Show("AUTENTICADO");
}
else MessageBox.Show("INGRESO NO AUTORIZADO");
}
}
}
Valora esta pregunta


0