所以小伙子们,我创建了一个CreateChar : Form
并创建了一个BaseHero
级英雄。
首先,我需要从createCharForm设置我的英雄状态。如何将其连接到另一个表单?这样我就可以轻松访问类值...
public partial class CreateChar : Form
{
private SoundPlayer backgroundMusic;
//public int HP
//{
// get
// {
// int isi;
// isi = (int)hpUpDown.Value;
// return isi;
// }
//}
//public int Mana
//{
// get
// {
// int isi;
// isi = (int)manaUpDown.Value;
// return isi;
// }
//}
//public int Damage
//{
// get
// {
// int isi;
// isi = (int)damageUpDown.Value;
// return isi;
// }
//}
//public int Def
//{
// get
// {
// int isi;
// isi = (int)defUpDown.Value;
// return isi;
// }
//}
public CreateChar()
{
InitializeComponent();
}
private void CreateChar_Load(object sender, EventArgs e)
{
backgroundMusic = new SoundPlayer("createChar.wav");
backgroundMusic.PlayLooping();
}
private void okBtn_Click(object sender, EventArgs e)
{
BaseHero Hero = new BaseHero();
Hero.Hitpoints = (int)hpUpDown.Value;
Hero.Mana = (int)manaUpDown.Value;
Hero.Damage = (int)damageUpDown.Value;
Hero.Defense = (int)defUpDown.Value;
}
}
public partial class Form1 : Form
{
BaseHero danang = new BaseHero();
monsterAir pugna = new monsterAir();
monsterAir dua = new monsterAir();
public Form1()
{
InitializeComponent();
//testTextBox.Text = pugna.Hitpoints.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = danang.Hitpoints.ToString();
textBox2.Text = danang.Exp.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
textBox3.Text = dua.Hitpoints.ToString();
textBox4.Text = dua.Expearned.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
danang.Attack(dua);
danang.checkMonster(dua);
}
private void btn_buatbaru_Click(object sender, EventArgs e)
{
CreateChar formCharacterBaru = new CreateChar();
if (formCharacterBaru.ShowDialog() == DialogResult.OK)
{
BaseHero Hero = new BaseHero();
Hero.Hitpoints = formCharacterBaru.HP;
Hero.Mana = formCharacterBaru.Mana;
Hero.Damage = formCharacterBaru.Damage;
Hero.Defense = formCharacterBaru.Def;
}
}
}
答案 0 :(得分:0)
使表单相互引用的一种方法是创建它们并在构造函数中传递它们。
Form1 frm1;
public CreateChar(Form1 f1)
{
InitializeComponent();
frm1 = f1;
}
在form1构造函数中:
CreateChar cc;
public Form1()
{
InitializeComponent();
cc = new CreateChar(this);
//testTextBox.Text = pugna.Hitpoints.ToString();
}