Impedir que un form se cierre csharp,vb.net
->
A continuación, este código te ayudará para evitar que un formularion se cierre sin previa validación, para esto usamos la evento FormClosing de los formulacion de Windows Forms.
En Visual Basic .Net es de esta manera
-
Private Sub forma_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
-
Dim valor1 As [Decimal]
-
Dim valor2 As [Decimal]
-
valor1 = Convert.ToDecimal(txtval1.Text)
-
valor2 = Convert.ToDecimal(txtval2.Text)
-
If valor1 <> valor2 Then
-
MessageBox.Show(Me, "Los valore deben coincidir", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
-
e.Cancel = True 'evita que el form se cierre
-
-
End If
-
End Sub
-
private void forma_FormClosing(object sender, FormClosingEventArgs e)
-
{
-
Decimal valor1;
-
Decimal valor2;
-
valor1 = Convert.ToDecimal(valor1.Text);
-
valor2 = Convert.ToDecimal(valor2.Text);
-
if (valor1 != valor2)
-
{
-
MessageBox.Show(this, "Los valores deben coincidir", "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
-
e.Cancel = true; //evita que el form se cierre
-
}
-
-
}
Un código bastante útil en el tema de validaciones y prohibiciones para que esas ocaciones que se necesita aplicar seguridad en los formularios.
Nos vemos.
Valida TextBox si es Entero o Decimal
->
Bueno estas funciones ayudan a validar si el texto ingresado en un textbox es un valor numérico ó es un valor decimal, estas son de las validaciones más usadas para desarrollar así que acá se los dejo.
-
Public Shared Function EsInteger(ByVal theValue As String) As Boolean
-
'funcion para enteros
-
Try
-
Convert.ToInt32(theValue)
-
Return True
-
Catch
-
Return False
-
End Try
-
End Function
-
-
Public Shared Function EsDecimal(ByVal theValue As String) As Boolean
-
'funcion para enteros
-
Try
-
Convert.ToDecimal(theValue)
-
Return True
-
Catch
-
Return False
-
End Try
-
End Function
-
public static bool EsInteger(string theValue)//funcion para enteros
-
{
-
try
-
{
-
Convert.ToInt32(theValue);
-
return true;
-
}
-
catch
-
{
-
return false;
-
}
-
}
-
-
public static bool EsDecimal(string theValue)//funcion para enteros
-
{
-
try
-
{
-
Convert.ToDecimal(theValue);
-
return true;
-
}
-
catch
-
{
-
return false;
-
}
-
}
otra manera de validar de parte de @cmsalvado gracias por el dato:
-
Public Shared Function EsInteger(ByVal theValue As String) As Boolean
-
Dim value As Integer
-
Return Integer.TryParse(theValue, value)
-
End Function
-
public static bool EsInteger(string theValue)
-
{
-
int value;
-
return int.TryParse(theValue, out value);
-
}
bueno espero les sea de gran ayuda.
nos vemos
validar si hay texto en un textbox vb.net c#
->
Esta código ayuda a revisar si existe un texto ingresado en un textbox en VB.net y C#.
-
If TextBox.Text.Length <1 Then
-
MessageBox.Show(Me, "Debe colocar un cliente válido", "error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
-
Return
-
End If
-
if (TextBox.Text.Length <1)
-
{
-
MessageBox.Show(this, "Debe colocar un cliente válido", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
-
return;
-
}
nos vemos espero les sirva algo sencillo pero muy útil.




























