Updated to reflect to my own source
I'm in process of building my first winform application in c# and I'm trying to figure out the best practice for structuring my classes to work smoothly when I use them in my forms.
I have a couple of examples which I will try to explain the best way i can.
When working with get/set variables in a class, the best practice should be something like this:
JobMove.cs
public class JobMove
{
private List<string> jobNames { get; set; }
public string Scanner;
public JobMove()
{
this.Scanner = Properties.Settings.Default.Scanner;
}
public void ListSelected(ListBox lbx)
{
foreach (string jName in this.jobNames)
{
lbx.Items.Add(jName);
}
}
public static List<string> GetCheckedJobs(ListView lw)
{
int countChecked = lw.CheckedItems.Count;
int itemCount = 0;
List<string> jList = new List<string>();
foreach (ListViewItem item in lw.CheckedItems)
{
JobInfo jobInfo = Job.Find(Convert.ToInt32(lw.Items[item.Index].SubItems[1].Text));
jList.Add(jobInfo.Name);
itemCount++;
}
return jList;
}
}
My problem is when I combine this with my forms and I call this, then I would try to do something like this:
MyForm1.cs
public partial class MyForm1 : Form
{
private void btnMoveJobs_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Scanner = cbxScanners.SelectedItem.ToString();
JobMove moveJobs = new JobMove();
frmMoveJobs FrmMoveJobs = new frmMoveJobs();
FrmMoveJobs.ShowDialog();
}
}
MyForm2.cs
public partial class frmMoveJobs : Form
{
public frmMoveJobs()
{
InitializeComponent();
JobMove moveJobs = new JobMove();
lblFrom.Text = moveJobs.Scanner;
moveJobs.ListSelected(lbxJobsToMove);
cbxMjScanners.DataSource = System.Enum.GetValues(typeof(Scanners));
}
}
But when I call MyClass in MyForm2 and I want to call the DoSomethingElse method, then myString will be reset to a null value. And that makes sense to me, but how do I work around this?
I tried to figure out what to use here to get easier around these flaws in my code, but my knowledge is far too weak to just implement an easy solution.
I know I could just store this variable in Settings.settings as an example, but to me that just seems like a real overload for such a simple task.
I might just need a point in the right direction to right on what to do in this situation.
If you do a MyClass myClass = new MyClass(); then indeed - the values are independent and unrelated. If you want to share the MyClass instance then pass the MyClass instance between the forms. Perhaps:
using(var form2 = new Form2()) {
form2.SensibleName = existingMyClassInstance;
form2.ShowDialog();
}
(note the using above btw; when using ShowDialog() it is your job to make sure the form is disposed; it only gets disposed automatically if using Show())
Firstly, they're properties, not variables (the variables are the underlying data source).
Secondly, the whole point of get/set accessors is so you can get and set the value without needing helper methods.
Thirdly, and as to your problem, you're creating a new instance of the class in each form (hinted at by the new keyword) and the value of the property will be whatever it is initialised as on construction of the instance (or not.) i.e. the values of properties are not shared between different instances of the same type.
Think of the mold for a key: I can get multiple instances of the key cut from a "blueprint", but any damage that one suffers won't be reflected by the rest - they're unique in that sense.
If you want the forms to both access the same instance of that type, then you will need to stash the instance somewhere in your code which is accessible to both.
A few options:
Pass in an instance of MyClass in the form2's constructor.
Make MyClass a static property of either Form1 or Form2 and access it via that on the other form.
Make MyClass static (not recommended).
If you want to use the instance of MyClass created in MyForm1 inside of MyForm2, you need to provide it to MyForm2.
Something like this would work:
public partial class MyForm2 : Form
{
public MyForm2(MyClass given)
{
InitializeComponent();
given.DoSomethingElse();
}
}
Easy Solution:
private static string myString { get; set; }
Why: because you initialize the class again when initializing Form2 and it will create a new class. With the "static" keyword you create a property which is the same in all instances of this class.
BUT: please read some books before continuing, this would be the solution to this problem, but the source of many others. Try to understand C# and Forms first, than (or alongside with reading/learning) start coding!
this is because each of your form has a new object of "MyClass".
To achieve what you want to do use a static property... this won't be initialized and gives back the same value for each object of MyClass
it looks like this
public class MyClass {
public static string myString { get; set; }
public void ChangeMyString(string newString)
{
myString = newString;
}
public void DoSomethingElse()
{
MessageBox.Show(myString);
}
}
Related
I've got a class where I've declared this struct
public struct Tasks
{
public string tName;
public string tDate;
public string tPriority;
public string tDescription;
}
In Form1, I've constructed
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Tasks[] entry=new Tasks[taskName.Length];
}
}
This works fine and dandy, but I want to use entry struct one a button press.
private void TaskName1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(entry[i].tDate, entry[i].tName, entry[i].tPriority);
frm.Show();
}
It obviously gives the warning "The name "entry" does no exist in the current context".
I'm fairly new to programming, but I have spent a few hours trying many things to no avail.
I've tried finding ways to make the entry[array] public somehow.
I've created public class and declared the struct and construct public, but still I couldn't get it to work.
I wanted to use the entry struct to send to another form because that's where I hold information and I will use the struct to write to ext files.
Any solutions or better ways to do this?
How can I use a struct across multiple forms, classes, etc.? Basically anywhere.
You keep your Tasks array inside the constructor of your form. So it's visible only within the scope of the constructor. To make it visible for other Form1 members it has to be declared on the level of your Form1 class:
public partial class Form1 : Form
{
Tasks[] entry;
public Form1()
{
InitializeComponent();
entry = new Tasks[taskName.Length];
}
}
This is only to make your syntax correct. Whether your approach is worth pursuing is entirely different question and I strongly advise you to read through the comments to your question.
You're attempting to reference a local variable declared in the constructor in a method that knows nothing about this variable. C# has a concept called "scope", in which variable declarations are only accessible by the block of code that creates them. ie. If you declare a variable inside method MyMethod(), you can only directly access it inside MyMethod(). Scopes can be nested, too, with any nested scope being able to access any variables in the parent scope, but not vice-versa. For example:
class MyClass {
int someInt = 0;
void MyMethod() {
int anotherInt = 5;
someInt += 1; // This works, as someInt was declared as a class-wide variable.
}
void AnotherMethod() {
anotherInt = 6; // Will not work! anotherInt was declared in MyMethod, so is in a separate scope than that of AnotherMethod.
string aString = "asd";
if (someInt == 0) { // Again, this works because this variable is declared in the parent scope.
string anotherString = "fgh";
aString = "asdasd"; // Works
}
anotherString = "oh no"; // Does not work, because if-statements create their own scope!
}
}
Basically, every time you see an opening curly brace ( { ), you know that a new scope is being created.
Move the declaration outside of the Form1() constructor, and just define it in the constructor.
public partial class Form1 : Form {
Tasks[] entry;
public Form1() {
InitializeComponent();
entry = new Tasks[taskName.Length];
}
}
You can then reference entry from any methods inside Form1.
I'm wondering if it is possible to access a textbox value from another class inside a C# winform.
For example, at the moment I have a bunch of different textboxes I'm turning on and off all within my Form1.cs class like so:
screentextBox.Visible = true;
However, to cut down on the amount of lines of code within my C# class I was wondering is it possible to make this call from another class, then in my Form1.cs call my other classes method?
Something like:
class Otherclass
{
public void ShowTextBox()
{
screentextBox.Visible = true;
}
}
Then in my Form1.cs simply call my new ShowTextBox method.
I'm sorry if this is a silly question, but I've looked around google and I couldn't find anything that could help me out.
You could pass the TextBox as a parameter to a function in another class:
class OtherClass
{
public void ShowTextBox(TextBox target)
{
target.Visible = true;
}
}
However, I would advise to keep all the methods and code pertaining to handling the GUI and its events inside the form itself. If you have large methods for calculations, etc., than those can be moved to other classes.
you can Make ScreentextBox as Public in Declaring class and access it in Another class like
class Otherclass
{
public void ShowTextBox()
{
Class1.ScreenTextBox.Visible =true;
}
}
You could define the ShowTextBox method in a partial class So you still have the access to the control and also tidy your code.
Add method for showing TextBox in your form:
public partial class Form1 : Form
{
public void ShowTextBox()
{
screentextBox.Visible = true;
}
}
and then pass your From1 to other forms and call this method from there.
Class OtherClass
{
public static void method(TextBox[] items)
{
foreach(item in items)
{
(item as TextBox).Visible = true;
}
}
}
to call this method from ur Form1.cs class--->
OtherClass.method( new TextBox[] { TxtBox1, TxtBox2, TxtBox3 } );
If you want to access the controls of Form1.cs from another class try this way
class Otherclass
{
Form1 f1 = new Form1();
f1.Controls["screentextBox"].Visible = true;
}
I would do it like this (example from John Willemse):
class OtherClass
{
public TextBox ShowTextBox(TextBox target)
{
target.Visible = true;
return target;
}
}
Yet another approach to this old problem: I've found that the old way is an easy way to make accessible controls (including all their properties and methods), and perhaps other variables, from any class within the project. This old way consists of creating an ad hoc class from scratch.
Note A: about the old way: I know, I know, global variables are evil. But, for many people coming here looking for a fast/flexible/suites-most-cases solution, this may be a valid answer and I have not seen it posted. Another thing: this solution is what I am actually using as the answer for what I came to this page looking for.
1st step: The new class file from scratch is below.
namespace YourProjectNamespace
{
public class dataGlobal
{
public System.Windows.Forms.TextBox txtConsole = null;
// Place here some other things you might want to use globally, e.g.:
public int auxInteger;
public string auxMessage;
public bool auxBinary;
// etc.
}
}
Note B: The class is not static nor has static members, which allows to create several instances in case it is needed. In my case I do take advantage of this feature. But, as a matter of fact, you may consider making this class' TextBox a public static field so that -once initialized- it is always the same throughout the application.
2nd step: Then you're able to initialize it in your Main Form:
namespace YourProjectNamespace
{
public partial class Form1 : Form
{
// Declare
public static dataGlobal dataMain = new dataGlobal();
public Form1()
{
InitializeComponent();
// Initialize
dataMain.txtConsole = textBox1;
}
// Your own Form1 code goes on...
}
}
3rd step: And from your other class (or form), the call to any property/method of Form1's textBox1:
namespace YourProjectNamespace
{
class SomeOtherClass
{
// Declare and Assign
dataGlobal dataLocal = Form1.dataMain;
public void SomethingToDo()
{
dataLocal.txtConsole.Visible = true;
dataLocal.txtConsole.Text = "Typing some text into Form1's TextBox1" + "\r\n";
dataLocal.txtConsole.AppendText("Adding text to Form1's TextBox1" + "\r\n");
string retrieveTextBoxValue = dataLocal.txtConsole.Text;
// Your own code continues...
}
}
}
[EDIT]:
A simpler approach, specifically for the TextBox visibility throughout classes, I have not seen in other answers:
1st step: Declare and initialize an auxiliary TextBox object in your Main Form:
namespace YourProjectNamespace
{
public partial class Form1 : Form
{
// Declare
public static TextBox txtConsole;
public Form1()
{
InitializeComponent();
// Initialize
txtConsole = textBox1;
}
// Your own Form1 code goes on...
}
}
2nd step: And from your other class (or form), the call to any property/method of Form1's textBox1:
namespace YourProjectNamespace
{
class SomeOtherClass
{
public void SomethingToDo()
{
Form1.txtConsole.Visible = true;
Form1.txtConsole.Text = "Typing some text into Form1's TextBox1" + "\r\n";
Form1.txtConsole.AppendText("Adding text to Form1's TextBox1" + "\r\n");
string retrieveTextBoxValue = Form1.txtConsole.Text;
// Your own code continues...
}
}
}
Comment to the [Edit]: I have noticed that many questions simply cannot be solved by the usual recommendation: "instead, make public properties on your form to get/set the values you are interested in". Sometimes there would be several properties/methods to implement... But, then again, I know... best practices should prevail :)
The case is this, I have two different forms from the same solution/project. What I need to do is to extract the value of a label in Form A and load it into Form B. As much as possible, I am staying away from using this code since it will only conflict my whole program:
FormB myForm = new FromB(label.Text);
myForm.ShowDialog();
What I am trying right now is a class with a property of get and set for the value I wanted to pass. However, whenever I access the get method from FormB, it returns a blank value.
I hope somebody can help me with this. Any other ways to do this is extremely appreciated. :)
public class Miscellaneous
{
string my_id;
public void SetID(string id)
{
my_id = id;
}
public string GetID()
{
return my_id;
}
}
You could do something like this:
Child form
public string YourText { get; set; }
public TestForm()
{
InitializeComponent();
}
public void UpdateValues()
{
someLabel.Text = YourText;
}
Initiate it
var child = new TestForm {YourText = someTextBox.Text};
child.UpdateValues();
child.ShowDialog();
With this approach you don't have to change the Constructor, you could also add another constructor.
The reason for them being empty is that the properties are set after the constructor, you could Also do someting like this to add a bit of logic to your getters and setters, However, I would consider not affecting UI on properties!
private string _yourText = string.Empty;
public string YourText
{
get
{
return _yourText;
}
set
{
_yourText = value;
UpdateValues();
}
}
In this case, the UI will be updated automaticly when you set the property.
You can use a static variable/method to hold/pass the value of a control (when it gets changed).
You can use form reference or control reference to get and pass values directly.
You can use custom event for that (notifying the code that subscribed).
btw. FormB myForm = new FromB(label.Text); did not work because you are passing by value and the value was empty at the moment of creation of FormB.
FormB myForm = new FromB(label); would have worked.
Well one approach to take is to create a singleton class in your application. When you form b loads or the label changes you update the singleton with the value. Then when form a needs the value it can just get the instance of the singleton within your application and it will have that value.
There are probably cleaner ways to do it but just thinking of an easy way to pass information back and forth and store any information needed for both forms.
EDIT: Here is an example of a singleton that I pulled from here:
http://www.yoda.arachsys.com/csharp/singleton.html
public sealed class Singleton
{
static readonly Singleton instance=new Singleton();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Singleton()
{
}
Singleton()
{
}
public static Singleton Instance
{
get
{
return instance;
}
}
}
Now all you need to do is put this class in a namespace that is accessible to both forms and then you can call the Instance property of this class and then reference your values. You can add properties to it as well for whatever you want to share. When you want to retrieve those values you would call it like this:
Singleton.Instance.YourProperty
((Form2)Application.OpenForms["Form2"]).textBox1.Text = "My Message";
declare public property varible in second form
Public property somevariable as sometype
and access it in first form using instance
Dim obj as New form2()
obj .somevariable ="value"
I don't want to create an object because it won't affect my visible form. How can I call a method so it does its thing in the visible side of things.
public class foo
{
public void SetString(string foo)
{
label1.Text = foo;
}
}
Inside another class:
foo X = new foo();
X.SetString("testlololol");
This will set the label, but VIRTUALLY, I won't be able to see it on my form.
How can I do the same thing, but on my VISIBLE side of things?
When you create your visible form store a references to it in some static property. Then other classes can use that property to run public methods of that class.
// the original form
class MyForm()
{
// form public method
public void MyMethod() { ... }
}
// class storing the reference to a form
class MyOtherClass
{
public static Form MyForm;
public void ShowForm()
{
MyForm = new MyForm();
MyForm.Show();
}
}
// invoke form public method in this class
class YetAnotherClass
{
public void SomeMethod ()
{
MyOtherClass.MyForm.MyMethod();
}
}
You need to somehow get the instance which is visible. Work out some information path from things that already know about your form (or whatever it is) to your other code. Consider what would happen if there were two visible forms - which one would you want? That should suggest a way forward. If you know for a fact that there'll only ever be one visible instance, you could use a singleton - but I'd strongly suggest that you don't.
Bear in mind that you may not need to know of it by its full type name - if this is crossing layers, you may want to work out some interface including the action in some abstract way.
I would usually either pass a reference of my form ('foo' in this case) to the other class. Or I would store off a copy of 'foo' to some static location. If you know that there will only ever be 1 instance of 'foo' you could do something like:
public class foo
{
public static foo Current { get; private set; }
public foo()
{
foo.Current = this;
}
public void SetString(string foo)
{
label1.Text = foo;
}
}
...and...
foo.Current.SetString("testlololol");
Though thats a bit hacky IMO, and doesnt support multiple instances of 'foo'.
Your second class needs to have a reference to the instance of the class that IS visible.
public class OtherClass{
foo myFoo;
public OtherClass( foo visibleFoo )
{
myFoo = visibleFoo;
}
public void method()
{
myFoo.SetString("testlolol");
}
}
How can I make a textbox in my winforms application that accepts new lines of text from anywhere in the application?
I have a main form that contains a textbox. I'd like to directly add text to the box from a method in another class.
Update
I tried this in my main form:
public void Output(String value)
{
if (txtOutput.Text.Length > 0)
{
txtOutput.AppendText(Environment.NewLine);
}
txtOutput.AppendText(value);
}
But I can't call Output from the other class. I'm new to C#, so perhaps I'm missing something obvious.
Regards, Miel.
PS Yes, I know this is bad design, but for now this seems to be the best way to do what I want. The textbox would function like a console.
You'll need to expose the Text property of the TextBox as a string property on your form. For example...
public string TextBoxText
{
get { return textBoxName.Text; }
set { textBoxName.Text = value; }
}
Edit
After reading the question edit, your problem is that you need a reference to a specific instance of the form whereever you're trying to execute that code. You can either pass around a reference (which is the better option), or you could use some smelly code and have a static property that refers to one instance of your form. Something like...
public partial class MyForm : Form
{
private static MyForm instance;
public static MyForm Instance
{
get { return instance; }
}
public MyForm() : base()
{
InitializeComponent();
// ....
instance = this;
}
}
Using this approach, you could call MyForm.Instance.Output("test");
In order to decouple a bit more you could inverse the control a bit:
// interface for exposing append method
public interface IAppend
{
void AppendText(string text);
}
// some class that can use the IAppend interface
public class SomeOtherClass
{
private IAppend _appendTarget = null;
public SomeOtherClass(IAppend appendTarget)
{
_appendTarget = appendTarget;
}
private void AppendText(string text)
{
if (_appendTarget != null)
{
_appendTarget.AppendText(text);
}
}
public void MethodThatWillWantToAppendText()
{
// do some stuff
this.AppendText("I will add this.");
}
}
// implementation of IAppend in the form
void IAppend.AppendText(string text)
{
textBox1.AppendText(text);
}
It looks like your design is a little bit corrupted. You shouldn't let buisness logic mess with GUI controls. Why don't you try a return value and assigning it on the interface side?
This is a REALLY bad way of doing it, but just to make sure all the answers are out there...
In the VS designer, each form control has an item in the Properties window named Modifiers that defaults to Private. Changing this to one of the others settings, such as Internal or Public, will let you access it from outside the form.
I must stress that this is the worst way to do it.