El código de la aplicación de prueba para usar las clases generadas |
Actualizado: 14/Jul/2004
Autor: Guillermo 'guille' Som
Volver al índice de Generar clases para acceder a una tabla
El código de Visual Basic .NET
'------------------------------------------------------------------------------ ' Utilidad genérica para usar las clases generadas automáticamente ' ' ©Guillermo 'guille' Som, 2004 '------------------------------------------------------------------------------ Imports Microsoft.VisualBasic Imports System Imports System.Data Public Class Form1 Inherits System.Windows.Forms.Form ' #Region " Código generado por el Diseñador de Windows Forms " '... #End Region '************************************************************************** '************************************************************************** ' Poner la clase a utilizar en la declaración de mClaseTabla ' y en la función nuevaClaseTabla '************************************************************************** Private mClaseTabla As authors ' Private Function nuevaClaseTabla() As authors Return New authors End Function '************************************************************************** '************************************************************************** ' Private labelCampos() As Label Private txtCampos() As TextBox Private lviActual As ListViewItem Private dt As DataTable ' Private Sub crearCampos(ByVal cols As DataColumnCollection) Dim k As Integer Dim n As Integer = cols.Count ' ' Quitar los controles anteriores de la colección del formulario If Not labelCampos Is Nothing Then For i As Integer = 1 To labelCampos.Length - 1 Me.Controls.Remove(labelCampos(i)) Next End If If Not txtCampos Is Nothing Then For i As Integer = 1 To txtCampos.Length - 1 Me.Controls.Remove(txtCampos(i)) Next End If ' Me.AutoScroll = False ' ReDim labelCampos(n - 1) ReDim txtCampos(n - 1) labelCampos(0) = LabelCampo_0 txtCampo_0.Enabled = True txtCampos(0) = txtCampo_0 k = txtCampos(0).TabIndex For i As Integer = 0 To n - 1 Dim col As DataColumn = cols(i) If i > 0 Then labelCampos(i) = New Label txtCampos(i) = New TextBox k += 1 labelCampos(i).TabIndex = k k += 1 txtCampos(i).TabIndex = k ' labelCampos(i).Name = "LabelCampo_" & i.ToString() txtCampos(i).Name = "txtCampo_" & i.ToString() labelCampos(i).Anchor = labelCampos(0).Anchor txtCampos(i).Anchor = txtCampos(0).Anchor labelCampos(i).Size = labelCampos(0).Size labelCampos(i).Location = labelCampos(i - 1).Location labelCampos(i).Top += 24 txtCampos(i).Size = txtCampos(0).Size txtCampos(i).Location = txtCampos(i - 1).Location txtCampos(i).Top += 24 labelCampos(i).Visible = True txtCampos(i).Visible = True Me.Controls.Add(labelCampos(i)) Me.Controls.Add(txtCampos(i)) txtCampos(i).Text = "" End If labelCampos(i).Text = col.ColumnName & ":" Next ' n = txtCampos(n - 1).Top + 24 + 36 Me.MaximumSize = New Size(Screen.PrimaryScreen.WorkingArea.Width, n) Me.AutoScroll = True End Sub ' Private Sub btnMostrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMostrar.Click ' txtCampo_0.Text = "Creando..." txtCampo_0.Refresh() ListView1.Columns.Clear() ' ' actualizar el valor de la cadena de selección por si se quiere filtrar 'mClaseTabla.CadenaSelect = txtSelect.Text dt = mClaseTabla.Tabla(txtSelect.Text) crearCampos(dt.Columns) ' LabelMostrar.Text = "Regs a mostrar" & vbCrLf & "De 0" & vbCrLf & "A " & (dt.Rows.Count - 1) txtDesde.Text = "0" If dt.Rows.Count < 101 Then txtHasta.Text = (dt.Rows.Count - 1).ToString Else txtHasta.Text = "99" End If ' For Each col As DataColumn In dt.Columns If col.DataType.ToString().StartsWith("System.Int") Then ListView1.Columns.Add(col.ColumnName, 60, HorizontalAlignment.Right) ElseIf col.DataType.ToString().StartsWith("System.Date") Then ListView1.Columns.Add(col.ColumnName, 120, HorizontalAlignment.Left) Else ListView1.Columns.Add(col.ColumnName, 100, HorizontalAlignment.Left) End If Next ' btnRefrescar_Click(sender, EventArgs.Empty) End Sub ' Private Sub btnRefrescar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefrescar.Click Dim j As Integer = CInt(txtDesde.Text) If j < 0 Then j = 0 If j > dt.Rows.Count - 1 Then j = dt.Rows.Count - 1 Dim k As Integer = CInt(txtHasta.Text) If k < 0 Then k = 0 If k > dt.Rows.Count - 1 Then k = dt.Rows.Count - 1 ListView1.Items.Clear() For n As Integer = j To k Dim r As DataRow = dt.Rows(n) Dim lvi As ListViewItem For i As Integer = 0 To dt.Columns.Count - 1 'Dim col As DataColumn = dt.Columns(i) If i = 0 Then lvi = ListView1.Items.Add(r(i).ToString) Else lvi.SubItems.Add(r(i).ToString) End If Next Next txtCampo_0.Text = "" ' btnNuevo.Enabled = True btnBorrar.Enabled = False btnActualizar.Enabled = False End Sub ' Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load txtSelect.Text = mClaseTabla.CadenaSelect ListView1.View = View.Details ListView1.FullRowSelect = True ListView1.LabelEdit = False ListView1.GridLines = True ListView1.Columns.Clear() ' btnBorrar.Enabled = False btnActualizar.Enabled = False btnNuevo.Enabled = False ' btnMostrar_Click(Nothing, Nothing) End Sub ' Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged If ListView1.SelectedItems.Count > 0 Then lviActual = ListView1.SelectedItems(0) For i As Integer = 0 To lviActual.SubItems.Count - 1 txtCampos(i).Text = lviActual.SubItems(i).Text Next btnBorrar.Enabled = True btnActualizar.Enabled = True End If End Sub ' Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click ' crear un nuevo usuario mClaseTabla = nuevaClaseTabla() ' For i As Integer = 0 To txtCampos.Length - 1 mClaseTabla(i) = txtCampos(i).Text Next Dim s As String = mClaseTabla.Crear() If s.StartsWith("ERROR") Then MessageBox.Show(s, "Error al crear uno nuevo") Else lviActual = ListView1.Items.Add(mClaseTabla(0).ToString) For i As Integer = 1 To txtCampos.Length - 1 lviActual.SubItems.Add(txtCampos(i).Text) Next End If End Sub ' Private Sub btnActualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnActualizar.Click ' actualizar los datos mClaseTabla = nuevaClaseTabla() ' ' Tener en cuenta el ID (que será el índice cero) ' ya que se usará para buscarlo en la tabla For i As Integer = 0 To txtCampos.Length - 1 mClaseTabla(i) = txtCampos(i).Text Next Dim s As String = mClaseTabla.Actualizar() If s.StartsWith("ERROR") Then MessageBox.Show(s, "Error al actualizar") Else For i As Integer = 1 To txtCampos.Length - 1 lviActual.SubItems(i).Text = txtCampos(i).Text Next End If End Sub ' Private Sub btnBorrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBorrar.Click ' Borrar el usuario mClaseTabla = nuevaClaseTabla() ' ' Tener en cuenta el ID (que será el índice cero) ' ya que se usará para buscarlo en la tabla For i As Integer = 0 To txtCampos.Length - 1 mClaseTabla(i) = txtCampos(i).Text Next Dim s As String = mClaseTabla.Borrar() If s.StartsWith("ERROR") Then MessageBox.Show(s, "Error al borrar") Else ListView1.Items.Remove(lviActual) lviActual = Nothing End If btnBorrar.Enabled = False End Sub End Class...
//----------------------------------------------------------------------------- // Utilidad genérica para usar las clases generadas automáticamente // (este código está generado con el conversor de VB a C#) // // ©Guillermo 'guille' Som, 2004 //----------------------------------------------------------------------------- using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace pruebaCS { // public class Form1 : System.Windows.Forms.Form { // public static void Main() { Application.Run(new Form1()); } // // #region " Código generado por el Diseñador de Windows Forms " //... #endregion // //----------------------------------------------------------------------------- // Poner la clase a utilizar en la declaración de mClaseTabla // y en la función nuevaClaseTabla //----------------------------------------------------------------------------- private authors mClaseTabla; // private authors nuevaClaseTabla() { return new authors(); } //----------------------------------------------------------------------------- // private Label[] labelCampos; private TextBox[] txtCampos; private ListViewItem lviActual; private DataTable dt; // private void crearCampos(DataColumnCollection cols) { int k; int n = cols.Count; // // Quitar los controles anteriores de la colección del formulario if( labelCampos != null ) { for(int i= 1; i<= labelCampos.Length - 1; i++) this.Controls.Remove(labelCampos[i]); } if( txtCampos != null ) { for(int i= 1; i<= txtCampos.Length - 1; i++) this.Controls.Remove(txtCampos[i]); } // this.AutoScroll = false; // labelCampos = new Label[(n - 1 + 1)]; txtCampos = new TextBox[(n - 1 + 1)]; labelCampos[0] = LabelCampo_0; txtCampo_0.Enabled = true; txtCampos[0] = txtCampo_0; k = txtCampos[0].TabIndex; for(int i= 0; i<= n - 1; i++) { DataColumn col = cols[i]; if( i > 0 ) { labelCampos[i] = new Label(); txtCampos[i] = new TextBox(); k += 1; labelCampos[i].TabIndex = k; k += 1; txtCampos[i].TabIndex = k; // labelCampos[i].Name = "LabelCampo_" + i.ToString(); txtCampos[i].Name = "txtCampo_" + i.ToString(); labelCampos[i].Anchor = labelCampos[0].Anchor; txtCampos[i].Anchor = txtCampos[0].Anchor; labelCampos[i].Size = labelCampos[0].Size; labelCampos[i].Location = labelCampos[i - 1].Location; labelCampos[i].Top += 24; txtCampos[i].Size = txtCampos[0].Size; txtCampos[i].Location = txtCampos[i - 1].Location; txtCampos[i].Top += 24; labelCampos[i].Visible = true; txtCampos[i].Visible = true; this.Controls.Add(labelCampos[i]); this.Controls.Add(txtCampos[i]); txtCampos[i].Text = ""; } labelCampos[i].Text = col.ColumnName + ":"; } n = txtCampos[n - 1].Top + 24 + 36; this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, n); this.AutoScroll = true; } // private void btnMostrar_Click(System.Object sender, System.EventArgs e) { // txtCampo_0.Text = "Creando..."; txtCampo_0.Refresh(); ListView1.Columns.Clear(); // //TODO: En C# hay que usar el nombre de la clase dt = authors.Tabla(txtSelect.Text); crearCampos(dt.Columns); // LabelMostrar.Text = "Regs a mostrar" + "\n\r" + "De 0" + "\n\r" + "A " + (dt.Rows.Count - 1); txtDesde.Text = "0"; if( dt.Rows.Count < 101 ) txtHasta.Text = (dt.Rows.Count - 1).ToString(); else txtHasta.Text = "99"; // foreach(DataColumn col in dt.Columns) { if( col.DataType.ToString().StartsWith("System.Int") ) { ListView1.Columns.Add(col.ColumnName, 60, HorizontalAlignment.Right); } else if( col.DataType.ToString().StartsWith("System.Date") ) { ListView1.Columns.Add(col.ColumnName, 120, HorizontalAlignment.Left); } else { ListView1.Columns.Add(col.ColumnName, 100, HorizontalAlignment.Left); } } // btnRefrescar_Click(sender, EventArgs.Empty); } // private void btnRefrescar_Click(System.Object sender, System.EventArgs e) { int j = Convert.ToInt32(txtDesde.Text); if( j < 0 ) j = 0; if( j > dt.Rows.Count - 1 ) j = dt.Rows.Count - 1; int k = Convert.ToInt32(txtHasta.Text); if( k < 0 ) k = 0; if( k > dt.Rows.Count - 1 ) k = dt.Rows.Count - 1; ListView1.Items.Clear(); for(int n= j; n<= k; n++) { DataRow r = dt.Rows[n]; ListViewItem lvi = null; for(int i= 0; i<= dt.Columns.Count - 1; i++) { if( i == 0 ) lvi = ListView1.Items.Add(r[i].ToString()); else lvi.SubItems.Add(r[i].ToString()); } } txtCampo_0.Text = ""; // btnNuevo.Enabled = true; btnBorrar.Enabled = false; btnActualizar.Enabled = false; } // private void Form1_Load(System.Object sender, System.EventArgs e) { // Centrar horizontalmente el formulairio this.Left = (Screen.PrimaryScreen.Bounds.Width - this.Width) / 2; // //TODO: En C# hay que usar el nombre de la clase txtSelect.Text = authors.CadenaSelect; // ListView1.View = View.Details; ListView1.FullRowSelect = true; ListView1.LabelEdit = false; ListView1.GridLines = true; ListView1.Columns.Clear(); // btnBorrar.Enabled = false; btnActualizar.Enabled = false; btnNuevo.Enabled = false; // btnMostrar_Click(null, null); } // private void ListView1_SelectedIndexChanged(System.Object sender, System.EventArgs e) { if( ListView1.SelectedItems.Count > 0 ) { lviActual = ListView1.SelectedItems[0]; for(int i = 0; i <= lviActual.SubItems.Count - 1; i++) txtCampos[i].Text = lviActual.SubItems[i].Text; btnBorrar.Enabled = true; btnActualizar.Enabled = true; } } // private void btnNuevo_Click(System.Object sender, System.EventArgs e) { // crear un nuevo usuario mClaseTabla = nuevaClaseTabla(); // for(int i = 0; i <= txtCampos.Length - 1; i++) mClaseTabla[i] = txtCampos[i].Text; // string s = mClaseTabla.Crear(); if( s.StartsWith("ERROR") ) MessageBox.Show(s, "Error al crear uno nuevo"); else { lviActual = ListView1.Items.Add(mClaseTabla[0].ToString()); for(int i= 1; i<= txtCampos.Length - 1; i++) lviActual.SubItems.Add(txtCampos[i].Text); } } // private void btnActualizar_Click(System.Object sender, System.EventArgs e) { // actualizar los datos mClaseTabla = nuevaClaseTabla(); // // Tener en cuenta el ID (que será el índice cero) // ya que se usará para buscarlo en la tabla for(int i = 0; i<= txtCampos.Length - 1; i++) { mClaseTabla[i] = txtCampos[i].Text; } string s = mClaseTabla.Actualizar(); if( s.StartsWith("ERROR") ) MessageBox.Show(s, "Error al actualizar"); else for(int i= 1; i<= txtCampos.Length - 1; i++) lviActual.SubItems[i].Text = txtCampos[i].Text; } // private void btnBorrar_Click(System.Object sender, System.EventArgs e) { // Borrar el usuario mClaseTabla = nuevaClaseTabla(); // // Tener en cuenta el ID (que será el índice cero) // ya que se usará para buscarlo en la tabla for(int i = 0; i<= txtCampos.Length - 1; i++) mClaseTabla[i] = txtCampos[i].Text; // string s = mClaseTabla.Borrar(); if( s.StartsWith("ERROR") ) MessageBox.Show(s, "Error al borrar"); else { ListView1.Items.Remove(lviActual); lviActual = null; } btnBorrar.Enabled = false; } } // }...