Sej Sej - 3 months ago 9
C# Question

C# Specific value passing between forms without new instance

I have a C# application that allows the user to log certain events that occur in a game. For simplicity I'll call them

ParentForm
and
ChildForm
.

ParentForm
is used 99% of the time, to log common events. This is represented as the user clicking a
PictureBox
and the
Tag
property of that
PictureBox
being added to a
ListBox
. When a "rare" event occurs, the user can click a "log rare event" button on
ParentForm
to open
ChildForm
which opens a set of "rare event"
PictureBoxes
, which function the same as in the
ParentForm
. The challenge is that I want these common and rare events to be logged to the same
ListBox
, so I am trying to find out how I would get a
PictureBox
click (and subsequent
Tag
from this
PictureBox
) on the
ChildForm
to the
ListBox
on the ParentForm.

The
ParentForm
does not close while
ChildForm
is open, and needs to stay open.

In the
ParentForm
code, I already have the code needed to capture one of the
PictureBox
clicks and grabbing the
Tag
, as well as handling dealing with adding it to the
ListBox
, so it'd be nice if I could just use these.

Here's what I've tried so far for the Parent:

// This file is EventLogger.cs
using rareEvent;
namespace mainWindow {
public partial class EventLogger : Form {
// In the ParentForm (listeners for PictureBox clicks are handled elsewhere)
public void pictureBox_Click(object sender, EventArgs e) {

PictureBox pbSender = (PictureBox) sender;

// Open new window and handle "rare" drops
if (pbSender.Tag.ToString() == "rare") {

// Open rare form
EventLogger.RareForm rare = new EventLogger.RareForm();
rare.Show();
}
}
}
}


and here's the child:

// This file is Rare.cs
using EventLogger;
namespace rareEvent {
public partial class rareEventForm : Form {

// In the ChildForm
private void pictureBox_Click(object sender, EventArgs e) {

// Does not compile if form is not instantiated, but I do not
// want a new instance
EventLogger form;
form.pictureBox_Click(sender, e);
}
}
}


I figured something like this would work, but it gives the error

The type or namespace name 'EventLogger' does not exist in the namespace
'mainWindow' (are you missing an assembly reference?)


Any help would be much appreciated. All the other examples I've found of value passing between forms all seem to create new instances which I don't want or were 8 years old and didn't work.

Appreciate it!

Edit: Code updated to have
using <namespace>
in each file. The problem still exists of not being able to send values between both forms without using
new
. (See comment to this answer)

Answer

In the first form create an instance (of it) here like my form1. It must be static and all datatypes you want to access should be public.

//FORM1
public partial class Form1 : Form
{
    //Instance of this form
    public static Form1 instance;

    //For testing
    public string myProperty = "TEST";

    //Assign instance to this either in the constructor on on load like this 
    public Form1()
    {

        InitializeComponent();
        instance = this;
    }
    //or
    private void Form1_Load(object sender, EventArgs e)
    {
        //Assign the instance to this class
        instance = this;
    }

Then in form2 when calling EventLogger.RareForm rare = new EventLogger.RareForm(); instead of new form do

EventLogger.RareForm rare = EventLogger.RareForm.instance

Or in my case

Form1 frm = Form1.instance;

I then check the property of form 1 FROM form2 like so

Console.WriteLine(frm.myProperty);

Output was "Test"

Any trouble shout.