如何从一种表格访问某个表格到另一个表格
问题描述:
我试图让公共班级宠物穿着不同形式的连接,但我无法做到,我可以在哪里将公共班级宠物与我创建的每个表单都是?如何从一种表格访问某个表格到另一个表格
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace prueba
{
static class Program
{
/// <summary>
/// Punto de entrada principal para la aplicación.
/// </summary>
public class mascotas
{
public string nombremascota, nombredueño, sexo, especie, estado, respuesta;
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
我尽量让这种形式的连接,但我有一个mascotas错误< -
this.listnombresmas.Items.Add(((mascotas)capturar[i]).);
的完整代码:
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.Collections;
namespace prueba
{
public partial class verdatos : Form
{
ArrayList capturar = new ArrayList();
public verdatos(ArrayList mascotaguardar)
{
capturar = mascotaguardar;
InitializeComponent();
cargadatos();
}
void cargadatos()
{
for (int i = 0; i < capturar.Count; i++)
{
this.listnombresmas.Items.Add(((mascotas)capturar[i]).);
}
}
private void label5_Click(object sender, EventArgs e)
{
}
}
}
另一种形式:
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.Collections;
namespace prueba
{
public partial class Form1 : Form
{
public class mascotas
{
public string nombremascota, nombredueño, sexo, especie, estado, respuesta;
}
string especie = "Pajaro", sexo = "Hembra", respuesta = "Si";
ArrayList nuevamascota = new ArrayList();
//----------------No Modificar NADA-------------------
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void buttonguardar_Click(object sender, EventArgs e)
{
if(compruebadatos()==true)
{
MessageBox.Show("Guardado con éxito");
almacenadatos();
resetcontrols();
}
else
{
MessageBox.Show("Faltan datos");
}
}
void resetcontrols()
{
this.tnomdueño.Text = "";
this.tnommascota.Text = "";
this.radiopajaro.Checked = true;
this.radiohembra.Checked = true;
this.radiosi.Checked = true;
this.listvivienda.SelectedIndex = 0;
}
void almacenadatos()
{
mascotas almacenar = new mascotas();
almacenar.nombremascota = this.tnommascota.Text;
almacenar.nombredueño = this.tnomdueño.Text;
almacenar.estado = this.listvivienda.Text;
almacenar.especie = especie;
almacenar.respuesta = respuesta;
almacenar.sexo = sexo;
nuevamascota.Add(almacenar);
}
#region botones
private void radiopajaro_CheckedChanged(object sender, EventArgs e)
{
if(radiopajaro.Checked)
{
especie = "Pajaro";
}
}
private void radioperro_CheckedChanged(object sender, EventArgs e)
{
if(radioperro.Checked)
{
especie = "Perro";
}
}
private void radiogato_CheckedChanged(object sender, EventArgs e)
{
if(radiogato.Checked)
{
especie = "Gato";
}
}
private void radioconejo_CheckedChanged(object sender, EventArgs e)
{
if(radioconejo.Checked)
{
especie = "Conejo";
}
}
private void radioerizo_CheckedChanged(object sender, EventArgs e)
{
if(radioerizo.Checked)
{
especie = "Erizo";
}
}
#endregion
bool compruebadatos()
{
if(tnommascota.Text == "" || tnomdueño.Text == "")
{
return false;
}
else
{
return true;
}
}
private void buttonverdatos2_Click(object sender, EventArgs e)
{
}
}
}
答
Right cli ck在你的项目上,“Add”和“Class ...”,输入mascotas.cs
作为名字并定义你的班级。您将有这样的事情:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YourNamespace
{
public class mascotas
{
public string nombremascota, nombredueño, sexo, especie, estado, respuesta;
}
}
您定义mascotas
为嵌套类的,所以你没法从另一个类访问它。
如果你的意思是这个!
+0
是的,这是这个!谢谢 !!! – 2014-12-03 20:46:52
+0
@MarcoMontoya:祝你好运;) – 2014-12-03 20:50:16
mascostas是嵌套在Program和Form1中的类。首先,我会删除其中一个,然后使用全名来标识它(例如Form1.mascotas或Program.mascotas,根据哪一个您将保留以及哪一个您将放弃)。甚至更好(在你的情况下)放下其中一个,并将该类移出其包含的类以使其可直接访问。 – 2014-12-03 20:33:42