JESSICA

FLOREZ RAMIRÉZ

JAZMÍN

HERNÁNDEZ SANTOYO

EDUARDO JOEL

LARA ARELLANO

3PRAV

PROGRAMACION

viernes, 5 de diciembre de 2014

Códigos para Visual Studio part 8

Para este es el mismo procedimiento, almacena igual los datois.
Codigo:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Agregar_con_validacion
{
    public partial class Form1 : Form
    {
        public static bool insertaelementos(string Codigo, string Nombre_alumno, string Ciudad, string Apellidos)
        {
            bool fl;
            try
            {
                OleDbConnection miconexion = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;DATA Source= " + @AppDomain.CurrentDomain.BaseDirectory + @"/alumnos.accdb;Persist Security Info=False");
                OleDbCommand comandquery = new OleDbCommand("insert into Alumnos(Codigo,Nombre_alumno,Ciudad, Apellidos) values ('" + Codigo + "','" + Nombre_alumno + "','" + Ciudad + "','" + Apellidos + "' )", miconexion);
                OleDbDataAdapter add = new OleDbDataAdapter();
                miconexion.Open();
                add.InsertCommand = comandquery;
                add.InsertCommand.ExecuteNonQuery();
                miconexion.Close();
                fl = true;
            }
            catch
            {
                fl = false;
            }
            return fl;
        }


        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox1.Focus();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult r;
            r = MessageBox.Show("¿Seguro que quieres salir?", "Confirmar", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (r == DialogResult.Yes)
            { Application.Exit(); }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (insertaelementos(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text))
            {
                MessageBox.Show("Registro guardado con exito!", "Exito!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Hubo un error al guardad el registro", "¡Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox1.Focus();
        }
    }
}




Códigos para Visual Studio part 7

En esta entrada Guardaremos un registro en la base de datos desde Visual Studio.
Para esta entrada no es necesario ninguna agregar alguna herramienta en el windows Form .

Código:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Grabar_sin_confirmacion
{
    public partial class Form1 : Form
    {
        public static void insertelement(string Codigo, string Nombre_alumno, string Ciudad, string Apellidos)
        {
            OleDbConnection miconex = new OleDbConnection (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ @AppDomain.CurrentDomain.BaseDirectory + @"/alumnos.accdb;Persist Security Info = False");
         
            OleDbCommand comando = new OleDbCommand ("Insert into Alumnos(Codigo,Nombre_alumno,Ciudad, Apellidos) values ('" + Codigo + "','" + Nombre_alumno + "','" + Ciudad + "','" + Apellidos + "' )", miconex);
           
            OleDbDataAdapter add = new OleDbDataAdapter ();
            miconex.Open();
            add.InsertCommand = comando;
            add.InsertCommand.ExecuteNonQuery();
            miconex.Close();

        }
        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult r;
            r = MessageBox.Show("¿Quiere abandonar la pagina?", "Confirmar", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (r == DialogResult.Yes)
            { Application.Exit(); }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox1.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            insertelement(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox1.Focus();


        }
     

    }
}



Al ejecutar:


Códigos para Visual Studio part 6

DataGridView:Muestra los datos en una cuadrícula personalizable.

En esta entrada subiremos los códigos para hacer que la base de datos que deseamos añadir al ejecutar windows form aparezca.

-Primero tienes que agregar un DataGridView en el Formulario.

Codigo:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Rejilla_conexión
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
    class conexion
    
    {
        static string cadenaconexion;
        static OleDbConnection conex;
        static IDbDataAdapter adaptador;
        static OleDbCommandBuilder constructor;
        static DataTable tabla;
        static BindingSource fuente;
        public static void conectar()
{
    cadenaconexion=@"provider=microsoft.Ace.OLEDB.12.0;Data sourse=" + @AppDomain.CurrentDomain.BaseDirectory + @"/BD_Alumnos.accdb;Persist Security info=False";
    conex= new OleDbConnection(cadenaconexion);
      conex.open();
}
        public static void Desconectar()
{
    conex.close();
}
        public static void consulta(string tabla, string campos, string orden)
{
    string consulta="select"+ campos + "from" + tabla + "orederby" +orden";";
    adaptador = new  (consulta,conex);
    constructor= new 
}
    }
   

}

En visual :


Comandos para conectar con bases de datos con OleDb

OleDbConnection (): Inicializa una nueva instancia de la clase OleDbConnection.

DataSource: Obtiene el nombre del servidor o el nombre del archivo de origen de datos. (Invalida DbConnection. DataSource .)


static: Utilice el modificador static para declarar un miembro estático, que pertenece al propio tipo en vez de a un objeto específico. El modificador static puede utilizarse con clases, campos, métodos, propiedades, operadores, eventos y constructores, pero no puede utilizarse con indizadores, destructores o tipos que no sean clases.

Open(): es la instrucción que da lugar a la apertura del fichero.

Close(): da lugar al cierre del fichero y a que el número de archivo o línea de comunicación quede libre para una posterior ocasión.


Fill: Llame al método Fill del adaptador de datos. De este modo, el adaptador ejecuta una instrucción SQL o un procedimiento almacenado al que se hace referencia en su propiedad SelectCommand y guarda los resultados en una tabla del conjunto de datos.

Realizar un juego matemático con Visual studio


MenuStrip: Proporciona un sistema de menús para un formulario.

Timer:El control Timer es un temporizador que nos permite ejecutar instrucciones de código, rutinas, funciones etc..., cada cierto intervalo de tiempo.
Este control es invisible en tiempo de ejecución, esto quiere decir que no tiene interfaz gráfica, solo es visible cuando lo agregamos a un formulario y estamos en modo de diseño .
La propiedad mas importante de este control es la propiedad Interval

IntervalDevuelve o establece el número de milisegundos entre dos llamadas al evento Timer de un control Timer.

-Primero tienes que agregar un windows form en donde agregaras las instrucciones.
-Utilizas el menustrip en donde puedes poner los menús y submenus que desees en este caso serán Operaciones:Suma , resta, divicion, multiplicacion y asi con los demas menus.
(Puedes agregar a cada submenu un ShortcutKeys para un acceso rápido a el )
-Agregar el timer.

-Agregas el siguiente código .
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Resover_Operaciones_Basicas
{
    public partial class Form1 : Form
    {
        float ki,matte;
     
        public Form1()
        {
         
            InitializeComponent();
        }

     
        private void Suma_Click(object sender, EventArgs e)
        {
            Multiplicacion.Checked = false;
            Division.Checked = false;
            Resta.Checked = false;
            beta();
        }
     
        private void but_jugar_Click(object sender, EventArgs e)
        {
            if (Suma.Checked)
            { label13.Text = "+"; }
            if (Resta.Checked)
            { label13.Text = "-"; }
            if (Division.Checked)
            { label13.Text = "/"; }
            if (Multiplicacion.Checked)
            { label13.Text = "*"; }

            matte = matte + 1;
            tb_resultado.Text = "";
            tb_t_a_m.Text = "00";
            tb_t_a_s.Text = "00";
            but_jugar.Visible = false;
            gb_operacion.Visible = true;
            gb_tiempo.Visible = true;
            alpha();
            gamma();
            beta();
            timer1.Start();
        }

        private void tiempoToolStripMenuItem_Click(object sender, EventArgs e)
        {
         
        }
        private void beta()
        {
            if (Suma.Checked)
            { lab_simbolo.Text = "+"; }
            if (Resta.Checked)
            { lab_simbolo.Text = "-"; }
            if (Division.Checked)
            { lab_simbolo.Text = "/"; }
            if (Multiplicacion.Checked)
            { lab_simbolo.Text = "*"; }
        }
        private void alpha()
        {
            if (Segundo_30.Checked)
            {
                tb_t_s_m.Text = 00.ToString();
                tb_t_s_s.Text = 30.ToString();

            }
            if (Segundo_90.Checked)
            {
                tb_t_s_m.Text = 01.ToString();
                tb_t_s_s.Text = 30.ToString();
            }
            if (minuto_1.Checked)
            {
                tb_t_s_s.Text = 00.ToString();
                tb_t_s_m.Text = 01.ToString();
            }
        }
        private void Segundo_30_Click(object sender, EventArgs e)
        {
            minuto_1.Checked = false;
            Segundo_90.Checked = false;
            alpha();
        }

        private void minuto_1_Click(object sender, EventArgs e)
        {
            Segundo_30.Checked = false;
            Segundo_90.Checked = false;
            alpha();
        }

        private void Segundo_90_Click(object sender, EventArgs e)
        {
            minuto_1.Checked = false;
            Segundo_30.Checked = false;
            alpha();
        }
        private void gamma()
        {
            int r_a = 0, r_b = 0;
            Random a = new Random(DateTime.Now.Millisecond);
            Random b = new Random(DateTime.Now.Second);

            if (Cifra1.Checked == true)
            {
                r_a = a.Next(1, 10);
                r_b = b.Next(1, 10);
            }
            if (Cifra2.Checked)
            {
                r_a = a.Next(10, 100);
                r_b = b.Next(10, 100);
            }
            if (Cifra_3.Checked)
            {
                r_a = a.Next(100, 999);
                r_b = b.Next(100, 999);
            }

            tb_val1.Text = r_a.ToString();
            tb_val2.Text = r_b.ToString();
        }
        private void Cifra2_Click(object sender, EventArgs e)
        {
            Cifra1.Checked = false;
            Cifra_3.Checked = false;
         
        }

        private void Cifra_3_Click(object sender, EventArgs e)
        {
            Cifra1.Checked = false;
            Cifra2.Checked = false;        
         
        }
        private void Cifra_1_Click(object sender, EventArgs e)
        {
            Cifra2.Checked = false;
            Cifra_3.Checked = false;
         
        }

        private void Resta_Click(object sender, EventArgs e)
        {
            Multiplicacion.Checked = false;
            Division.Checked = false;
            Suma.Checked = false;
            beta();
         
        }

        private void Multiplicacion_Click(object sender, EventArgs e)
        {
         
            Division.Checked = false;
            Resta.Checked = false;
            Suma.Checked = false;
            beta();
        }

        private void Division_Click(object sender, EventArgs e)
        {
         
            Multiplicacion.Checked = false;
            Resta.Checked = false;
            Suma.Checked = false;
            beta();
        }

        private void tb_resultado_KeyDown(object sender, KeyEventArgs e)
        {
            float q=0, w=0, r=0,y=0;
            DialogResult yui= 0;

            if (e.KeyCode == Keys.Enter && lab_simbolo.Text == "+")
            {

                q = float.Parse(tb_val1.Text);
                w = float.Parse(tb_val2.Text);
                r = float.Parse(tb_resultado.Text);
                y = q + w;
                timer1.Stop();
                if (y == r)
                {

                    yui = MessageBox.Show(" Correcta :) ", "Tu respuesta es: ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    ki = ki + 1;
                }
                else
                {
                    yui = MessageBox.Show("Incorrecta :( ", "Tu respesta es:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                if (yui == DialogResult.OK)
                {
                    but_jugar.Visible = true;
                    gb_operacion.Visible = false;
                    gb_tiempo.Visible = false;

                }
            }
            if (e.KeyCode == Keys.Enter && lab_simbolo.Text == "*")
            {

                q = float.Parse(tb_val1.Text);
                w = float.Parse(tb_val2.Text);
                r = float.Parse(tb_resultado.Text);
                y = q * w;
                timer1.Stop();
                if (y == r)
                {

                    yui = MessageBox.Show(" Correcta :) ", "Tu respuesta es: ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    ki = ki + 1;

                }
                else
                {
                    yui = MessageBox.Show("Incorrecta :( ", "Tu respesta es:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                if (yui == DialogResult.OK)
                {
                    but_jugar.Visible = true;
                    gb_operacion.Visible = false;
                    gb_tiempo.Visible = false;
                }
            }
             if (e.KeyCode == Keys.Enter && lab_simbolo.Text == "/" )
            {

                q = float.Parse(tb_val1.Text);
                w = float.Parse(tb_val2.Text);
                r = float.Parse(tb_resultado.Text);
                y = q / w;
                timer1.Stop();
                if (y == r)
                {

                    yui = MessageBox.Show(" Correcta :) ", "Tu respuesta es: ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    ki = ki + 1;
                 
                }
                else
                {
                    yui = MessageBox.Show("Incorrecta :( ", "Tu respesta es:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                if (yui == DialogResult.OK)
                {
                but_jugar.Visible = true;
                gb_operacion.Visible = false;
                gb_tiempo.Visible = false;
                }
             }
               if (e.KeyCode == Keys.Enter && lab_simbolo.Text == "-" )
            {

                q = float.Parse(tb_val1.Text);
                w = float.Parse(tb_val2.Text);
                r = float.Parse(tb_resultado.Text);
                y = q - w;
                timer1.Stop();
                if (y == r)
                {

                    yui = MessageBox.Show(" Correcta :) ", "Tu respuesta es: ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    ki = ki + 1;
                 
                }
                else
                {
                    yui = MessageBox.Show("Incorrecta :( ", "Tu respesta es:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                if (yui == DialogResult.OK)
                {
                but_jugar.Visible = true;
                gb_operacion.Visible = false;
                gb_tiempo.Visible = false;
                }
            }
        }

        private void gb_operacion_Enter(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            tb_t_a_s.Text = ((int.Parse(tb_t_a_s.Text)) + 1).ToString();
            if (int.Parse(tb_t_a_s.Text) == 60)
            {
                tb_t_a_m.Text = (int.Parse(tb_t_a_m.Text) + 1).ToString();
                tb_t_a_s.Text = "0";
            }
            if (int.Parse(tb_t_a_s.Text) == int.Parse(tb_t_s_s.Text) && int.Parse(tb_t_a_m.Text) == int.Parse(tb_t_s_m.Text) )
            {
                timer1.Stop();
                DialogResult w;
                w = MessageBox.Show("Tu tiempo a terminado", "Fin del juego", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                if (w == DialogResult.OK)
                {
                    tb_resultado.Text = "0";
                    gb_operacion.Visible = false;
                    gb_tiempo.Visible = false;
                    but_jugar.Visible = true;
                }
            }

         
        }
        private void awer()
        {
            DialogResult w;
            w = MessageBox.Show( "El Porcentaje de las operaciones que has realizado bien :) es de "+(((100 * ki) / matte).ToString())+"%", "RATIO", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void tb_t_a_s_TextChanged(object sender, EventArgs e)
        {

        }

        private void tb_resultado_TextChanged(object sender, EventArgs e)
        {

        }

        private void salirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult w;
            w = MessageBox.Show("¿Quieres anbandonar el juego?", "Vas a salir", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
             if (w == DialogResult.Yes)
             {
                 Application.Exit();
             }

        }

        private void porcentageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            awer();
        }

        private void instruccionesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            instrucciones instruct = new instrucciones();
            instruct.ShowDialog();
            this.Show();
        }
Al ejecutar

Si acaba tu tiempo: