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.
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
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
Here's the function in Form1 (which i'm trying to reference)
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
}
}
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)
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 < temp.Length; i++)
{
newFile[i] = temp[i];
}
File.WriteAllLines(Form1.file, newFile);
ConfigForm.ReadFile(Form1.file);
this.Close();
}
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 =]
I'm pretty new at programming so if any kind souls happen to help could you keep it as simple as possible?
Thanks much =]