//--------------------------------------------------------------------------
// Proyecto: Sencilla Agenda de contactos
// Desarrollo: gsur
// Fecha: 18/03/2006
// Compilar:
// mcs sagenda.cs -pkg:gtk-sharp -pkg:glade-sharp -r:System.Data.dll
// -r:Mysql.Data.dll -resource:vagenda.glade
// Entorno de desarrollo: Mono
// Comentarios: El borrado de contactos tiene un error, ya que no recorre el
// numero actual de contactos, falta añadir un poco mas de codigo,
// pero el borrado si lo aplica correctamente, asi que quedan
// advertidos
//--------------------------------------------------------------------------
using System;
using System.Data;
using MySql.Data.MySqlClient;
using Gtk;
using Glade;
public class Agenda
{
[Glade.Widget] Gtk.Window ventana;
[Glade.Widget] Gtk.Entry txtcontacto;
[Glade.Widget] Gtk.Entry txtdire;
[Glade.Widget] Gtk.Entry txttel;
[Glade.Widget] Gtk.Entry txtmail;
[Glade.Widget] Gtk.Label lblnregs;
[Glade.Widget] Gtk.Entry txtbusqueda;
public string sql; //Variable para las cadenas de consulta
public string connectionString;
public IDbConnection dbcon;
public IDbCommand dbcmd;
public IDataReader reader;
public int npos; //Renglon actual
public long nc; //Numero de renglones
public System.UInt32 id_contacto; //Clave primaria de los contactos
public string nombre; //Nombre del contacto
public string direccion; //Direccion del contacto
public string telefono; //Telefono del contacto
public string email; //Email del contacto
public int opcion; //Situacion actual [Insercion o Update]
public static void Main(String[] args)
{
Application.Init();
Agenda gt=new Agenda();
Application.Run();
}
public Agenda()
{
Glade.XML gxml=new Glade.XML(null, "vagenda.glade","ventana",null);
gxml.Autoconnect(this);
connectionString ="Server=localhost;" + "Database=personal;" + "User ID=rafita;" + "Password=;" + "Pooling=false";
dbcon = new MySqlConnection(connectionString);
dbcon.Open();
dbcmd = dbcon.CreateCommand();
sql = "SELECT * FROM miscontactos ORDER BY nombre_contacto LIMIT 0,1";
dbcmd.CommandText = sql;
reader = dbcmd.ExecuteReader();
leer_contacto(reader);
npos=0;
dhabilita(false);
reader.Close();
reader=null;
sql = "SELECT COUNT(id_contacto) AS ncount FROM miscontactos";
dbcmd.CommandText = sql;
reader = dbcmd.ExecuteReader();
reader.Read();
nc = (long) reader["ncount"];
lblnregs.Text="Contacto: 1 de " + nc;
opcion=0;
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
}
public void on_ventana_delete_event(object o, DeleteEventArgs args)
{
dbcon.Close();
dbcon = null;
Application.Quit();
}
public void on_bsalir_clicked(System.Object b, EventArgs e)
{
dbcon.Close();
dbcon = null;
Application.Quit();
}
public void on_bprimero_clicked(System.Object b, EventArgs e)
{
npos=0;
lblnregs.Text="Contacto: 1 de " + nc;
mover();
}
public void on_banterior_clicked(System.Object b, EventArgs e)
{
if(npos>0) {
npos-=1;
mover();
}
else
advierte("\n\nSe ha llegado al primer contacto");
lblnregs.Text="Contacto: " + (int) (npos + 1) + " de " + nc;
}
public void on_bsiguiente_clicked(System.Object b, EventArgs e)
{
if(npos<((int) nc-1)) {
npos+=1;
mover();
}
else
advierte("\n\nSe ha llegado al ultimo contacto");
lblnregs.Text="Contacto: " + (int) (npos + 1) + " de " + nc;
}
public void on_bultimo_clicked(System.Object b, EventArgs e)
{
npos=(int) (nc-1);
mover();
lblnregs.Text="Contacto: " + nc + " de " + nc;
}
public void guardar_cancelar()
{
nombre = txtcontacto.Text;
direccion = txtdire.Text;
telefono = txttel.Text;
email = txtmail.Text;
}
public void on_bnuevo_clicked(System.Object b, EventArgs e)
{
dhabilita(true);
guardar_cancelar();
txtcontacto.Text="";
txtdire.Text="";
txttel.Text="";
txtmail.Text="";
opcion=1;
}
public void on_beditar_clicked(System.Object b, EventArgs e)
{
dhabilita(true);
guardar_cancelar();
opcion=2;
}
public void on_bguardar_clicked(System.Object b, EventArgs e)
{
dbcmd = dbcon.CreateCommand();
switch(opcion)
{
case 0:
String msg;
msg="\n\nNo ha elegido alguna opcion [Nuevo/Editar]";
MessageDialog md = new MessageDialog(ventana, DialogFlags.DestroyWithParent, MessageType.Warning, ButtonsType.Ok,msg);
ResponseType r = (ResponseType) md.Run();
if(r==ResponseType.Ok) {
md.Destroy();
}
break;
case 1:
guardar_nuevo();
break;
case 2:
guardar_edicion();
break;
}
opcion=0;
dhabilita(false);
}
public void guardar_nuevo()
{
sql = "INSERT INTO miscontactos(nombre_contacto, direccion_contacto, telefono_contacto, email_contacto) VALUES(";
sql = sql + "'" + txtcontacto.Text + "','" + txtdire.Text + "','" + txttel.Text + "','" + txtmail.Text + "')";
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
sql = "SELECT COUNT(id_contacto) AS ncount FROM miscontactos";
dbcmd.CommandText = sql;
reader = dbcmd.ExecuteReader();
reader.Read();
nc = (long) reader["ncount"];
nc-=1;
reader.Close();
reader = null;
sql = "SELECT * FROM miscontactos ORDER BY id_contacto LIMIT " + nc + ",1";
dbcmd.CommandText = sql;
reader = dbcmd.ExecuteReader();
reader.Read();
System.UInt32 na = (System.UInt32) reader["id_contacto"];
id_contacto = na;
reader.Close();
reader = null;
sql = "SELECT * FROM miscontactos ORDER BY nombre_contacto";
dbcmd.CommandText = sql;
reader = dbcmd.ExecuteReader();
int ncont=0;
while(reader.Read() && ((System.UInt32) reader["id_contacto"]!=na)) {
ncont+=1;
}
npos=ncont;
ncont+=1;
nc+=1;
lblnregs.Text = "Contacto: " + ncont + " de " + nc;
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
}
public void guardar_edicion()
{
sql = "UPDATE miscontactos SET nombre_contacto='" + txtcontacto.Text + "',";
sql = sql + "direccion_contacto='" + txtdire.Text + "',";
sql = sql + "telefono_contacto='" + txttel.Text + "',";
sql = sql + "email_contacto='" + txtmail.Text + "' WHERE id_contacto=" + id_contacto;
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
dbcmd.Dispose();
dbcmd = null;
}
public void on_bcancelar_clicked(System.Object b, EventArgs e)
{
txtcontacto.Text = nombre;
txtdire.Text = direccion;
txttel.Text = telefono;
txtmail.Text = email;
dhabilita(false);
}
public void on_beliminar_clicked(System.Object b, EventArgs e)
{
String msgdelete;
msgdelete="\n¿Desea realmente eliminar este contacto?";
msgdelete = msgdelete + "\n\nSi desea continuar pulse Aceptar, si no";
msgdelete = msgdelete + "\ndesea continuar pulse Cancelar";
MessageDialog md = new MessageDialog(ventana, DialogFlags.DestroyWithParent, MessageType.Warning, ButtonsType.OkCancel,msgdelete);
ResponseType r = (ResponseType) md.Run();
if(r==ResponseType.Ok) {
dbcon = new MySqlConnection(connectionString);
dbcon.Open();
dbcmd = dbcon.CreateCommand();
sql = "DELETE FROM miscontactos WHERE id_contacto=" + id_contacto;
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
md.Destroy();
}
else
md.Destroy();
}
public void on_bbuscar_clicked(System.Object b, EventArgs e)
{
dbcmd = dbcon.CreateCommand();
sql = "SELECT * FROM miscontactos WHERE nombre_contacto LIKE '" + txtbusqueda.Text + "%'";
dbcmd.CommandText = sql;
reader = dbcmd.ExecuteReader();
if(reader.Read())
{
id_contacto = (System.UInt32) reader["id_contacto"];
System.UInt32 na = id_contacto;
txtcontacto.Text=(string) reader["nombre_contacto"];
txtdire.Text=(string) reader["direccion_contacto"];
txttel.Text=(string) reader["telefono_contacto"];
txtmail.Text=(string) reader["email_contacto"];
reader.Close();
reader = null;
sql = "SELECT * FROM miscontactos ORDER BY nombre_contacto";
dbcmd.CommandText = sql;
reader = dbcmd.ExecuteReader();
int ncont=0;
while(reader.Read() && ((System.UInt32) reader["id_contacto"]!=na)) {
ncont+=1;
}
npos=ncont;
ncont+=1;
lblnregs.Text = "Contacto: " + ncont + " de " + nc;
reader.Close();
reader = null;
}
else
{
MessageDialog md = new MessageDialog(ventana, DialogFlags.DestroyWithParent, MessageType.Warning, ButtonsType.Ok, "\n\nNo se enontro alguna coincidencia");
ResponseType r = (ResponseType) md.Run();
if(r==ResponseType.Ok)
md.Destroy();
}
dbcmd.Dispose();
dbcmd = null;
}
public void mover()
{
dbcmd = dbcon.CreateCommand();
sql = "SELECT * FROM miscontactos ORDER BY nombre_contacto LIMIT " + npos + ",1";
dbcmd.CommandText = sql;
reader = dbcmd.ExecuteReader();
leer_contacto(reader);
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
}
public void dhabilita(Boolean valor)
{
txtcontacto.Editable = valor;
txtdire.Editable = valor;
txttel.Editable = valor;
txtmail.Editable = valor;
}
public void advierte(String mensaje)
{
MessageDialog md = new MessageDialog(ventana, DialogFlags.DestroyWithParent, MessageType.Warning, ButtonsType.Ok, mensaje);
ResponseType r = (ResponseType) md.Run();
if(r==ResponseType.Ok)
md.Destroy();
}
public void leer_contacto(IDataReader mireader)
{
mireader.Read();
id_contacto = (System.UInt32) mireader["id_contacto"];
txtcontacto.Text=(string) mireader["nombre_contacto"];
txtdire.Text=(string) mireader["direccion_contacto"];
txttel.Text=(string) mireader["telefono_contacto"];
txtmail.Text=(string) mireader["email_contacto"];
}
}