2 Replies - 935 Views - Last Post: 12 July 2011 - 06:10 PM Rate Topic: -----

#1 Mallbrig  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-April 10

How to add items from active form to another form?

Posted 12 July 2011 - 04:50 PM

Hello all. I am a self taught programer and have run into a problem with my code. Any advice would be greatly appreciated.

I am working on a program that has a listView box in the main form and when a user selects add item it will pop open a new form that has pick lists and of dependent drop downs as well as text boxes. I want to keep the second form open so the user can keep adding material to the list in the main form. I have done something similar before but it was dependent on the close of the second form to add the item to a text box on the main. Below is a sample of my code so far.

This is the code for the button click to add material
 
private void button6_Click(object sender, EventArgs e)//Material selection
        {
            MaterialSelection myForm2 = new MaterialSelection();
            myForm2.ShowDialog();
            myForm2.Dispose();
            textBox2.Text = myForm2.matselected;//gets selected material from mat form
        }



This is the code at the start of the popup form
public string matselected//add class to get selected mat on close
        {
            get { return listBox3.Text; }
        }



In the new I did this
public string memlist
        {
            get { return memlist; }
        }



This code is from another program that I have and it works on close but I want to add the items to a list in another open form. When I add this code it gives me the following error.

Error 2 An object reference is required for the non-static field, method, or property 'programname.AddItem.memlist.get'


Any push in the right direction will be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: How to add items from active form to another form?

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: How to add items from active form to another form?

Posted 12 July 2011 - 05:43 PM

Check this topic:
How To Get Values From Form1 To Form2 (Or Any Other Forms)
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: How to add items from active form to another form?

Posted 12 July 2011 - 06:10 PM

 
private void button6_Click(object sender, EventArgs e)//Material selection
        {
            MaterialSelection myForm2 = new MaterialSelection();
            myForm2.ShowDialog();
            myForm2.Dispose();
            textBox2.Text = myForm2.matselected;//gets selected material from mat form
        }



Line 5 you dispose of myForm2.
Line 6 you try to get data from the {disposed of} myForm2

10pm - Dispose of my car
11pm - Try to drive my car to the store.

Do you see why that would fail?



Attached Image

Can you see where this is a circular reference? It will eventually die of a 'Stack Overflow Error'




Quote

Any push in the right direction will be greatly appreciated.


I would push you toward doing more tutorials or completing a 'Intro to C# book' (cover to cover) before trying to create a project from scratch. I know its not as satisfying to follow instructions to build a project that is not something you feel you can use immediately, but we've all had to start there and work our way up. Its fairly obvious that you don't yet have a grasp on the basics. You wouldn't build a house on a shaky foundation. Don't try to do it with your education.

The tutorials below walk through making an application including inheritance, custom events and custom controls, object serialization and more.
Bulding an application - Part 1
Building an application - Part 2
Quick and easy custom events
Separating data from GUI - PLUS - serializing the data to XML
Passing values between forms/classes

D.I.C. C# Resource page Start here
Intro to C# online tutorial then here...
C# control structures then here.
MSDN Beginner Developer video series
MSDN video on OOP principals, making classes, constructors, accessors and method overloading
MSDN Top guideline violations, know what to avoid before you do it.
Design patterns as diagrams

I hate sending people to another site when we have such good tutorials here, but this series shouldn't be overlooked.
Programming OOP in C# - Part 1
Programming OOP in C# - Part 2
Programming OOP in C# - Part 3
Programming OOP in C# - Part 4
Programming OOP in C# - Part 5


Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)
  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
  • Learn how to search the 'net for examples. How to do a good search that will get you targeted answers.

  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    btnSave(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveMenuItem(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveContextMenu(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    Form1_Closing(object sender, eventargs e)
    {
        if (IsDirty) SavePreferences();
    }
    

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1