hello

i'have a problem using multiple form

the Form1
is the main form

and Form2 , the Form that the user will insert data in, and then these data must appear at Form1

this is my code :)

 public partial class Form1 : Form
    {
        public Form3 ff = new Form3();
        public double pricee;
        public Form1()
        {
            InitializeComponent(); 
        }
        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (checkedListBox1.GetItemChecked(checkedListBox1.SelectedIndex))
            {
                ff.Show();

                if (ff.button1.Equals("button1"))
                {
                    listBox3.Items.Add("hello");
                    label6.Text = "hello";
                }
            }
            else
            {
                listBox2.Items.Remove(checkedListBox1.SelectedItem);
            }

        }

Form2 :

public partial class Form2 : Form
    {
        private double price;

        public Form2()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 f = new Form1();

            if (textBox1.Text.Equals(""))
                MessageBox.Show("insert a numeric value");
            else
            {
                 price = double.Parse(textBox1.Text);
                 this.Hide();
            }
            return;
        }
        public  double getP() 
        {
            return price;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

i'am a new in c# :)

Dani AI

Generated

is right that your code has a few structural problems; the two that stop data appearing on the original form are: (1) creating a brand‑new Form1 inside Form2 (so you update the wrong instance), and (2) treating controls/objects like strings (for example comparing a Button to "button1"). ’s question about which data to show is also relevant — pick a single transfer method and stick with it. Below are two simple, safe patterns: a modal dialog (easy) and an event/callback (decoupled).

Use a modal dialog and a public read-only property (preferred for simple one-off inputs):

/* in Form1 */
using (var dlg = new PriceForm())
{
    if (dlg.ShowDialog(this) == DialogResult.OK)
    {
        double price = dlg.Price;     // read the value the user entered
        listBox3.Items.Add(price.ToString());
        label6.Text = price.ToString();
    }
}
/* in PriceForm (Form2) */
public double Price { get; private set; }

private void okButton_Click(object sender, EventArgs e)
{
    if (double.TryParse(priceTextBox.Text, out var p))
    {
        Price = p;
        DialogResult = DialogResult.OK;
        Close();
    }
    else
    {
        MessageBox.Show("Insert a numeric value");
    }
}

If you want looser coupling (Form2 shouldn’t need to know about Form1), use a callback/event:

/* when creating the form in Form1 */
var f2 = new PriceForm();
f2.OnPriceEntered = p => { listBox3.Items.Add(p.ToString()); label6.Text = p.ToString(); };
f2.Show();
/* in PriceForm */
public Action<double> OnPriceEntered;
private void okButton_Click(...)
{
    if (double.TryParse(priceTextBox.Text, out var p))
    {
        OnPriceEntered?.Invoke(p);
        Close();
    }
}

Final notes: never do new Form1() inside Form2 to talk back to the main window; instead pass data (property/event) or use ShowDialog. Also check SelectedIndex >= 0 before using it and don’t compare control objects to strings.

Recommended Answers

All 2 Replies

niether understand your question nor code
which data do you want to show on form2

Line 3 will not compile, because Form3 does not exist.
Did you mean public Form ff = new Form();
or public Form Form3 = new Form(); ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.