Foros del Web

Foros del Web (http://www.forosdelweb.com/)
-   .NET (http://www.forosdelweb.com/f29/)
-   -   objetos a nivel formulario en c# (http://www.forosdelweb.com/f29/objetos-nivel-formulario-c-603304/)

robertgustavo 06/07/2008 10:20

objetos a nivel formulario en c#
 
hola, como se declara objetos a nivel en C#
tengo un void que se llama cargardatos asi:
___________________________________

private void CargarDatos()
{
oledbconnection Cn=new oledbconnection();
oledbdataadapter Da=new oledbdataadapter();
dataset ds=new dataset();
try
{
da.fill(ds,"clientes");
datagridview.datasource=ds.tables["clientes"]
}
}

private void Nuevo()
{
//desde aca no puedo usar "Cn", la coneccion o el "Ds", dataset

}

como hago para usarlos en cualquier parte del formulario

Peterpay 06/07/2008 10:53

Respuesta: objetos a nivel formulario en c#
 
Cn y Ds y Da tendrian q estar nivel del inicio de tu class formulario

inmediatamnete despues. de

public Class Fomulario1:Form
{
---> tus propiedades o miembros
--->tus metoods
}

robertgustavo 06/07/2008 17:26

Respuesta: objetos a nivel formulario en c#
 
no funciona
en esta parte:

OleDbDataAdapter Da = new OleDbDataAdapter("Select * from Articulos", Cn);

Cn sale marcado dice:


Error 1 Un inicializador de campo no puede hacer referencia al campo no estático, método o propiedad 'Myprogram.FrmArticulos.Cn' C:\Documents and Settings\Roberth\Mis documentos\Visual Studio 2005\Projects\myprogram\myprogram\FrmArticulos.cs 17 79Myprogram

__________________________________________________ ______________
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

namespace MyProgram
{
public partial class FrmArticulos : Form

{
OleDbConnection Cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|\\SismasDB.mdb");
OleDbDataAdapter Da = new OleDbDataAdapter("Select * from Articulos", Cn);
DataSet Ds = new DataSet();
DataView Dv = new DataView();
BindingSource Bs = new BindingSource();
public int Pos;
public FrmArticulos()
{
InitializeComponent();
}
private void FrmArticulos_Load(object sender, EventArgs e)
{
LlenarDatos();
FormatearGrid();
}
void LlenarDatos()
{
try
{
Da.Fill(Ds, "Articulos");
this.DgArticulos.DataSource = Ds.Tables["Articulos"];
Bs.DataSource = Ds.Tables["Articulos"];
}
catch (OleDbException ex)
{
MessageBox.Show("No se pudo conectar al origen de datos,compruebe que exista o reinstale la aplicación", "Error Inesperado", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
finally
{
Ds.Clear();
Da.Fill(Ds, "Articulos");
}
}
private void FormatearGrid()
{
this.DgArticulos.Columns[0].Width = 60;
this.DgArticulos.Columns[1].Width = 120;
}
private void CerrarArticulosForm(object sender, EventArgs e)
{
this.Close();
}

private void CmdBuscarImagen_Click(object sender, EventArgs e)
{
if (OFDBuscaImagen.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.ImagePreview.Image = Image.FromFile(this.OFDBuscaImagen.FileName);
}
}
private void habilitarControles()
{
this.TxtCodigo.Enabled = true;
this.TxtPrecio.Enabled = true;
this.TxtPrecioCompra.Enabled = true;
this.TxtPrecioVenta.Enabled = true;
this.TxtProducto.Enabled = true;
this.TxtStock.Enabled = true;
this.TxtStockMaximo.Enabled = true;
this.TxtStockMaximo.Enabled = true;
this.CboCategoria.Enabled = true;
this.CboMarca.Enabled = true;
this.CboMedida.Enabled = true;
this.CboProveedor.Enabled = true;
this.CmdSelect1.Enabled = true;
this.CmdSelect2.Enabled = true;
this.CmdSelect3.Enabled = true;
this.CmdSelect4.Enabled = true;
this.CmdBuscarImagen.Enabled = true;
}
private void DeshabilitarControles()
{
this.TxtCodigo.Enabled = false;
this.TxtPrecio.Enabled = false;
this.TxtPrecioCompra.Enabled = false;
this.TxtPrecioVenta.Enabled = false;
this.TxtProducto.Enabled = false;
this.TxtStock.Enabled = false;
this.TxtStockMaximo.Enabled = false;
this.TxtStockMaximo.Enabled = false;
this.CboCategoria.Enabled = false;
this.CboMarca.Enabled = false;
this.CboMedida.Enabled = false;
this.CboProveedor.Enabled = false;
this.CmdSelect1.Enabled = false;
this.CmdSelect2.Enabled = false;
this.CmdSelect3.Enabled = false;
this.CmdSelect4.Enabled = false;
this.CmdBuscarImagen.Enabled = false;

}

private void CmdNuevo_Click(object sender, EventArgs e)
{
habilitarControles();
CmdGuardar.Enabled = true;
CmdNuevo.Enabled = false;
}
private void Nuevo()
{

}

private void CmdGuardar_Click(object sender, EventArgs e)
{
DeshabilitarControles();
CmdNuevo.Enabled=true;
CmdGuardar.Enabled=false;
}

}
}

Peterpay 07/07/2008 07:28

Respuesta: objetos a nivel formulario en c#
 
Si pero puedes poner todo la inicializacion del connection dentro del form load.

si fuece asi tu variable tendria q ser estatica q no es el caso solo quieres q sea globlal.

public partial class FrmArticulos : Form

{
OleDbConnection Cn;
OleDbDataAdapter Da;
DataSet Ds;
DataView Dv;
BindingSource Bs;
public int Pos;
public FrmArticulos()
{
OleDbConnection Cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|\\SismasDB.mdb");
OleDbDataAdapter Da = new OleDbDataAdapter("Select * from Articulos", Cn);
DataSet Ds = new DataSet();
DataView Dv = new DataView();
BindingSource Bs = new BindingSource();
public int Pos;

InitializeComponent();
}

con eso tus variables son de ambito global dentro de tu forma y las inicializas cuando se crea tu forma.

robertgustavo 07/07/2008 08:27

Respuesta: objetos a nivel formulario en c#
 
yes, salio!! gracias Peterpay
Saludos


La zona horaria es GMT -6. Ahora son las 19:18.

Desarrollado por vBulletin® Versión 3.8.7
Derechos de Autor ©2000 - 2026, Jelsoft Enterprises Ltd.