Ejercicios Basicos de C# - Resolviendo
(1/1)
KarlosVid(ÊÇ):
Desarrollando:
Ejercicio 1:
Código:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Btn_Salir_Click(object sender, EventArgs e)
{
Close();
}
private void Btn_Nuevo_Click(object sender, EventArgs e)
{
this.Txt_NomTrab.Text = "";
this.Txt_ApePatTrab.Text = "";
this.Txt_ApeMatTrab.Text = "";
this.Rbt_Mas.Checked = false;
this.Rbt_Fem.Checked = false;
}
//Validación de Digitos Ingresados
//48-57 => (0-9)
//41-60 => (a-z)
//97-122 => (A-Z)
private void Txt_NomTrab_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 41 && e.KeyChar <= 60 || e.KeyChar >= 97 && e.KeyChar <= 122 || e.KeyChar == 32)
{ //Es Letra
}else{
MessageBox.Show("Se Acepta Letras", "Mensaje de Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
e.Handled = true;
}
}//Fin Txt_NomTrab
private void Txt_ApePatTrab_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 41 && e.KeyChar <= 60 || e.KeyChar >= 97 && e.KeyChar <= 122 || e.KeyChar == 32)
{ //Es Letra
}else{
MessageBox.Show("Se Acepta Letras", "Mensaje de Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
e.Handled = true;
}
}//Fin Txt_ApePatTrab
private void Txt_ApeMatTrab_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 41 && e.KeyChar <= 60 || e.KeyChar >= 97 && e.KeyChar <= 122 || e.KeyChar == 32)
{ //Es Letra
}else{
MessageBox.Show("Se Acepta Letras", "Mensaje de Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
e.Handled = true;
}
}//Fin Txt_ApeMatTrab
private void Btn_MostrarResult_Click(object sender, EventArgs e)
{
Boolean valida = false;
this.errorProvider1.SetError(this.Txt_NomTrab, "");
this.errorProvider1.SetError(this.Txt_ApePatTrab, "");
this.errorProvider1.SetError(this.Txt_ApeMatTrab, "");
if (this.Txt_NomTrab.Text.Trim() == "")
{
this.errorProvider1.SetError(this.Txt_NomTrab, "Ingrese Nombre Trabajador");
valida = true;
}
if (this.Txt_ApePatTrab.Text.Trim() == "")
{
this.errorProvider1.SetError(this.Txt_ApePatTrab, "Ingrese Apellido Paterno");
valida = true;
}
if (this.Txt_ApeMatTrab.Text.Trim() == "")
{
this.errorProvider1.SetError(this.Txt_ApeMatTrab, "Ingrese Apellido Materno");
valida = true;
}
if (valida == true)
{
return;
}
else
{///Ok
//this.Lbl_Res_Edad.Text = this.dateTimePicker1.Value+""; //08/08/2008 09:51
//this.Lbl_Res_Edad.Text = this.dateTimePicker1.Value.Day+""; //08
//this.Lbl_Res_Edad.Text = this.dateTimePicker1.Value.Month+""; //08
//this.Lbl_Res_Edad.Text = this.dateTimePicker1.Value.Year + ""; //2008
//this.Lbl_Res_Edad.Text = this.dateTimePicker1.Value.GetDateTimeFormats() + "";
}
}
}
}
Me falta extraer la fecha(dd-mm-yy) :huh: Alguien sabe como hacerlo. La validación ya lo hice.
Saludos, estare agradecido el que me de una mano ayudandome a resolver.
h0oke:
De dónde quieres "extraer" la fecha, de un textbox y almacenarla en una variable?
Pablo Videla:
Por que no usas el calendario para la fecha? es mas simple.
KarlosVid(ÊÇ):
Bueno el profesor es muy simple en dar ejemplos. Pero yo lo voy hacer con un 3 combos. El viernes lo termino Y lo Publico. Les dare un Avance de lo que hice del 1er ejercicio.
Para el sabado voy tratar de terminarlo y lo público en el foro. Aver si me dan unas correcciones o mejoras de codigos. ;)
Código:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//DateTimeFormatInfo myDTFI = new CultureInfo("en-US", false).DateTimeFormat;
DateTime myDT = this.monthCalendar1.TodayDate;//Utilizando un Calendario
this.Lbl_Fecha.Text = myDT.ToString("D");//sábado, 09 de agosto de 2008
////*** LLENANDO DIAS, EN LOS COMBOS
int d;
for (d = 1; d <= 31; d++) {
this.Cbb_Ing_Dia.Items.Add(d);
this.Cbb_Nac_Dia.Items.Add(d);
}
this.Cbb_Ing_Dia.SelectedText = "Día";
this.Cbb_Nac_Dia.SelectedText = "Día";
////*** LLENANDO MESES, EN LOS COMBOS
int m;
String[] Mes={"Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Setiembre","Octubre","Nomviembre","Diciembre"};
for (m = 0; m <= Mes.Length-1; m++)
{
this.Cbb_Ing_Mes.Items.Add(Mes[m]);
this.Cbb_Nac_Mes.Items.Add(Mes[m]);
}
this.Cbb_Ing_Mes.SelectedText = "Mes";
this.Cbb_Nac_Mes.SelectedText = "Mes";
////*** LLENANDO AÑOS, EN LOS COMBOS
int a;
for (a = 1750; a <= 2250; a++) {
this.Cbb_Ing_Anio.Items.Add(a);
this.Cbb_Nac_Anio.Items.Add(a);
}
this.Cbb_Ing_Anio.SelectedText = "Año";
this.Cbb_Nac_Anio.SelectedText= "Año";
}
private void Btn_Salir_Click(object sender, EventArgs e)
{
Close();
}
private void Btn_Nuevo_Click(object sender, EventArgs e)
{
this.Txt_NomTrab.Text = "";
this.Txt_ApePatTrab.Text = "";
this.Txt_ApeMatTrab.Text = "";
this.Rbt_Mas.Checked = false;
this.Rbt_Fem.Checked = false;
}
//Validación de Digitos Ingresados
//48-57 => (0-9)
//41-60 => (a-z)
//97-122 => (A-Z)
private void Txt_NomTrab_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 41 && e.KeyChar <= 60 || e.KeyChar >= 97 && e.KeyChar <= 122 || e.KeyChar == 32)
{ //Es Letra
}else{
MessageBox.Show("Se Acepta Letras", "Mensaje de Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
e.Handled = true;
}
}//Fin Txt_NomTrab
private void Txt_ApePatTrab_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 41 && e.KeyChar <= 60 || e.KeyChar >= 97 && e.KeyChar <= 122 || e.KeyChar == 32)
{ //Es Letra
}else{
MessageBox.Show("Se Acepta Letras", "Mensaje de Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
e.Handled = true;
}
}//Fin Txt_ApePatTrab
private void Txt_ApeMatTrab_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 41 && e.KeyChar <= 60 || e.KeyChar >= 97 && e.KeyChar <= 122 || e.KeyChar == 32)
{ //Es Letra
}else{
MessageBox.Show("Se Acepta Letras", "Mensaje de Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
e.Handled = true;
}
}//Fin Txt_ApeMatTrab
private void Btn_MostrarResult_Click(object sender, EventArgs e)
{
Boolean valida = false;
this.errorProvider1.SetError(this.Txt_NomTrab, "");
this.errorProvider1.SetError(this.Txt_ApePatTrab, "");
this.errorProvider1.SetError(this.Txt_ApeMatTrab, "");
if (this.Txt_NomTrab.Text.Trim() == "")
{
this.errorProvider1.SetError(this.Txt_NomTrab, "Ingrese Nombre Trabajador");
valida = true;
}
if (this.Txt_ApePatTrab.Text.Trim() == "")
{
this.errorProvider1.SetError(this.Txt_ApePatTrab, "Ingrese Apellido Paterno");
valida = true;
}
if (this.Txt_ApeMatTrab.Text.Trim() == "")
{
this.errorProvider1.SetError(this.Txt_ApeMatTrab, "Ingrese Apellido Materno");
valida = true;
}
if (valida == true)
{
return;
}
else
{///Ok
DateTime FechaActual = this.monthCalendar1.TodayDate;
int MesSistema, DiaSistema,AnioActual, Edad, TiempoServ;
String MesEdad,DiaEdad,MesServ,DiaServ;
String Msg_MesServ, Msg_DiaServ, Msg_MesEdad, Msg_DiaEdad;
MesSistema = int.Parse(FechaActual.ToString("MM"));// 8 = Agosto
DiaSistema = int.Parse(FechaActual.ToString("dd"));// 25 = dia
AnioActual = int.Parse(FechaActual.ToString("yyyy"));// 2009 = Año
///***** [[_ FECHA DE NACIMIENTO _]]
if (this.Cbb_Nac_Dia.SelectedValue == "Día" || this.Cbb_Nac_Mes.SelectedValue == "Mes" || this.Cbb_Nac_Anio.SelectedValue == "Año")
{
MessageBox.Show("Seleccione una Fecha de Naciminto Correcta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else { //Ok - Fechas Ingresadas Correctas
if (MesSistema <= (this.Cbb_Nac_Mes.SelectedIndex + 1)){
MesEdad = "";
DiaEdad = "";
}
else
{ //Ok - Fechas Ingresadas Correctas
//MESES
if (MesSistema <= (this.Cbb_Nac_Mes.SelectedIndex + 1))
{
MesEdad = "";
Msg_MesEdad = "";
}
else
{
MesEdad = (MesSistema - (this.Cbb_Nac_Mes.SelectedIndex + 1)) + "";
if (MesEdad == "1")
{//Singular
Msg_MesEdad = " mes";
}
else
{
Msg_MesEdad = " meses";
}
}
//DIAS
if (DiaSistema <= (this.Cbb_Nac_Dia.SelectedIndex + 1))
{
DiaEdad = "";
Msg_DiaEdad = "";
}
else
{
DiaEdad = (DiaSistema - (this.Cbb_Nac_Dia.SelectedIndex + 1)) + "";
if (DiaEdad == "1")
{//Singular
Msg_DiaEdad = " día";
}
else
{
Msg_DiaEdad = " días";
}
}
if (MesSistema >= this.Cbb_Nac_Mes.SelectedIndex //Si el Mes del Sistema es Mayor al Mes de Nacimiento
&& DiaSistema >= (this.Cbb_Nac_Dia.SelectedIndex +1))//Si el Día del Sistema es Mayor al Día de Nacimiento
{
Edad = (AnioActual - int.Parse(this.Cbb_Nac_Anio.Text)) + 1;//Agregandolo un año para la Edad.
if ((DiaSistema == (this.Cbb_Nac_Dia.SelectedIndex +1)) && (MesSistema==(this.Cbb_Nac_Mes.SelectedIndex+1)))
{
this.Lbl_Edad.Text = Edad + "";
this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "/Img/Felicidades.png");
MessageBox.Show("¡¡¡ Feliz " + Edad + " Cumpleaños!!! Saludos cordiales y pásale bien.", "Felicitaciones", MessageBoxButtons.OK, MessageBoxIcon.Information);
}else{
this.Lbl_Edad.Text = Edad + " años, " + MesEdad + Msg_MesEdad + ", " + DiaEdad + Msg_DiaEdad + ".";
}
}
else {
Edad = (AnioActual - int.Parse(this.Cbb_Nac_Anio.Text));
// Mostrando en DIAS
if (DiaSistema <= (this.Cbb_Nac_Dia.SelectedIndex + 1))
{ // Result ( - ) Negativo -4 dias
this.Lbl_Edad.Text = Edad + " años, " + MesEdad + Msg_MesEdad + ".";
}else{ // Result ( + ) Positivo +4 dias
this.Lbl_Edad.Text = Edad + " años, " + MesEdad + Msg_MesEdad + ", " + DiaEdad + Msg_DiaEdad + ".";
}
}
}
}///***** OKOKOK==================================================================
///***** [[_ FECHA DE INGRESO _]]
if (this.Cbb_Ing_Dia.SelectedValue == "Día" || this.Cbb_Ing_Mes.SelectedValue == "Mes" || this.Cbb_Ing_Anio.SelectedValue == "Año")
{
MessageBox.Show("Seleccione una Fecha de Ingreso Correcto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else { //Ok - Fechas Ingresadas Correctas
//MESES
if (MesSistema <= (this.Cbb_Ing_Mes.SelectedIndex + 1)){
MesServ = "";
Msg_MesServ = "";
}else{
MesServ = (MesSistema - (this.Cbb_Ing_Mes.SelectedIndex + 1)) + "";
if (MesServ == "1")
{//Singular
Msg_MesServ = " mes";
}else{
Msg_MesServ = " meses";
}
}
//DIAS
if (DiaSistema <= (this.Cbb_Ing_Dia.SelectedIndex + 1))
{
DiaServ = "";
Msg_DiaServ = "";
}else{
DiaServ = (DiaSistema - (this.Cbb_Ing_Dia.SelectedIndex + 1)) + "";
if (DiaServ == "1")
{//Singular
Msg_DiaServ = " día";
}else{
Msg_DiaServ = " días";
}
}
if (MesSistema >= this.Cbb_Ing_Mes.SelectedIndex //Si el Mes del Sistema es Mayor al Mes de Ingreso de Servicio
&& DiaSistema >= (this.Cbb_Ing_Dia.SelectedIndex +1))//Si el Día del Sistema es Mayor al Día Ingreso de Servicio
{
TiempoServ = (AnioActual - int.Parse(this.Cbb_Ing_Anio.Text)) + 1;//Agregandolo un año para el Tiempo de Servicio.
if ((DiaSistema == (this.Cbb_Ing_Dia.SelectedIndex +1)) && (MesSistema==(this.Cbb_Ing_Mes.SelectedIndex+1)))
{
this.Lbl_TiemServic.Text = TiempoServ + "";
this.pictureBox1.Image = Image.FromFile(Application.StartupPath + "/Img/Felicidades.png");
MessageBox.Show("¡¡¡ Cumple " + TiempoServ + " Años de Servicio!!! Saludos cordiales y pásale bien.", "Felicitaciones", MessageBoxButtons.OK, MessageBoxIcon.Information);
}else{// Mostrando en DIAS
// Result ( + ) Positivo +4 dias
this.Lbl_TiemServic.Text = TiempoServ + " años, " + MesServ + "" + Msg_MesServ + ", " + DiaServ + " días.";
}
}
else {
TiempoServ = (AnioActual - int.Parse(this.Cbb_Ing_Anio.Text));
// Mostrando en DIAS
if (DiaSistema <= (this.Cbb_Ing_Dia.SelectedIndex + 1))
{ // Result ( - ) Negativo -4 dias
this.Lbl_TiemServic.Text = TiempoServ + " años, " + MesServ + Msg_MesServ + ".";
}else{ // Result ( + ) Positivo +4 dias
this.Lbl_TiemServic.Text = TiempoServ + " años, " + MesServ + Msg_MesServ + ", " + DiaServ + Msg_DiaServ + ".";
}
}
}///***** OKOKOK==================================================================
//// Probando con los codigos.
//*****************************
//this.Lbl_Res_Edad.Text = this.dateTimePicker1.Value+""; //08/08/2008 09:51
//this.Lbl_Res_Edad.Text = this.dateTimePicker1.Value.Day+""; //08
//this.Lbl_Res_Edad.Text = this.dateTimePicker1.Value.Month+""; //08
//this.Lbl_Res_Edad.Text = this.dateTimePicker1.Value.Year + ""; //2008
//this.Lbl_Res_Edad.Text = this.dateTimePicker1.Value.GetDateTimeFormats() + "";
//this.dateTimePicker1.CustomFormat = "MMMM, yyyy - dddd";
//this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
// int AnioIng, MesIng, DiaIng, AnioNac, MesNac,DiaNac,Edad ;
// AnioIng = this.dateTimePicker1.Value.Year;
// MesIng = this.dateTimePicker1.Value.Month;
// DiaIng = this.dateTimePicker1.Value.Day;
// AnioNac = this.dateTimePicker2.Value.Year;
// MesNac = this.dateTimePicker2.Value.Month;
// DiaNac = this.dateTimePicker2.Value.Day;
// DateTime FecSist = this.monthCalendar1.TodayDate;
// Edad = int.Parse(FecSist.ToString("YYYY")) - AnioNac;
// //this.Lbl_Edad.Text = Edad+"";
// this.Lbl_Res_Edad.Text = Edad + "";
//// this.Lbl_Res_Edad.Text = this.dateTimePicker1.Value+"";
}
}
}
}
Saludos... :xD
KarlosVid(ÊÇ):
Listo Amigos ya esta resuelto los ejercicios de 4 Formas distintas ::). :laugh: Solo les pediria que comenten del contenido cuando lo terminen de Ver los ejercicios. Y asi los user lo puedan descargar con confianza. Esta bien elaborado, espero que les gusten. Lo tuve que subir a megaupload y rapidshare, porque tiene mucha programación y no me alcanzaria postearlo todo :-\. Primero ver y luego escribir en el post, ok. ;)
Descargar:
http://www.megaupload.com/?d=7ZUOO1EI
http://rapidshare.com/files/267716667/Desarrollo_EjemplosC__4Formas__www.sidat.tk.rar.html
Descargar:
http://www.megaupload.com/?d=7ZUOO1EI
http://rapidshare.com/files/267716667/Desarrollo_EjemplosC__4Formas__www.sidat.tk.rar.html
Saludos... :xD
Navegación