您好我刚接触到C#的东西我刚刚学习。
所以我的问题是我想将listBox1
项Form1
从Form2
发送到listBox2
listBox1
,并显示Form2
的项目在public partial class TopliNapitki : Form
{
public TopliNapitki()
{
InitializeComponent();
}
int pomozno=0;
decimal cena = 0;
decimal kava = 1.10m;
decimal cap = 1.30m;
decimal bKava = 1.60m;
decimal kavaM = 1.30m;
decimal caj = 1.30m;
decimal kakav = 1.70m;
decimal vCokolada = 1.80m;
decimal lKava = 2.50m;
public string _textBox1
{
get { return textBox1.Text; }
}
public string _listBox1
{
get { return listBox1.Text; }
}
private void button1_Click(object sender, EventArgs e)
{
var form1 = new Form1();
this.Close();
form1.Show();
}
int stetjeKava = 1;
private void button2_Click(object sender, EventArgs e)
{
int id=1;
if (stetjeKava > 1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex=id-1);
listBox1.Items.Add(id + "." + "Kava" + " Cena: " + kava + "x" + stetjeKava+" " +cena);
cena = cena + kava;
textBox1.Text = Convert.ToString(cena);
}
if (stetjeKava == 1)
{
listBox1.Items.Add(id + "." + "Kava" + " Cena: " + kava + "x" + stetjeKava);
cena = cena + kava;
textBox1.Text = Convert.ToString(cena);
}
stetjeKava = stetjeKava + 1;
pomozno = pomozno + 1;
}
int stetjeCap=1;
private void button3_Click(object sender, EventArgs e)
{
int id = 2;
if (stetjeCap > 1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex = id - 1);
listBox1.Items.Add(id + "." + "Capuccino" + " Cena: " + cap + "x" + stetjeCap + " " + cena);
cena = cena + cap;
textBox1.Text = Convert.ToString(cena);
}
if (stetjeCap == 1)
{
listBox1.Items.Add(id + "." + "Capuccino" + " Cena: " + cap + "x" + stetjeCap);
cena = cena + cap;
textBox1.Text = Convert.ToString(cena);
}
stetjeCap = stetjeCap + 1;
}
int stetjeBKava = 1;
private void button4_Click(object sender, EventArgs e)
{
int id = 3;
if (stetjeBKava > 1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex = id - 1);
listBox1.Items.Add(id + "." + "Bela Kava" + " Cena: " + bKava + "x" + stetjeBKava + " " + cena);
cena = cena + bKava;
textBox1.Text = Convert.ToString(cena);
}
if (stetjeBKava == 1)
{
listBox1.Items.Add(id + "." + "Bela Kava" + " Cena: " + bKava + "x" + stetjeBKava);
cena = cena +bKava;
textBox1.Text = Convert.ToString(cena);
}
stetjeBKava = stetjeBKava + 1;
}
int stetjeKavaM = 1;
private void button5_Click(object sender, EventArgs e)
{
int id = 4;
if (stetjeKavaM > 1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex = id - 1);
listBox1.Items.Add(id + "." + "Kava z Mlekom" + " Cena: " + kavaM + "x" + stetjeKavaM + " " + cena);
cena = cena + kavaM;
textBox1.Text = Convert.ToString(cena);
}
if (stetjeKavaM == 1)
{
listBox1.Items.Add(id + "." + "Kava z Mlekom" + " Cena: " + kavaM + "x" + stetjeKavaM);
cena = cena + kavaM;
textBox1.Text = Convert.ToString(cena);
}
stetjeKavaM = stetjeKavaM + 1;
}
int stetjeCaj = 1;
private void button6_Click(object sender, EventArgs e)
{
int id = 5;
if (stetjeCaj > 1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex = id - 1);
listBox1.Items.Add(id + "." + "Čaj" + " Cena: " + caj + "x" + stetjeCaj + " " + cena);
cena = cena + caj;
textBox1.Text = Convert.ToString(cena);
}
if (stetjeCaj == 1)
{
listBox1.Items.Add(id + "." + "Čaj" + " Cena: " + caj + "x" + stetjeCaj);
cena = cena + caj;
textBox1.Text = Convert.ToString(cena);
}
stetjeCaj = stetjeCaj + 1;
}
int stetjeKakav = 1;
private void button7_Click(object sender, EventArgs e)
{
int id = 6;
if (stetjeKakav > 1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex = id - 1);
listBox1.Items.Add(id + "." + "Kakav" + " Cena: " + kakav + "x" + stetjeKakav + " " + cena);
cena = cena + kakav;
textBox1.Text = Convert.ToString(cena);
}
if (stetjeKakav == 1)
{
listBox1.Items.Add(id + "." + "Kakav" + " Cena: " + kakav + "x" + stetjeKakav);
cena = cena + kakav;
textBox1.Text = Convert.ToString(cena);
}
stetjeKakav = stetjeKakav + 1;
}
int stetjeVCoko = 1;
private void button8_Click(object sender, EventArgs e)
{
int id = 7;
if (stetjeVCoko > 1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex = id - 1);
listBox1.Items.Add(id + "." + "Vroča čokolada" + " Cena: " + vCokolada + "x" + stetjeVCoko + " " + cena);
cena = cena + vCokolada;
textBox1.Text = Convert.ToString(cena);
}
if (stetjeVCoko == 1)
{
listBox1.Items.Add(id + "." + "Vroča čokolada" + " Cena: " + vCokolada + "x" + stetjeVCoko);
cena = cena + vCokolada;
textBox1.Text = Convert.ToString(cena);
}
stetjeVCoko = stetjeVCoko + 1;
}
int stetjeLKava = 1;
private void button9_Click(object sender, EventArgs e)
{
int id = 8;
if (stetjeLKava > 1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex = id - 1);
listBox1.Items.Add(id + "." + "Ledena kava" + " Cena: " + lKava + "x" + stetjeLKava + " " + cena);
cena = cena + lKava;
textBox1.Text = Convert.ToString(cena);
}
if (stetjeLKava == 1)
{
listBox1.Items.Add(id + "." + "Ledena kava" + " Cena: " + lKava + "x" + stetjeLKava);
cena = cena + lKava;
textBox1.Text = Convert.ToString(cena);
}
stetjeLKava = stetjeLKava + 1;
}
private void button10_Click(object sender, EventArgs e)
{
var form1 = new Racun();
form1.Show();
form1._textBox = _textBox1;
form1._listBox = _listBox1;
this.Close();
form1.Show();
}
}
}
。
Im来自斯洛文尼亚,因此更新了表格名称和其他条目
这是第一个名为:
的表格 public partial class Racun : Form
{
public Racun()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var form1 = new Form1();
this.Close();
form1.Show();
}
public string _textBox
{
set { textBox1.Text = value; }
}
public string _listBox
{
set { listBox1.Items.Add(value); }
}
}
第二个表格:
public string _listBox1
{
get { return listBox1.Text; }
}
private void button10_Click(object sender, EventArgs e)
{
var form1 = new Racun();
form1.Show();
form1._listBox = _listBox1;
this.Close();
form1.Show();
}
}
我有这个设置用于发送listBox的数据,但它在Form1上不起作用:
public string _listBox
{
set { listBox1.Items.Add(value); }
}
Form2中有这个:
{{1}}
答案 0 :(得分:2)
传递构造函数中的值:
string _listValue;
public Racun(string listValue)
{
InitializeComponent();
_listValue = listValue;
}
它将被称为:
private void button1_Click(object sender, EventArgs e)
{
var form1 = new Rancun(_listBox);
this.Close();
form1.Show();
}
我还认为您在尝试从列表框中取出所选项目时遇到错误。它应该像这样调用:
get { return listBox1.SelectedValue.ToString(); }