accessing form from class

R

Ringo

namespace Microsoft.ServiceModel.Samples
{
public partial class Form1 : Form
{ ...... }
public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
{
How do I use a control on the main form from this
Class???????
textbox1.text="foo";//???????????????????????
}

You shouldn't. But if you must, just change the access modifier for the
control, in the Properties in the VS Designer, so that it's visible to
other classes. Then of course you need a reference to your form in that
class.
A much better way (but still requires getting a reference to the form to
some place where the CalculatorService class can see it) is to provide
some kind of accessor in your form class that encapsulates the control's
text. For example:

Thanks, I can't figure out how to initialize _form. Can you please
explain that part?
Thanks
Ringo





namespace Microsoft.ServiceModel.Samples
{
public partial class Form1 : Form
{
public string TextBox1Text
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
}

public class CalculatorService : ICalculator
{
private Form1 _form; // this has to be initialized somewhere,
like
your constructor for example

public double Add(double n1, double n2)
{
_form.TextBox1Text = "foo";
}
}
 
R

Ringo

You do it the same way you'd initialize any variable.

Without a concise-but-complete code example, there's no way to offer a  
specific suggestion.  You just assign the value you want to the variable.  
But where you get that value depends on the rest of your code.

Pete

Here is the complete code. I guess I don't understand if I initialize
it in the form or in the new class.
Thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Microsoft.ServiceModel.Samples
{
[ServiceContract(Namespace = "http://
Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
// Step7: Create the method declaration for the contract.
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);

}


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static TextBox tb1 = new TextBox();
public string TextBox1Text
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
public string SpSend
{
set { sp.Write(value); }
}

private void Form1_Load(object sender, EventArgs e)
{
// Step 1 of the address configuration procedure: Create a
URI to serve as the base address.
// Uri baseAddress = new Uri("http://localhost:8000/
ServiceModelSamples/Service");
Uri baseAddress = new Uri("http://192.168.1.103:8000/
ServiceModelSamples/Service");
tb1.Text = "ok";
tb1.Visible = true;
tb1.Top = 50;
tb1.Left = 50;
tb1.BringToFront();
Controls.Add(tb1);


// Step 2 of the hosting procedure: Create ServiceHost
ServiceHost selfHost = new ServiceHost(typeof
(CalculatorService), baseAddress);

try
{
// Step 3 of the hosting procedure: Add a service
endpoint.
selfHost.AddServiceEndpoint(
typeof(ICalculator),
new WSHttpBinding(),
"CalculatorService");


// Step 4 of the hosting procedure: Enable metadata
exchange.
ServiceMetadataBehavior smb = new
ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);

// Step 5 of the hosting procedure: Start (and then
stop) the service.
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate
service.");
Console.WriteLine();
// Console.ReadLine();
// Console.WriteLine("terminated service.");
// Close the ServiceHostBase to shutdown the service.
// selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}",
ce.Message);
selfHost.Abort();
}
}

private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("terminated.");

// Close the ServiceHostBase to shutdown the service.
// selfHost.Close();

}
}
public class CalculatorService : ICalculator
{
private Form1 _form;
public double Add(double n1, double n2)
{
double result = n1 + n2;
_form.TextBox1Text = "foo";

Console.WriteLine("Received Add({0},{1})", n1, n2);
// Code added to write output to the console window.
Console.WriteLine("Return: {0}", result);
return result;
}

public double Subtract(double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine("Received Subtract({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}

public double Multiply(double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine("Received Multiply({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}

public double Divide(double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine("Received Divide({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}


}
}
 
A

ajstadlin

If your "Form1" was loaded by your Application, then you can access it by
calling:

((Form1)Application.OpenForms["Form1"])

Explaned:

1. Application.OpenForms is a collection of the Forms openned by the
Application
2. "Form1" as the index into OpenForms[] assumes that your Form1 has the
default .Name property value. You can change this. OpenForms[0] is probably
the main application thread's form.
3. Use the (Form1) cast to access the Methods, Properties, Controls,
Components you added to the form that are declared with Public scope.

ajs
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top