Solved

Newbie Question - how to access a winforms text box from outside the Class?

Posted on 2006-11-13
7
732 Views
Last Modified: 2008-01-09
I have a form with a text box -
wanted to write some messages to it from an outside class.

got an error stating that i could not do that from my external class, becuase 'it does not exist in that context'

fair enough, so I added a public function to the form1 class (my form is called form1)
that looks like this:

 public void settext(string thetext)
        {
            textBox1.Text += thetext;
        }

Now in my external class, I'm trying to call that function, but I need help with the syntax, as I'm doing something wrong:

  public class jacklogger : WatiN.Core.Interfaces.ILogWriter
    {
        public void LogAction(string message)
        {
            Form1.settext( message + "\r\n");
        }
   
    }

this gives me the error:
"An Object reference is required for the nonstatic field, method, or property WindowsApplication1.Form1.setttext(string)"

What am I doing wrong?
0
Comment
Question by:fruhj
  • 3
  • 3
7 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17930785
You need to call the SetText method on an instance of the class, not the class itself:

   public void LogAction(string message)
   {
       Form1 myForm = new Form1();            // This is probably held somewhere else and is just here to demonstrate that you need an instance of Form1 to call the method on
       myForm.SetText(message + "\r\n");
   }
0
 
LVL 6

Expert Comment

by:dorothy2
ID: 17930789
(1) Did you instantiate Form1 in jacklogger, or pass a reference to it somewhere else?

(2) Is jacklogger only going to be used for Windows application logging? If not, you might want to pass in the form reference as well as the string message reference to LogAction.().

Dorothy
0
 
LVL 12

Author Comment

by:fruhj
ID: 17931004
Carl,

   If I already have Form1 open,

   would the code Form1 myform = new Form1() open a second copy of the form? (I assume so)
   how do I go about pulling from the instance that's already opened?

--------------

Dorothy,

  No instantiating, or passing of references  - the form is the project's default so it's getting created somewhere else.
  how do I go about passing a reference to my code

  for #2, I'm using a preexisting framework that's defined the function LogAction(string message) so I think that means I'm 'overriding' it - so I would not have the option of passing in any additional parameters - I think.

  in my code
  I have a few lines that are (to my limited knowledge) supposed to 'hook' in the logger:
  Logger.LogWriter = new jacklogger.

--------------------------------------------

Ok for everyone's benefit, if it helps - let me give you a big picture view...

I'm using a library called WatiN thats used for web testing.
WatiN by itself is pretty easy to use -

WatiN.Core.IE ie = new WatiN.Core.IE("http://mysite.com");
ie.Link(WatiN.Core.Find.ByText("this is the link text, not the id, nor the name")).Click();
if (ie.Title != "Congratulations, you are on my test page")
            {
                textBox1.Text += "ERROR - WRONG TITLE\r\n";
                failcount += 1;
            }

ok so that's the basics of using WatiN

I don't really want to have to put in a bunch of if statements for each thing I do, and Watin has some built in logging.
by default, the logging goes to the immediate window in the development environment (in this case VC# 2005 express)

I'm told by the people on the WatiN list that I can override thier logging procedure and put the text anywhere I want,
so that's what I'm trying to do.

Here's the complete code from my form - there's a button called 'button1' and thats where I've got all the page logic.
==========================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WatiN.Core.Interfaces;
using WatiN.Core.Logging;


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

        private void button1_Click(object sender, EventArgs e)
        {
            // this establishes the logwriter
            //Logger.LogWriter = new DebugLogWriter();
            Logger.LogWriter = new jacklogger();

            //open IE
            int failcount = 0;
            WatiN.Core.IE ie = new WatiN.Core.IE("http://www.com");

            textBox1.Text += "opened cdps\r\n";
            textBox1.Text += ie.Title + "\r\n";
            ie.Link(WatiN.Core.Find.ByText("mylink text")).Click();
            if (ie.Title != "Welcome to test page")
            {
                textBox1.Text += "ERROR - WRONG TITLE\r\n";
                failcount += 1;
            }
            else
            {   textBox1.Text += "Test 1 Passed - correct Title for page 1\r\n";
            }

            // next check the page for the text"page 2"
            //this is in a div, don't know how to find it...
            if (ie.ContainsText("this is page 2") != true)
            {
                textBox1.Text += "ERROR - Expected Page 2 - not found\r\n";
            }
            else
            {
                textBox1.Text += "Test 2 Passed - welcome to page 2 found on the page\r\n";
            }



            //*******************************************************************
            //if we have a failure, then fail the test...
            if (failcount>0)
            {
                label1.Text = "Result: Fail (" + failcount + " failure(s))";
            label1.ForeColor = System.Drawing.Color.Red ;
            }else{
                label1.Text = "Result: PASS";
                label1.ForeColor = Color.Green;
            }

        }//end button 1 click

        //I added this function to give an external routine a way to access the text object
        //but its not working....
        public void settext(string thetext)
        {
            textBox1.Text += thetext;
        }

    }

    //this is where I believe I am creating my own ILogWriter - with my own output code.
    public class jacklogger : WatiN.Core.Interfaces.ILogWriter
    {
        public void LogAction(string message)
        {
            Form1.settext( message + "\r\n");
        }
   
    }

   
}



 

====================================================================

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17931228
In that case you need to look in the class called "Program.cs", in there is a line that will say something like:

    Application.Run(new Form1());

You need to modify this a little to keep a reference to the form. You will want to change "Program.cs" to look something like:

namespace WindowsApplication1
{
    public static Form1 myForm;

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
   
            myForm = new Form1();
            Application.Run(myForm);
        }
    }
}

Then in your code you can use:

    Program.myForm.SetText("Your text");
0
 
LVL 12

Author Comment

by:fruhj
ID: 17931583
Carl, this looks awesome,

I understand what you're saying, but the compiler does not...

in my code when i type Program my intellisense options are .Equals or .ReferenceEquals

all these failed:
  Program.myForm.SetText
  WindowsApplication1.myForm.SetText,
  myForm.SetText,
  WindowsApplication1.Program.


The error I get each time is - the type or namespace name 'myForm' does not exist in the namespace "WindowsApplication1' (are you using an assembly reference?)

Thanks for working through this with me!
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 17931765
Thats because I wasn't paying attention and put the variable declaration in the wrong place :o)

It should say:

namespace WindowsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
   
            myForm = new Form1();
            Application.Run(myForm);
        }

        public static Form1 myForm;
    }
}
0
 
LVL 12

Author Comment

by:fruhj
ID: 17932031
Carl, you are awesome,

It's working now.

I have some other issues with the code, but this issue is resolved so I'm closing this question.

Thanks again for all your help.

Dorothy -

  I appreciate your input as well, and would normally split points, but in this case you can see that Carl went above and beyond, and for that I owe him the full value (and then some).
 
  - Jack
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now