user2003045
user2003045

Reputation: 17

Copy paste from one form to other c#

What im trying to do:

  1. Search products by BarCode on Form1 form;
  2. if it cannot be found:
    1. Open Inventory form 2. Search product by name or description
    3. On ListView click on the found product copy its BarCode and paste it to the Form1 barcode textbox.

All that is done corectly. The problem is that every time I add product from Inventory form a new Form1 is opened.

The values are not processed in the same Form1, so assume I sell 4 products:

In the end I get 3 opened Form1 forms, one with 2 products and two forms with single product (added through Inventory form). I need them to be all in one.

Thanks

//-------------------------Form1--------------------------------------------

private void btnInventory_Click(object sender, EventArgs e)

    {
        Inventory Inventory = new Inventory();
        Inventory.Show();
    }



 private string _theValue;

    public string TheValue
    {
        get
        {
            return _theValue;
        }
        set
        {
            _theValue = value;
            txtItems.Text = value;

        }
    }

//-----------------------------Inventory---------------------------------

private void ShowForm1()
    {

        string value = label9.Text;
        Form1 newForm = new Form1();
        newForm.TheValue = value;
        this.Close();
        newForm.ShowDialog();
    }



private void lvList_Click(object sender, EventArgs e)

    {
        label9.Text = lvList.SelectedItems[0].SubItems[0].Text;
        this.ShowForm1();

    }

Im sorry for the delay, i had to wait 8h before posting again

Thanks for ur reply.

I just tried that Form1

  private void btnInventory_Click(object sender, EventArgs e)
     {

         Inventory _inv = new Inventory();
         if (DialogResult.OK == _inv.ShowDialog())
             {
                 txtItems.Text = _inv.fugi;
             }

     }

and in Inventory Form

private string test;
        public string fugi
        {
            get { return test; }
            set { test = label9.Text; }
        }



private void lvList_Click(object sender, EventArgs e)
        {

            label9.Text = lvList.SelectedItems[0].SubItems[0].Text;
            this.DialogResult = DialogResult.OK;
            this.Close();


        }

txtItems.Text does not get the value of test from inventory form

Upvotes: 0

Views: 253

Answers (1)

Sayse
Sayse

Reputation: 43300

Its opening a new dialog because you tell it to in ShowForm1, Personally I would change your btnInventory click as follows

private void btnInventory_Click(object sender, EventArgs e)
{
    Inventory _inv = new Inventory();
    if(DialogResult.OK == Inventory.ShowDialog())
    {
          valueIWantToSet = _inv.Accessor;
    }
}

Accessor you will need to make yourself similar to

public TypeOfVar Accessor
{
    get{return m_privateVariableThatIWillMakeAndSetToMyBarcode;}
}

Edit: Once you have gotten the value of your barcode you need to set DialogResult as Follows

 this.DialogResult = DialogResult.OK;

and then set the variable you wish to access to the barcode before closing your form

Edit2: Your ShowForm1 will end up similar to this (May want to rename this method!)

{
this.DialogResult = DialogResult.OK;
m_myVar = SelectedItem..;
this.Close;
}

UPDATE ANSWER

You are still having problems as you haven't used the set property correctly, your get is fine as it is. There is a keyword in c# called value that should be used for setters. this value will take the value of whatever is on the right hand side of an = sign.. you can think of it like this...

fugi = label9.Text

In the above line, fugi is using your properties getter in order to get the value that needs to be set to label9.Text. The = sign says that you intend to use the setter for this property and set the value of value to label9.Text.

Properties with a getter and setter are used so you do not have to provide access to the underlying variable to somewhere you would not like to and then can have the option to just set or get this variable as needed.

This means that your problem persists have you have yet to set the value of test, it is still the default string value. So you have a couple of ways to solve your problem.

The first way is just to provide a getter for label9.Text and remove the need for your private variable.

public string Fugi //Properties should really start with capital letter
{
    get{return label9.Text;}
}

The second is to set the value of test before you call your getter in btnInventoryClick and remove the setter method

private void lvList_Click(object sender, EventArgs e)
{
   test = label9.Text;

and the third ist to set test as shown in method 2 but also change the set method of Fugi to the following to allow this test variable to be set elsewhere.

set{text = value;}

Upvotes: 1

Related Questions