PC Review


Reply
Thread Tools Rate Thread

How to access form controls from "static" methods?

 
 
Jon E. Scott
Guest
Posts: n/a
 
      10th Jan 2007
I'm a little confused with "static" methods and how to access other unstatic
methods. I'm a little new to C#.

I'm testing a callback routine within a DLL and the callback function
returns a string as one of the arguments. As shown below, I have no
problems showing the string in the Report() method in a messagebox or
console window, but how does one access a memo or label on a form from a
static method? I want to throw the strings from the callback function into
a memo control on the form.

namespace WindowsApplication1
{
public partial class Form1 : Form
{
private delegate void CallBack(int testInt, string testStr);

[DllImport("N:\\Temp\\Project1.dll")]
private static extern bool TestCallBack(CallBack x);

private void UpdateStatus(string status)
{
label1.Text = status;
}

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
CallBack myCallBack = new CallBack(Form1.Report);
TestCallBack(myCallBack);
}

private static void Report(int testInt, string testStr)
{
//Form1.UpdateStatus(testStr);
MessageBox.Show(string.Concat(testInt.ToString(), " - ",
testStr));
}
}
}

--
Thanks,
Jon E. Scott
Blue Orb Software
http://www.blueorbsoft.com



 
Reply With Quote
 
 
 
 
Brian Gideon
Guest
Posts: n/a
 
      10th Jan 2007
Jon,

Make the callback method an instance member instead of a static member.
When creating the callback delegate use the syntax this.Report instead
of Form1.Report.

Brian

Jon E. Scott wrote:
> I'm a little confused with "static" methods and how to access other unstatic
> methods. I'm a little new to C#.
>
> I'm testing a callback routine within a DLL and the callback function
> returns a string as one of the arguments. As shown below, I have no
> problems showing the string in the Report() method in a messagebox or
> console window, but how does one access a memo or label on a form from a
> static method? I want to throw the strings from the callback function into
> a memo control on the form.
>
> namespace WindowsApplication1
> {
> public partial class Form1 : Form
> {
> private delegate void CallBack(int testInt, string testStr);
>
> [DllImport("N:\\Temp\\Project1.dll")]
> private static extern bool TestCallBack(CallBack x);
>
> private void UpdateStatus(string status)
> {
> label1.Text = status;
> }
>
> public Form1()
> {
> InitializeComponent();
> }
>
> private void button1_Click(object sender, EventArgs e)
> {
> CallBack myCallBack = new CallBack(Form1.Report);
> TestCallBack(myCallBack);
> }
>
> private static void Report(int testInt, string testStr)
> {
> //Form1.UpdateStatus(testStr);
> MessageBox.Show(string.Concat(testInt.ToString(), " - ",
> testStr));
> }
> }
> }
>
> --
> Thanks,
> Jon E. Scott
> Blue Orb Software
> http://www.blueorbsoft.com


 
Reply With Quote
 
DeveloperX
Guest
Posts: n/a
 
      10th Jan 2007
One option would be to have a static member variable which holds a
reference to the form, then you would just need to beginInvoke on the
control on the form.

Jon E. Scott wrote:
> I'm a little confused with "static" methods and how to access other unstatic
> methods. I'm a little new to C#.
>
> I'm testing a callback routine within a DLL and the callback function
> returns a string as one of the arguments. As shown below, I have no
> problems showing the string in the Report() method in a messagebox or
> console window, but how does one access a memo or label on a form from a
> static method? I want to throw the strings from the callback function into
> a memo control on the form.
>
> namespace WindowsApplication1
> {
> public partial class Form1 : Form
> {
> private delegate void CallBack(int testInt, string testStr);
>
> [DllImport("N:\\Temp\\Project1.dll")]
> private static extern bool TestCallBack(CallBack x);
>
> private void UpdateStatus(string status)
> {
> label1.Text = status;
> }
>
> public Form1()
> {
> InitializeComponent();
> }
>
> private void button1_Click(object sender, EventArgs e)
> {
> CallBack myCallBack = new CallBack(Form1.Report);
> TestCallBack(myCallBack);
> }
>
> private static void Report(int testInt, string testStr)
> {
> //Form1.UpdateStatus(testStr);
> MessageBox.Show(string.Concat(testInt.ToString(), " - ",
> testStr));
> }
> }
> }
>
> --
> Thanks,
> Jon E. Scott
> Blue Orb Software
> http://www.blueorbsoft.com


 
Reply With Quote
 
Brian Gideon
Guest
Posts: n/a
 
      10th Jan 2007

DeveloperX wrote:
> One option would be to have a static member variable which holds a
> reference to the form, then you would just need to beginInvoke on the
> control on the form.
>


If there is more than one of the same Form running then the callback
method would see the value of that variable as it was set by the last
created instance.

Also, it's not completely clear that BeginInvoke is needed. If
TestCallBack executes synchronously then the callback will be executing
on the UI thread as well. If TestCallBack executes asynchronously then
BeginInvoke would be needed.

 
Reply With Quote
 
DeveloperX
Guest
Posts: n/a
 
      10th Jan 2007
Very true, I could of fleshed that out an mentioned InvokeRequired, etc
but it looked asynchronous (don't make me justify that ). I did
consider mentioning schemes for handling multiple instances of the
form, but assumed it wasn't a requirement based on the snippet

Brian Gideon wrote:
> DeveloperX wrote:
> > One option would be to have a static member variable which holds a
> > reference to the form, then you would just need to beginInvoke on the
> > control on the form.
> >

>
> If there is more than one of the same Form running then the callback
> method would see the value of that variable as it was set by the last
> created instance.
>
> Also, it's not completely clear that BeginInvoke is needed. If
> TestCallBack executes synchronously then the callback will be executing
> on the UI thread as well. If TestCallBack executes asynchronously then
> BeginInvoke would be needed.


 
Reply With Quote
 
Jon E. Scott
Guest
Posts: n/a
 
      10th Jan 2007
"Brian Gideon" wrote...
>
> Make the callback method an instance member instead of a static member.
> When creating the callback delegate use the syntax this.Report instead
> of Form1.Report.


Knew it was something simple like that. Got it working. Thanks!

--
Thanks,
Jon E. Scott
Blue Orb Software
http://www.blueorbsoft.com


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Other methods for preventing "not have exclusive access at this time"message? jlbaze@gmail.com Microsoft Access Security 1 16th Jan 2009 01:15 AM
"New Record" methods (not Access "methods") Monty Microsoft Access 9 26th Sep 2006 04:05 AM
<FORM METHOD="post" onSubmit="return fieldcheck()" name="orientation" action="http://ws-kitty.BU.edu/AT/survey/orientation/script/write.asp" language="JavaScript"> Joeyej Microsoft ASP .NET 0 4th Jun 2004 08:55 PM
"Static" Methods in Class Modules Ripan Microsoft Excel Programming 2 12th Jan 2004 04:55 PM
"Static" Methods in Class Modules Ripan Microsoft Excel Misc 1 12th Jan 2004 04:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:33 PM.