fruhj
asked on
Newbie Question - how to access a winforms text box from outside the Class?
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.ILog Writer
{
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(s tring)"
What am I doing wrong?
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.ILog
{
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.
What am I doing wrong?
(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
(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
ASKER
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.By Text("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.By Text("myli nk 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.ILog Writer
{
public void LogAction(string message)
{
Form1.settext( message + "\r\n");
}
}
}
========================== ========== ========== ========== ========== ==
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.By
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.By
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.ILog
{
public void LogAction(string message)
{
Form1.settext( message + "\r\n");
}
}
}
==========================
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.EnableVisualSt yles();
Application.SetCompatibleT extRenderi ngDefault( false);
myForm = new Form1();
Application.Run(myForm);
}
}
}
Then in your code you can use:
Program.myForm.SetText("Yo ur text");
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.EnableVisualSt
Application.SetCompatibleT
myForm = new Form1();
Application.Run(myForm);
}
}
}
Then in your code you can use:
Program.myForm.SetText("Yo
ASKER
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.Progra m.
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!
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
myForm.SetText,
WindowsApplication1.Progra
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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");
}