Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing functions from other forms
    primarykey
    data
    text
    <p>I'm working on program in c# which uses a listbox as a selection method in its main form. It has functions where you can edit the items in the listbox.</p> <p>I wanted to edit the items from a seperate dedicated form, so I made a new instance of the form but whenever I try to access the original form's functions (which I have made public) I get this error: Error 2 An object reference is required for the non-static field, method, or property</p> <p>I've looked around on the internet quite a bit but all I see is people talking about using a static property on my function. However, when I do this I get more of the above errors popping up over variables and the like within the function</p> <p>Here's the function in Form1 (which i'm trying to reference)</p> <pre><code>public void ReadConfig(string configFile) { fileList.Clear(); listBoxName.Items.Clear(); FileStream file = null; if (!File.Exists(file)) { MessageBox.Show(file + " was not found: Creating blank file"); using (file = File.Create(file)) ; } else { string line; int lineNumber = 1; // I cut out some long code here where the program reads from a file and saves it to an object } } </code></pre> <p>Here's a code snippet for where the error takes place (I cut some code where I save it to a text file, but the main part that's of concern is the Form1.ReadFile(Form1.file)</p> <pre><code> private void buttonSave_Click(object sender, EventArgs e) { string[] temp = File.ReadAllLines(Form1.file); string[] newFile; if (itemNew == true) { newFile = new string[temp.Length + 1]; } else { newFile = new string[temp.Length]; } for (int i = 0; i &lt; temp.Length; i++) { newFile[i] = temp[i]; } File.WriteAllLines(Form1.file, newFile); ConfigForm.ReadFile(Form1.file); this.Close(); } </code></pre> <p>I hope that's enough code to go off. My program is pretty long so I tried to keep it as short and direct as possible. Thanks for being patient with me =]</p> <p>I'm pretty new at programming so if any kind souls happen to help could you keep it as simple as possible?</p> <p>Thanks much =]</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload