Solved

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

Posted on 2006-11-13
7
746 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

617 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