define variable internal for get him in child window wpf-c# - c#

i have in the main window variable
example:
public partial class MainWindow : Window
{
internal int i;
public MainWindow()
{
InitializeComponent();
}
}
and i want to use him in child window and for that i do him internal (the two window in the same namespace) and the child window still doesn't recognize the variable
what i'm suppose to do?

You should create a public property on the child window of type int. When you create the child window then set that property based on the value of the field in the parent window.
It appears that you want to be able to not just read the variable in the child class, but also modify it and have that change reflected in the parent form, so that will complicate the answer.
We'll need to start off with a helper class. Because the data we're interested in is an int (which is a value type) we'll need something that is a reference type (i.e. a class).
public class Wrapper<T>
{
public T Value { get; set; }
}
So we'll start by not having an integer in the parent form, but instead an instance of this class:
public class Form1
{
private Wrapper<int> data = new Wrapper<int>(); //TODO give better name
//...
}
Next we'll need a public property on the child form; rather than an int we'll use this new class:
public class Form2
{
public Wrapper<int> Data { get; set; }
//...
}
Now when we create the child class we'll set the property based on the value in the parent class:
public void someMethod()
{
Form2 childForm = new Form2();
childForm.Data = data;
childForm.Show();
}
Now that we've done all this we've ensure that both the parent and child class have a reference to the same instance of Wrapper, so any changes to the Value property of the Wrapper instance (from either class) will be "seen" by either reference.

Related

Keyword 'this' is not available when passing it to child form

I have three forms, where one of them is the parent and the other two are the children. The reason I'm doing this is so that the parent form can reference the children, and vice versa (I actually ran into a infinite recursion error before doing this, but all gone).
I have written the code as below:
public partial class PerfilAcesso : Form
{
// this is the parent
BDE bdeForm = new BDE(this); //error line
Workshop workshopForm = new Workshop(this); //error line
// rest of the info
}
public partial class Workshop : Form
{
// this is one child
PerfilAcesso perfilAcesso;
public Workshop(PerfilAcesso parent)
{
InitializeComponent();
perfilAcesso = parent;
}
}
public partial class BDE : Form
{
// this is another child
PerfilAcesso perfilAcesso;
public BDE(PerfilAcesso parent)
{
InitializeComponent();
perfilAcesso = parent;
}
}
However, it won't compile, because it gives the following error
Keyword 'this' is not available in the current context
at lines 4 and 5, where I pointed out.
I tried setting the property IsMdiContainer in the parent form to true, but it didn't work.
Could someone give me any directions as to what I'm doing wrong? I've gone through questions about creating a parent/child form, and they all show the same.
this is not available in field initializations. You will need to move the initialization to a constructor if you need to use this:
public partial class PerfilAcesso : Form
{
public PerfilAcesso ()
{
bdeForm = new BDE(this);
workshopForm = new Workshop(this);
}
BDE bdeForm;
Workshop workshopForm;
}

Accessing object created in different Window in WPF application

I have created one window and declared 2 instances of my object, then I modified them and wanted to pass to another Window. My questions are:
How would i do that ?
(I can pass simple types such as string or int trough window constructor but passing my own object giving me an error (Inconsistent Accessibility parameter order is less accessible then method))
Does it have any connection with dataContext ?
Can anybody explain to me how I can achieve that (in the simplest possible way)? What are the correct ways to do that ?
Here is part of my code (everything is in one namespace):
public partial class Main_window : Window
{
Order myOrder = new Order();
Menu menu = new Menu();
public Main_window()
{ InitializeComponent() }
private void OpenSecondWindow(object sender, RoutedEventArgs e)
{
Second_Window SecondWindow = new Second_Window();
Second.ShowDialog();
}
}
// Second Window class
public partial class Second_Window : Window
{
public Second_Window(Order someOrder)
{ InitializeComponent(); }
}
Make sure that the Order type, and any other type you intend to inject the SecondWindow with, is defined as a public class:
public class Order { ... }
A non-public type cannot be part of the signature of a public method or constructor.

Static referenced in another .cs file

I am overlooking something simple I think. I have a form with a checkbox. I need to know if the checkbox is checked in a different cs file/class to know whether to make a column header Option1 or Option2.
Form1 (Public partial class) code:
public bool Checked
{
get
{
return checkBox1.Checked;
}
}
In my Export1 class I have private void CreateCell1 that takes in the data to be exported (creating an excel file from a datatable). The section of code I can't get to work is:
if (Form1.Checked.Equals("true"))
{
newRow["Option1"] = date2;
}
else
{
newRow["Option2"] = date2;
}
I am getting -Error 1 An object reference is required for the non-static field, method, or property 'Matrix1.Form1.Checked.get'
What did I overlook?
Well, the problem here is exactly what the compiler is telling you. You need an object reference in order to access the property.
Allow me to explain.
In C#, by default, class members (fields, methods, properties, etc) are instance members. This means that they are tied to the instance of the class they are a part of. This enables behavior like the following:
public class Dog
{
public int Age { get; set; }
}
public class Program
{
public static void Main()
{
var dog1 = new Dog { Age: 3 };
var dog2 = new Dog { Age: 5 };
}
}
The two instances of Dog both have the property Age, however the value is tied to that instance of Dog, meaning that they can be different for each one.
In C#, as with a lot of other languages, there are things called static members of classes. When a class member is declared static, then that member is no longer tied to an instance of the class it is a part of. This means that I can do something like the following:
public class Foo
{
public static string bar = "bar";
}
public class Program
{
public static void Main()
{
Console.WriteLine(Foo.bar);
}
}
The bar field of the Foo class is declared static. This means that it is the same for all instances of Foo. In fact, we don't even have to initialize a new instance of Foo to access it.
The problem you are facing here is that, while Form1 is not a static class and Checked is not a static property, you are treating it as such. In order for what you are trying to do to work, you need to create an instance of Form1 and access that instance's Checked property.
Depending on how your program is structured, there are many ways of doing this. If Form1 is created in the scope where you are trying to access Checked, then this will be straightforward. If Form1 is what spawns the new scope, then common practice is to pass a reference to it in the constructor.
For example, if Form1 creates a new Form2 then we do the following:
public class Form2 : Form
{
private Form1 parent;
public Form2(Form1 parent)
{
this.parent = parent;
InitializeComponent();
}
}
And then you can access parent throughout Form2. Of course, depending on the structure of your program, the exact implementation will be different. However, the general pattern is the same. Pass the reference to Form1, from the scope it was created in, to the new class, and then access it from there.
One way or another, you need to access the specific instance of Form1 that you're trying to check.
A few ways to do this are:
Pass the instance to the class constructor
Setting a public property of the other class to the instance of the form
Pass the instance to the method directly
For example:
public class SomeOtherClass
{
// One option is to have a public property that can be set
public Form1 FormInstance { get; set; }
// Another option is to have it set in a constructor
public SomeOtherClass(Form1 form1)
{
this.FormInstance = form1;
}
// A third option would be to pass it directly to the method
public void AMethodThatChecksForm1(Form1 form1)
{
if (form1 != null && form1.Checked)
{
// Do something if the checkbox is checked
}
}
// This method uses the local instance of the Form1
// that was either set directly or from the constructor
public void AMethodThatChecksForm1()
{
AMethodThatChecksForm1(this.FormInstance);
}
}
This class would need to be instantiated by the instance form1 using one of these methods:
// Pass the instance through the constructor
var someOtherClass = new SomeOtherClass(this);
// Or set the value of a property to this instance
someOtherClass.FormInstance = this;
// Or pass this instance to a method of the class
someOtherClass.AMethodThatChecksForm1(this);

how to edit the primary form using a form that was created from the primary form in C#

I want to edit a primary form(Form1) using a form that is created from its code(Form2). The way I have done it is that, when the event is triggered, the new form appears, and everything works, but I want it so that when a button is pressed, the original form is edited. The code I have tried is this:
Form1 form1 = new Form1();
private void button1_Click(object sender, EventArgs e)
{
form1.startNewGame();
this.Hide();
}
By the way, I realise that this is creating a new instance of the form, I want to know how to edit the already existing instance of the form.
Without having the code of Form2 is hard to help you, but I will try. Instead of creating an instance of Form1, add a public property to form2
public Form1 PrimaryForm{get;set;} and when you showin form2 set that property to this
A solution would be to pass a reference of your Main Form to the child form when the child form is initialized. You can then set the value of the mainform through the reference. See below for an example:
public class ChildForm1 : Form
{
// Fields
private MainForm _mainForm;
private bool _value1;
private bool _value2;
// Default constructor
public ChildForm1()
{
}
// Overloaded constructor that accepts a container of values
public ChildForm1(ValuesContainer values, MainForm mainForm)
{
_mainForm = mainForm;
_value1 = values.Value1;
_value2 = values.Value2;
//Set a main form value
_mainForm.Value = "This value was changed by ChildForm1."
}
}
public class ChildForm2 : Form
{
// Field
private bool _value3;
// Default constructor
public ChildForm2()
{
}
// Overloaded constructor that accepts a container of values
public ChildForm2(ValuesContainer values)
{
_value3 = values.Value3;
}
}
public class MainForm : Form
{
public string Value { get; set; }
// Default constructor
public MainForm()
{
}
// Simulated - Event method called when button is clicked for child form 1
public void CallChildForm1()
{
ValuesContainer values = new ValuesContainer();
// Set the values from the main form
values.Value1 = true;
values.Value2 = true;
// Call the child form while passing in the container of values that we just populated.
ChildForm1 childForm = new ChildForm1(values);
childForm1.Show();
}
// Simulated - Event method called when button is clicked for child form 2
public void CallChildForm2()
{
ValuesContainer values = new ValuesContainer();
// Set the value from the main form
values.Value3 = true;
// Call the child form while passing in the container of values that we just populated.
ChildForm2 childForm = new ChildForm2(values);
childForm2.Show();
}
}
// Simple data object or container used to transfer information between complex objects such as forms and controls.
// These are also known as data classes or data transfer objects (DTOs)
public class ValuesContainer
{
public bool Value1 { get; set; }
public bool Value2 { get; set; }
public bool Value3 { get; set; }
}
This might not be the best practice but is the simplest way to do it. Here are the steps;
1) in Form1 add a public method to edit whatever you want
2) in Form2 add a reference to type Form1
3) instantiate Form2 and set InstanceOfForm2.ReferenceToForm1 = this;
4) in the event handler of Form2 use the reference to call your public method like
//inside some event handler
this.ReferenceToForm1.MyPublicMethodThatEditsTheDisplay();
One thing to remember is that Form1 is just an object like any other. The only thing preventing you from editing it anywhere you choose is the access level of it's properties (can't touch private fields, obviously) and the lack of reference. Everything beyond that is a matter of opinion and 'best practice' if you want to edit from anywhere, make the field public/provide a public method and pass/set a reference.

C# Component Container

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!

Categories

Resources