If I run my project I get this window:
Then I press the button auto hinzufügen == add car . Here the code of the method:
private void button1_Click(object sender, EventArgs e)
{
addCarcs window = new addCarcs();
window.Show();
}
Now the second window opens:
I fill both fields and press the button auto hinzufügen == add car . Then I create a new object of the class car . Here the code:
private void button1_Click(object sender, EventArgs e)
{
if((kennzeichen.Text != "") && (automarke.Text != ""))
{
car myCar = new car();
myCar.addCar(kennzeichen.Text, automarke.Text);
this.Close();
}
}
Here is the code of the class car :
class car
{
public car()
{
this.carMark = "";
this.carMark = "";
}
public void addCar(string carMark, string carBrand)
{
this.carMark = carMark;
this.carBrand = carBrand;
carNumber++;
}
public string showCar()
{
return string.Format(
"Automarke: {0} --- Autokennzeichen {1}", this.carBrand, this.carMark);
}
private string carMark;
private string carBrand;
static int carNumber;
}
Now I come to the main Window again:
Now I press the second button auto anzeigen == show car . I want to show the added car, but if I write this code in the button method:
private void showCar_Click(object sender, EventArgs e)
{
myCar.showCar();
}
I get an error:
How can I send the created object's of a class between different forms?
You need to create a Property Car MyCar { get;set; }' at the class level. Set its value in the constructor like
Car MyCar { get;set; }' at the class level. Set its value in the constructor like
Car MyCar { get;set; }' at the class level. Set its value in the constructor like
MyCar = new Car() and access the
MyCar` everywhere you need the Car object.
You should create a member car in the class that "creates" both windows, so both forms you open "recognize" the member car, and can access its data.
You can find more info here: https://msdn.microsoft.com/en-us/library/ms173121.aspx
and here: https://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx
Well, you need to do the following:
Add a property called addedCar to your addCarcs Class:
public class addCarcs : Form
{
public car addedCar { set; get; }
}
Add a property Called latestAddedCar to your Form1 Class:
public class Form1 : Form
{
public car latestAddedCar { set; get; }
}
Change you addition logic to a ShowDialog :
private void button1_Click(object sender, EventArgs e)
{
addCarcs window = new addCarcs();
DialogResult dResult = window.ShowDialog();
if (dResult == System.Windows.Forms.DialogResult.OK)
{
latestAddedCar = window.addedCar;
}
}
Change your Data Entry Submission Logic to:
private void button2_Click(object sender, EventArgs e)
{
if ((kennzeichen.Text != "") && (automarke.Text != ""))
{
car myCar = new car();
myCar.addCar(kennzeichen.Text, automarke.Text);
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
}
Finally, access the newly added Attribute from the Main class Form1 :
private void showCar_Click(object sender, EventArgs e)
{
if(latestAddedCar != null)
latestAddedCar.showCar();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.