Stack Overflow when trying to access form controls from class - c#

I have a problem, after adding this code so I can access my MainWindow controls in Downloader class:
public partial class MainWindow : Form
{
private Downloader fileDownloader;
public MainWindow()
{
InitializeComponent();
fileDownloader = new Downloader(this);
}
//smth
}
and
class Downloader : MainWindow
{
private MainWindow _controlsRef;
public Downloader(MainWindow _controlsRef)
{
this._controlsRef = _controlsRef;
}
// smth
}
it now gives me "An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll" on line
this.mainControlPanel.ResumeLayout(false);
in MainWindow.Designer.cs. If i comment out the code above, it works fine. Any ideas please?
PS. Also, when I'm in Downloader class, should i access the controls like
textbox.Text
or
_controlsRef.textbox.Text
Both seem to give no compile errors, is there any difference between the two?

Your Downloader class inherits MainWindow. When you instansiate it, according to the C# specification, the base class is initialized first. When MainWindow initializes, it creates a new instance of Downloader, which causes it eventually to stackoverflow, because you're in an endless cyclic dependency.
Since Downloader inherits MainWindow, there is no point in getting an instance of it via your constructor. You can simply access it's protected and public members from your derived.
For example:
public partial class MainWindow : Form
{
protected string Bar { get; set; }
public MainWindow()
{
Bar = "bar";
InitializeComponent();
}
}
public class Downloader : MainWindow
{
public void Foo()
{
// Access bar:
Console.WriteLine(Bar);
}
}

It's problem because here you are having cyclic dependancy i.e. both the method waiting for each other to complete.
check
public MainWindow()
{
InitializeComponent();
fileDownloader = new Downloader(this);//this wait to complete downloader intialization
}
public Downloader(MainWindow _controlsRef)//this wait to complete mainwindow first
{
this._controlsRef = _controlsRef;
}
my mean to say as you are inheriting Downloader from MainWindow , which in constructor crating downloader instace...and downloader calls Mainwindow constructor first as its parent crates problem for you here
Solution
if want reference you can do like this
public partial class MainWindow : Form
{
protected MainWindow mainWindow;
public MainWindow()
{
InitializeComponent();
mainWindow = this;
}
//smth
}
class Downloader : MainWindow
{
public Downloader()
{
//this.mainWindow //will give you reference to main winsow
}
// smth
}

Related

Secondwindow can't acces Textboxes of Mainwindow

So i have two Windows.
The first window stores all the data with textboxes, comboboxes etc.
In the second window i want to the user to enter some Information and based on the Information i want to Change something in the mainwindow.
I changed
public partial class Window2 : Window
to
public partial class Window2 : MainWindow
but it still does not work.
public partial class MainWindow : Window
{
int Languagetoken = 1;
public MainWindow()
{
InitializeComponent();
DateTextBox.Text = DateTime.Now.ToShortDateString();
}
...
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
{
Do not know how to do it. The code above is not in the same xaml they just Show the initialization of both.
There are several ways to solve this but the simplest one in your case is probably to inject Window2 with a reference to the MainWindow when you create it:
Window2 win = new Window2(this);
Window2:
public partial class Window2 : Window
{
private readonly MainWindow _mainWindow;
public Window2(MainWindow mainWindow)
{
_mainWindow = mainWindow;
InitializeComponent();
}
...
}
You could then access any internal or public member of the MainWindow in Window2 using the _mainWindow reference, e.g.:
_mainWindow.textBlock1.Text = "...";
By default text boxes and all other controls are represented by private member variables int he class representing your form so you won't be able to reach into it.
In order access it from outside, you can add a property inside the first window...
public string DataText
{
get
{
return DataTextBox.Text;
}
set
{
DataTextBox.Text = value;
}
}
Or you can change the access level of the controls to internal or public so that you can access it by using a reference to the window.

C# Call method from another class

I've tried the bellow code which is working but it keeps opening multiple forms every time i call calltest() method. How can i call test() method without opening it's form? Should i move all my methods to another class file? Thanks
namespace test1
{
public partial class MainForm : Form
{
public void test()
{
//code
}
...
}
}
namespace test1
{
public partial class SecondForm : Form
{
private void calltest()
{
MainForm form1 = new MainForm();
form1.test();
}
...
}
}
You might want to reconsider your design. However, here is a direct answer to your question:
The issue you are having is because each time in the calltest method, you are creating a new instance of MainForm.
Instead, you should inject the original instance of MainForm and store it inside an instance variable inside SecondForm like this:
public partial class SecondForm : Form
{
MainForm form1 = null;
public SecondForm(MainForm main_form)
{
form1 = main_form;
}
private void calltest()
{
form1.test();
}
...
}
Now when you create your instance of SecondForm, make sure that you inject the original instance of MainForm with something like this:
SecondForm second_form = new SecondForm(main_form); //If you are creating this from MainForm, then simply pass `this` as the construction parameter

Access Application.MainWindow content in usercontrol

In WPF application it is easy to access MainWindow content but how to do in usercontrol.
In WPF Application, if my MainWindow name is Window1 and i have to access its one of the string variable named CName from some other class in same application.
Window1 ww = Application.Current.MainWindow as Window1;
String reqstring = ww.CName ;
I want to do same thing in usecontrol too.
Assume my usercontrol name is ABCcontrol and first/main Class name is ABCconrolLib
namespace ABCcontrol
{
public partial class ABCcontrolLib : UserControl
{
Public String CName = "ABC";
//....
}
}
Now i want to access this CName in some other class of same usercontrol by using Application.Control.MainWindow or by some other similar way
Please help me out as this is my first UserControl in WPF
You can access Application.Current.MainWindow because you have just one instance of MainWindow in your whole application so they defined it as a static property.
If you have the same characteristic for your users control (just you need to use a single instance of it in the whole application) you can define a static property there as well.
Something like :
then you can access name property like UserControl1.Instance.CName
public partial class UserControl1 : UserControl
{
public static UserControl1 Instance
{
get;
private set;
}
Public String CName = "ABC";
public UserControl1()
{
if (Instance != null)// there should be just one instance
throw new NotSupportedException();
Instance = this;
InitializeComponent();
}
}
if you get error then you do not have just one instance of your usercontrol, you have to define a List and register each instance of your users controls there. Something like:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
(App.Current as App).UserControl1List.Add(this);
InitializeComponent();
}
}
public partial class App : Application
{
public App()
{
UserControl1List = new List<UserControl1>();
}
public List<UserControl1> UserControl1List
{
get;
private set;
}
}
then you can reach to each instance like (App.Current as App).UserControl1List[0]...
or you can use a dictionary for storing each UserControl there.

How to call my public class to my main form?

I have this class ClassMainForm and a form named MainForm. I made a method in my class, then inside of that is my codes like quantity1.Show. My question is, how do I call my function from class to my main form? I'd appreciate all your help.
ClassMainForm :
public void Visible()
{
GroupInstruction.Hide(); // <<== how do i call my controls in my MainForm?
quantity1.Show(); // <<== how do i call my controls in my MainForm?
}
Thanks Guys...
First create a new instance of your class:
ClassMainForm cmf = new ClassMainForm();
After this you can use cmf.NAMEOFYOURFUNCTION to call your function.
NAMEOFYOURFUNCTION = One of the methods/functions in your class.
With the cmf. u refer to your class, and after the dot u select the name of your method/function.
EDIT:
Placed a numerupdown on the form, and I called it numerUpDown1.
MAINFORM:
namespace Stack_Overflow_2
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
public NumericUpDown numericupdown()
{
return numericUpDown1;
}
}
}
CLASS:
namespace Stack_Overflow_2
{
class ClassMainForm
{
MainForm mf = new MainForm();
public void Visible()
{
mf.numericupdown().Show();
}
}
}

Accessing mainwindow.xaml.cs methods from another class

hi i am relatively new at Wpf application
i want to know that can i create a object of mainwindow.xaml.cs
in some other class
And then using that object excess the user defined methods in mainwindow.xaml.cs
i am trying to something like this
it creates the object of mainwindow but can't access the method
the method which i am trying to access is public defined
MainWindow window = new Mainwindow();
window.point_to_screen();
it gives this error
Error 2 The type name 'point_to_screen' does not exist in the type 'System.Windows.Window'
You should cast the current mainwindow to a MainWindow object.
If you create a new window you won't be able to acces your current open window
MainWindow wnd = (MainWindow)Application.Current.MainWindow;
wnd.point_to_screen();
Make sure your method be in public protection level, and check if you use an instance method (not static) :
public class MainWindow
{
...
public void point_to_screen()
{
...
}
}
and use it:
MainWindow window = new Mainwindow();
window.point_to_screen();
if you use a static method your code should be like this:
public class MainWindow
{
...
public static void point_to_screen()
{
...
}
}
and use it:
MainWindow.point_to_screen();

Categories