Link to home
Start Free TrialLog in
Avatar of sydneyguy
sydneyguyFlag 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");
        }
    }
}
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

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

Avatar of 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
?

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.
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
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.
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
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
just a cs file not compiling at the moment.
i would just be using a cs file instead of a form
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.
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
    {

    }
}
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();
           
         }

       
    }
}
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for all your help am up and running so thanks you