I'm using CompactFramework to make a WinCE App.
I have a Form1 and a Class1. In the Form1 I have a label1 which I need to get access from Class1.
If I try:
Form1.label1
it doesn't appear, despite I set Modifier property to public.
How could I get access to the label?
Thanks for any help!
The issue is that Form1 is a class, and you need an instance of that class, because your label is not static.
var myForm = new Form1();
myForm.label1.Text = "hello";
Obviously you don't want to create the new form from your class, because your application likely created the form.
So you should pass the form into the class. Does the class get instantiated by the form? If so you can pass "this" into the class to send a reference to the instantiated form.
But really, you don't want to do this, because its difficult to maintain. Better would be to have the class raise some sort of event that the form could subscribe to to update itself.
You can instance Form1 to Class1 and then use this instance to set Form1.label1.
This would be an elegant solution. Check this code:
public partial class Form1 : Form
{
public Label Label1 { get; set; }
public void Caller()
{
MyClass cls = new MyClass();
cls.Form1 = this;
cls.DoSomeJob();
}
}
public class MyClass
{
public Form1 Form1 { get; set; }
public void DoSomeJob()
{
Form1.Label1.Text = "Some text...";
}
}
Try to create property or method on the Form1 class to return label1 object
Related
I'm trying to append text in my richtextbox which is called ConsoleText. It's not working very well. I'm using a property in my form to access the richtextbox in the Class.
It looks like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Skipped the rest
public string ACText
{
set
{
ConsoleText.AppendText(value);
}
}
Now from my class's constructor.
public McDonalds(string email, string pass)
{
Form1 f = new Form1();
f.ACText = "test";
}
It's not showing any text in my richtextbox sadly. I know it works, because i can in the property use a messageBox and see that the value is passed into it.
Thanks in advance i really need help with this.
Calling Form1 f = new Form1(); does not give you a reference to an existing form, it creates a new one with blank/default values in the form's controls.
HOW to solve this greatly depends on your design. If you want to tie your class to that form implementation, our class needs either a reference to the form, a reference to the control, or the value of the control that you're interested in passed to it.
For example:
public McDonalds(string email, string pass, Form1 form)
{
form.ACText = "test";
}
A cleaner solution would be to RETURN a value from your McDonalds class and let the FORM set the control value appropriately rather than tying your class to that form class.
you can use Singleton:
Singletons make having single instances easy. They allow for single allocations and instances of data. We review the singleton types. We see one of the fastest implementations. And we review other possibilities.
public partial class Form1 : Form
{
public static Form1 instance = null;
public Form1()
{
instance = this; //add this class to singleton
InitializeComponent();
}
public void Show(string Message)
{
MyConsole.Text = Message;
}
another class:
Form1.instance.Show("blah blah");
Create Delegate in Form1 Class binded method ACText (string val), and Pass the Delegate to McDonalds Class. Fire the Delegate
namespace YourNameSpace
{
public delegate void RichTextBoxDelegate(string text);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void ACText(string s)
{
ConsoleText.AppendText(s);
}
// In Some Method Call MCDonald's form
public void ShowMcDonalds()
{
RichTextBoxDelegate deleg = new RichTextBoxDelegate(ACText);
MCdonalds ob = new McDonalds(deleg);
ob.show();
}
}
}
Pass the deleg to McDonalds form
Just fire the Delagate
public McDonalds(RichTextBoxDelegate sp)
{
Form1 f = new Form1();
sp("This is Test");
}
deleg("Test value"); // form McDonald's Form
I am working on something and I have to instantiate the class. My question is where would I do that at? Would I do it before this:
public partial class Form1 : Form
{
InputClass myclass = new InputClass();
public Form1()
{
InitializeComponent();
}
Or
public partial class Form1 : Form
{
public Form1()
{
InputClass myclass = new InputClass();
InitializeComponent();
}
Here is another code I am working on but it is not working out to well this is what my code looks like right now:
public partial class Form1 : Form
{
InputClass myClass = new InputClass();
myClass.yourname = "";
myClass.Banner = "";
public Form1()
{
InitializeComponent();
}
I am new to C# and I am trying to figure this out. I need to instantiate the class. Then when the page load add to set the labels text from the _banner variable. Then add code to set the property yourname from the text in the textbox when the user presses the button. Then i need to clear the textbox. I also have to display the name in a messagebox from the class.
class InputClass
{
public string _banner;
private string _yourName;
public InputClass(String _banner)
{
this._banner = _banner;
}
public string yourName
{
get { return _yourName; }
set { _yourName = value; }
}
}
}
If you want to access your object from other methods in your class then you need to use a member field rather than a local variable.
private InputClass myClass = new InputClass { YourName = "", Banner = "" };
public Form1()
{
InitializeComponent();
}
Another option is to declare a member field but initialize it inside the constructor:
private InputClass myClass;
public Form1()
{
InitializeComponent();
this.myClass = new InputClass { YourName = "", Banner = "" };
}
This isn't too useful in your specific case, but it can be useful if you need to pass parameters from your constructor to the InputClass constructor.
You can only set the properties inside a function body! not inside the class context.
Instantiating the class would work inside Form1() or at declaration time. IMO best style in your case would be:
public partial class Form1 : Form
{
InputClass myclass;
public Form1()
{
InitializeComponent();
myclass = new InputClass();
}
}
This enables one to use myClass not only in the Form1 constructor, but also in any other function.
First of all, make the distinction between Declaration and Instantiation. In your first snippet, you're declaring the InputClass member in the class scope, meaning it will be shared by all methods in the class. Once you do that, it doesn't matter if you instantiate it in the constructor or during declaration, it (mostly) works out to the same thing.
Secondly, I'm guessing that this is an ASP.NET project, since you refer to "page load". If so, remember that your Form1 instance doesn't stay alive between page loads. Every time you reload the page, either manually with F5 or via button-clicks/postbacks, you're creating a new instance of Form1, which will create a new instance of InputClass.
What you're doing in the first example is a declaration of a member variable of Form1 named myclass. You can assign it a value at the same place, which is fine:
public partial class Form1 : Form
{
InputClass myclass = new InputClass();
public Form1()
{
InitializeComponent();
}
}
But you usually cannot insert actual code statements in your class declaration (like the assignment myClass.yourname = ""). You need to put them in the constructor. So a correct way to do this would be:
public partial class Form1 : Form
{
InputClass myClass = new InputClass();
public Form1()
{
myClass.yourname = "";
myClass.Banner = "";
InitializeComponent();
}
}
For performing actions on button click, look here: http://msdn.microsoft.com/en-us/library/43sxkdeb(v=vs.80).aspx
I have a windows form and my own class in my project
I have a method in my own class
public object Sample(Form MyForm,string ComponentName)
{
}
I want to get components of the "MyForm" from another class How Can I Make THIs?
form class
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
MyOwnClass
public class Sample
{
public object GetComponentMethod(Form form,string ComponentName)
{
////
}
}
Have you tried with:
Control myControl= form.controls.Find(...)?
updated
Sorry but in this case I cannot understand what are you looking for!
updated
you have to create a public property Components! So you can retrieve data you need!
It looks like you are just trying to access members of one object from another object.
If so you need to expose some way of accessing a specific instance of a class.
If you will only ever have one instance (of say your Form1) the simplest way is to expose that single instance via a public static property. This is called a singleton pattern:
public partial class Form1 : Form
{
public static Form1 Singleton { get; private set; }
public Form1()
{
Form1.Singleton = this;
InitializeComponent();
}
}
You can the access your Form1 instance using Form1.Singleton.SomeProperty from anywhere.
I am not promoting any specific Singleton pattern here, as there are too many issues over thread safety, but for your simple example this will do the job. Call the static property "Singleton" or "This" or "SolutionToMyWoes" or whatever you like!
I need to call "panel.invalidate" outside my form (WINform) class also I need to change some other controls as well, I read similar question here, and tried what they said, but it didn't work and I wasn't convinced at all.
The answer I read was about exposing a public method like this:
public void EnableButton(bool enable)
{
this.myButton.Enabled = enable;
}
Also I made a static instance in the other file
static Form1 myForm = new Form1();
Any useful suggestions??
The problem is the "myForm" reference. It is a reference to an instance of Form1 that isn't visible and doesn't match the one that the user is looking at. It can't be a match, you created a new one.
Whatever class needs to update the form must have a constructor that takes a Form1 reference. You can create the class object in your Form1 constructor or Load event, pass "this". Using Application.OpenForms[0] is another way to get the reference, one you should not use.
Are you updating from the same thread? Otherwise you might need to use Invoke.
Here's a nice short article about how to do that:
http://blogs.msdn.com/csharpfaq/archive/2004/03/17/91685.aspx
Control.Invalidate() is a public method, but the control itself is most likely not public. You will have to expose the call to Control.Invalidate() through a public facing method in your form or by marking the control in question as public.
public class MyForm : Form {
private TextBox tbxName = new TextBox();
public InvalidateTextBox() {
tbxName.Invalidate();
}
}
OR
public class MyForm : Form {
public TextBox tbxName = new TextBox();
}
public class SomeOtherClass {
public void InvalidateTextBox(MyForm form) {
form.tbxName.Invalidate();
}
}
I have a simple windows application in C# with 3 forms.
first form is main form (its name is FrmMain), second is FrmData and third is FrmShow.
In main form (FrmMain) I have created an instance from second form (FrmData) and show it :
public partial class FrmMain : Form
{
public Form FrmModifyData; //for FrmData
int PersonCode;
public FrmMain()
{
InitializeComponent();
}
private void btnShowDataForm_Click(object sender, EventArgs e)
{
FrmModifyData= new FrmData();
FrmModifyData.ShowDialog();
}
}
but I can't access from FrmModifyData to FrmMain fields like PersonCode .
How can I access to creator object's field?
Note: I'm a beginner.
thanks.
You would need to add a property to your FrmModifyData class to take an instance of the FrmMain class. Then you can do this:
FrmModifyData = new FrmData();
FrmModifyData.ParentData = this;
FrmModifyData.ShowDialog();
Then inside FrmModifyData you would have access to the public members of FrmMain. Obviously this is kind of quick and dirty and not very reusable so i would suggest adding more explicit properties to FrmModifyData with only the data you need to use.
If you want to access PersonCode field, you should declare it as public. No visibility modifier will make it private, hence not accesible from other casses.
I would make it something like this.
With this way you're able to use the FrmModifyData in other forms.
I know it's an old post, but yes, you did read it :)
public partial class FrmMain : Form
{
// public Form FrmModifyData; <-- do not declare it in your FrmMain
// (is't a modal dialog, so you won't get more instances)
public int PersonCode {get; set;}
public FrmMain()
{
InitializeComponent();
}
private void btnShowDataForm_Click(object sender, EventArgs e)
{
FrmData FrmModifyData = new FrmData();
FrmModifyData.PersonCode = this.PersonCode;
DialogResult result = FrmModifyData.ShowDialog();
if(result == DialogResult.Ok)
{
// do something with the result
this.PersonCode = FrmModifyData.PersonCode;
}
}
}