Avatar of sydneyguy
sydneyguy
Flag for Australia asked on

simple c# does not exist in the current context question

have set up two forms form1 and form2, i have a messagebox.show on form2 and from form1 want to run the cold that displays the message on form2 , have been reading but still a bit vague on how to get the other function to run by setting up the correct scope to be able to reference it.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace testsiteforconnectionbetweenforms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            msgdisplay();
        }
       
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace testsiteforconnectionbetweenforms
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void msgdisplay()
        {
            MessageBox.Show("hello world");
        }
    }
}
C#

Avatar of undefined
Last Comment
sydneyguy

8/22/2022 - Mon
Mike Tomlinson

You need an instance of Form2, AND the msgdisplay() method must be Public (not Private):
    public partial class Form2 : Form
    {
        public void msgdisplay()
        {
            MessageBox.Show("hello world");
        }
    }

Open in new window


Then, in Form1:
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
            f2.msgdisplay();
        }

Open in new window

sydneyguy

ASKER
thanks for that, but it opens up a new form, what i want to do is to add a codefile and then put this code on there so that it runs but does not open up another form
so i added a new file
which is blank but what goes in here
i understand that    //      f2.Show(); will stop the form from dispalying but the object still runs in the background.
any ideas
Mike Tomlinson

?

Do you want a form or not?  Why is this code you're trying to run in the Form2 class, if you don't want Form2?

Give me a "big picture" of what you're doing and how Form2 fits into this picture.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
sydneyguy

ASKER
big picture is i wish to load up code to do certain jobs on to files instead of forms so that i can use the whole file again and make my program more modular and portable.
so i thought if i loaded them up on codefiles the i coudl better manage them.
so what i want to do is to learn how to run a hello world box and from there progress to passing and returning variables that do there certain jobs.
so this is were i am heading towards
kaufmed

Are you trying to dynamically load Form code from a file? In other words, you want to compile the code "on-the-fly", essentially? It is possible, but I don't think it's advisable. If this is what you are after, then I think you would do better with a plugin-style architecture.
sydneyguy

ASKER
all i want to do is have some data base managament code sitting on a code file so that i can call the function on the codefile and it does its job then returns or would a class file be better to use
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
kaufmed

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
sydneyguy

ASKER
just a cs file not compiling at the moment.
i would just be using a cs file instead of a form
Mike Tomlinson

Gotcha...add a CLASS to your Project and then add that code into the class.  Create an instance of the class (just like with the form) and use that instance to access the code in the methods.
sydneyguy

ASKER
i see that now
but when you add a new class it takes on the same name space.
should it not be able to see the function if it exists under the same namespace but on a different cs file, as i have added a new Class1.cs
i though i could ref the file at the top object back on form1 so that it would see all the code on the Class1 page.
if you have any good sites or examples showing that i am very happy to read up which is what i am doing any way. this area always tricked me up so want to get it mastered
thanks for you help

using System;
using System.Collections.Generic;
using System.Text;

namespace CSV_import_export
{
    class Class1
    {

    }
}
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
sydneyguy

ASKER
but not wanting to define every function that i need in the class its self

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace testsiteforconnectionbetweenforms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Form2 f2 = new Form2();
      //      f2.Show();
            msgdisplay();
           
         }

       
    }
}
sydneyguy

ASKER
i guess this does expose the whole code for the page that i require so will keep going with this and keep reading to better under stand it
do you know if any really good tutroials that go through setting up an manipulating classes and namespaces ect on the web
thanks
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
sydneyguy

ASKER
thanks for all your help am up and running so thanks you
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.