the value of filteredSymptoms is rashes, cough, diarrhea and when passing these values of filteredSymptoms to another form, the value of filteredSymptoms is not being passed to the form2 (this is the second form). What part of my code is wrong or missing something?
Form1
FinalScreen showForm = new FinalScreen();
showForm.checkedSymptoms = filteredSymp;
Form2
public string checkedSymptoms { get; set; }
label1.Text = checkedSymptoms;
Create an overload constructor on FinalScreen
public FinalScreen()
{
InitializeComponent();
}
public FinalScreen(string checkedSymptoms)
:base()
{
InitializeComponent();
CheckedSymptoms = checkedSymptoms;
}
public string CheckedSymptoms { get; set; }
private void FinalScreen_Load(object sender, EventArgs e)
{
label1.Text = CheckedSymptoms;
}
Then
FinalScreen showForm = new FinalScreen(filteredSymp);
Related
I am new to c#, I am creating a quiz program (I am at the very beginning) and I have generated a class with a question, 4 answers and a correct answer. I have created one question in Public form1, How do I make the correct answer show up in a messagebox when button1 is clicked?
namespace Prog02
{
public class Question
{
public string Que { get; }
public string Ans1 { get; }
public string Ans2 { get; }
public string Ans3 { get; }
public string Ans4 { get; }
public string CorrectAns { get; set; }
public Question(string que, String ans1, String ans2, String ans3, String ans4, String correctans)
{
Que = que;
Ans1 = ans1;
Ans2 = ans2;
Ans3 = ans3;
Ans4 = ans4;
CorrectAns = correctans;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//define question1
Question Question1 = new Question("What number is lowest", "1", "2", "3", "4", "1");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Question1.CorrectAns);
}
}
}
You have created an instance of the Question class directly in the constructor of the form (within Form1, below the method call InitializeComponent). The specified variable Question1 is only valid in this local scope of this specific function. So if you want to access it from outside of this function, you have to make it available in the class. This can be achieved by declaring a field within the class like private Question question; directly before the constructor call and changing your assignment to question = new Question(...);
public partial class Form1 : Form
{
private Question question;
public Form1()
{
InitializeComponent();
this.question = new Question("What number is lowest", "1", "2", "3", "4", "1");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.question.CorrectAns);
}
}
You should move out the question instance and make it a field.
Also there were some other wrong parts with the code.
Here is the full code with the fixes.
Added comments for describing the changes.
using System;
using System.Collections.Generic;
namespace Prog02
{
public class Question
{
// Added "private set" to the properties below
public string Que { get; private set;}
public string Ans1 { get; private set;}
public string Ans2 { get; private set;}
public string Ans3 { get; private set;}
public string Ans4 { get; private set;}
public string CorrectAns { get; private set; }
public Question(string que, String ans1, String ans2, String ans3, String ans4, String correctans)
{
Que = que;
Ans1 = ans1;
Ans2 = ans2;
Ans3 = ans3;
Ans4 = ans4;
CorrectAns = correctans;
}
}
public partial class Form1 : Form
{
// Created a private field for holding the question instance.
private Question question1;
public Form1()
{
InitializeComponent();
//define question1
question1 = new Question("What number is lowest", "1", "2", "3", "4", "1");
}
private void button1_Click(object sender, EventArgs e)
{
// Use the instance here ("question1" instead of "Question1")
MessageBox.Show(question1.CorrectAns);
}
}
}
I have a form1.cs that contains
List<Shape> shapeList = new List<Shape>();
listBoxDB listBoxHist = new listBoxDB();
public Form1()
{
listBoxHist.Show();
InitializeComponent();
this.DoubleBuffered = true;
}
I have another file called listBoxDB.cs that has a button that will take data from a listbox in listBox and store in it a string Lname that form1 uses. However I can't access my shapelist to add to the list correct due to protection level. How can I fix this?
private void button2_Click(object sender, EventArgs e) //load 1 line
{
//string s = Form1.datInfo;
Form1.Lname = listBox1.GetItemText(listBox1.SelectedItem);
//Console.WriteLine(text);
Shape Line = new Line();
Line.loadLine();
Form1.shapeList.Add(Line); <-- I get a protection error here.
Invalidate();
}
I am able to access Lname using this getter/setter
public static string Lname { get; set; }
How can I do the same to access my shapeList in form1 within another form?
Your problem seems to be the difference between static and instance variables.
If you make Lname static, like this public static string Lname { get; set; }, then you can access it via Form1.Lname, but it effectively becomes a globally defined variable. If you create two instances of Form1 they would both share the same Lname and that isn't usually what you should do.
Instead what you should do is declare it as public string Lname { get; set; } and then you could make a SetForm1 method on your listBoxDB form to pass the reference to the instance of Form1 in so that you can access Form1's public variables from listBoxDB.
Your code could then look something like this:
public class Form1 : Form
{
public string Lname { get; set; }
public List<Shape> shapeList = new List<Shape>();
private listBoxDB listBoxHist = new listBoxDB();
public Form1()
{
listBoxHist.SetForm1(this);
listBoxHist.Show();
InitializeComponent();
this.DoubleBuffered = true;
}
}
public class listBoxDB : Form
{
private Form1 _form1 = null;
public void SetForm1(Form1 form1)
{
_form1 = form1;
}
private void button2_Click(object sender, EventArgs e) //load 1 line
{
//string s = Form1.datInfo;
_form1.Lname = listBox1.GetItemText(listBox1.SelectedItem);
//Console.WriteLine(text);
Shape Line = new Line();
Line.loadLine();
_form1.shapeList.Add(Line);
Invalidate();
}
}
I have this code to send it from Form1 to Form2:
public partial class Form1 : Form
{
public ShoppingBasket myBasket = new ShoppingBasket();
public Form1()
{
InitializeComponent();
}
private void editButton_Click(object sender, EventArgs e)
{
int c = lstCart.Items.Count - 1;
for (int i = c; i <= 0; i++)
{
if (lstCart.GetSelected(i))
{
Form2 fm2 = new Form2();
fm2.productNameTextBox.Text = myBasket[i].ProductName;
fm2.quantityTextBox.Text = Convert.ToString(myBasket[i].Quantity);
fm2.latestPriceTextBox.Text = Convert.ToString(myBasket[i].LatestPrice);
fm2.ShowDialog();
}
}
}
}
Then this is my Form2 code:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
Form1 fm1 = new Form1();
private void okBtn_Click(object sender, EventArgs e)
{
int c = fm1.lstCart.Items.Count - 1;
for (int i = c; i <= 0; i++)
{
if (this.fm1.lstCart.GetSelected(i))
{
this.fm1.myBasket[i].ProductName = this.productNameTextBox.Text;
this.fm1.myBasket[i].Quantity = Convert.ToInt32(this.quantityTextBox.Text);
this.fm1.myBasket[i].LatestPrice = Convert.ToDecimal(this.latestPriceTextBox.Text);
this.Close();
}
}
}
private void cancelBtn_Click(object sender, EventArgs e)
{
this.Close();
}
}
This is my ShoppingBasket class:
public class ShoppingBasket : List<OrderItem>
{
public ShoppingBasket()
{
}
public decimal BasketTotal { get; set; }
public new void Add(OrderItem i)
{
base.Add(i);
}
public new void Remove(OrderItem i)
{
base.Remove(i);
}
OrderItem class:
public class OrderItem
{
public OrderItem(string productName,
decimal latestPrice, int quantity)
{
ProductName = productName;
LatestPrice = latestPrice;
Quantity = quantity;
TotalOrder = latestPrice * quantity;
}
public string ProductName { get; set; }
public decimal LatestPrice { get; set; }
public int Quantity { get; set; }
public decimal TotalOrder { get; set; }
}
The problem I am getting is that it gives me: 'ArgumentOutOfRangeException was unhandled' saying "Index -1 is out of range. Parameter name: index" pointing to this line:
if (this.fm1.lstCart.GetSelected(i))
But previously it has given me another error saying "Object reference not set to an instance of an object."
How do I make it so that the values previously in the selected field in Form1 are changed to the values I pass from Form2 back to Form1?
As daniel mentioned you need to pass a reference to your form1 into form2, personally I'd do it in a constructor
public Form2(Form1 form)
{
fm1 = form;
}
Then you really should try to only update a forms fields within the form itself whereever possible so since form2 is modal I'd do something similar to this
using(Form2 fm2 = new Form2(this))
{
fm2.productNameTextBox.Text = myBasket[i].ProductName;
fm2.quantityTextBox.Text = Convert.ToString(myBasket[i].Quantity);
fm2.latestPriceTextBox.Text = Convert.ToString(myBasket[i].LatestPrice);
if(DialogResult.OK == fm2.ShowDialog(this))
{
myBasket[i].ProductName = frm2.productNameTextBox.Text;
myBasket[i].Quantity = Convert.ToInt32(frm2.quantityTextBox.Text);
myBasket[i].LatestPrice = Convert.ToDecimal(frm2.latestPriceTextBox.Text);
}
}
then to close form2 use
this.DialogResult = DialogResult.OK;
When you make new Form1() inside Form2, you are creating a completely new Form1 with no filled data. It's not the original Form1 that called Form2.
So, what you need is to set that Form1 to the original, not to a new one:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form1 fm1; //just declare, but let Form1 do the assign job.
//the rest of the code...
}
And in Form 1
for (int i = c; i <= 0; i++)
{
if (lstCart.GetSelected(i))
{
Form2 fm2 = new Form2();
fm2.productNameTextBox.Text = myBasket[i].ProductName;
fm2.quantityTextBox.Text = Convert.ToString(myBasket[i].Quantity);
fm2.latestPriceTextBox.Text = Convert.ToString(myBasket[i].LatestPrice);
//here is the news:
fm2.fm1 = this;
fm2.ShowDialog();
}
}
I'm not able to pass data of multiple text boxes from Form1 to Form2 through a button click. I have tried the following way, but it is not working.
Did I do it wrong? And if so, how can I do it?
Form1 Code:
public partial class Form1: Form {
Form2 frm2;
public Form1() {
InitializeComponent();
}
private void btnInvoice_Click_1(object sender, EventArgs e) {
this.Hide();
if(frm2==null)
frm2=new Form2();
frm2.ValueFromForm1(txtFirstName.Text);
frm2.ValueFromForm1(txtLastName.Text);
frm2.ValueFromForm1(txtCellNo.Text);
frm2.ValueFromForm1(txtDate.Text);
frm2.ValueFromForm1(txtDueDate.Text);
frm2.Show();
}
}
Form2 Code:
public partial class Form2: Form {
public Form2() {
InitializeComponent();
}
public void ValueFromForm1(string value) {
txtFirstName.Text=value;
txtLastName.Text=value;
txtCellNo.Text=value;
txtMaskDueDate.Text=value;
txtMaskDate.Text=value;
}
}
You are assigning same value to all textboxes. And result will be last value you are assigning (which is txtDueDate text). Create different methods to assign values for each of textboxes, or pass values as array, or as a custom object:
public class Invoice
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string CellNo { get; set; }
// etc
}
On Form1
private void btnInvoice_Click_1(object sender, EventArgs e)
{
this.Hide();
if (frm2 == null)
frm2 = new Form2();
Invoice invoice = new Invoice();
invoice.FirstName = txtFirstName.Text;
invoice.LastName = txtLastName.Text;
invoice.CellNo = txtCellNo.Text;
// etc
frm2.Invoice = invoice;
frm2.Show();
}
On Form2
public Invoice Invoice
{
set
{
txtFirstName.Text = value.FirstName;
txtLastName.Text = value.LastName;
txtCellNo.Text = value.CellNo;
// etc
}
}
How about in creating a Properties in Form2? Creating a class is one of the solution but you could simple create Properties to get and set
Form1
private void btnInvoice_Click_1(object sender, EventArgs e)
{
this.Hide();
using(var f = new Form2())
{
f.FirstName = txtFirstName.Text;
f.LastName = txtLastName.Text;
f.CellNo = txtCellNo.Text;
f.ShowDialog();
}
}
Form2
public string FirstName { get; set; }
public string LastName { get; set; }
public string CellNo { get; set; }
private void Form2_Load(object sender, EventArgs e)
{
txtFirstName.Text = FirstName;
txtLastName.Text = LastName;
txtCellNo.Text = CellNo;
}
The method implementation is very wrong, or atleast the logic you are thinking! You are assigning same value to all textboxes.
You can solve the problem two ways.
The easy way is to change the modifier for controls in Form2 from private to internal or public. After changing modifier u can call it like
private void btnInvoice_Click_1(object sender, EventArgs e)
{
this.Hide();
if (frm2 == null)
frm2 = new Form2();
frm2.txtFirstName.Text = txtFirstName.Text;
frm2.txtLastName.Text = txtLastName.Text;
frm2.txtCellNo.Text = txtCellNo.Text;
frm2.txtDate.Text = txtDate.Text;
frm2.txtDueDate.Text = txtDueDate.Text;
frm2.Show();
}
How to change modifier ?
or the complicated way by creating multiple Methods like
public void ValueForFirstName(string value)
{
txtFirstName.Text = value;
}
public void ValuForLastName(string value)
{
txtLastName.Text = value;
}
I have a quick question. I created a class in the Form2 called "MyObject" which has two variables in it. On the push of a button, the variables in Form2 are changed. Now my question is how to retrieve MyObject in Form1? Here is my sample code:
Form1
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this);
f.ShowDialog();
??????? (how can I retrieve Myobject here?????)
}
Form2
public class MyObject
{
public int Value1 { get; set; }
public int Value2 { get; set; }
}
public Form2(Form1 frm1)
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyObject obj = new MyObject();
obj.Value1 = 102;
obj.Value2 = 50;
}
Thanks everyone
Do this
Form1
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this);
f.ShowDialog();
MyObject mo = f.GetMyObject;
}
Form2
public Form2(Form1 frm1)
{
InitializeComponent();
}
public MyObject GetMyObject
{
get
{
return obj;
}
}
MyObject obj;
private void button1_Click(object sender, EventArgs e)
{
obj = new MyObject();
obj.Value1 = 102;
obj.Value2 = 50;
}
MyObject
public class MyObject
{
public int Value1 { get; set; }
public int Value2 { get; set; }
}
You shouldn't define MyObject inside of Form2. You should put it in it's own file. You almost always want each class to have its own file.
You should also give MyObject a better name; something that's more descriptive of what it actually does/holds.
Finally, to actually pass the data one easy and effective way is to just create a property on Form2:
public MyObject PropertyName {get; private set;}
(Don't forget to rename the property to something better.)
You can then set it from within Form2 by saying ProperyName = new MyObject();
You can then access that property from Form1 (after your call to ShowDialog) by using f.PropertyName.