Calling functions and methods in a child form
-
I am relatively new to C# windows programming and have lots to learn. I want to call a function in a child window from the parent window in an MDI application. If I make the function STATIC the parent form can see it, however a control(textbox)in the child function is not recognized, 'MainWindow.FileForm.textBox1' denotes a 'field' where a 'class' was expected'. If I make the function non-static I cannot call it from the parent form. How do I accomplish the call? Thank you..
-
I am relatively new to C# windows programming and have lots to learn. I want to call a function in a child window from the parent window in an MDI application. If I make the function STATIC the parent form can see it, however a control(textbox)in the child function is not recognized, 'MainWindow.FileForm.textBox1' denotes a 'field' where a 'class' was expected'. If I make the function non-static I cannot call it from the parent form. How do I accomplish the call? Thank you..
You do indeed have lots to learn, and this bit is fundamental. When you declare a class, it does not exist yet:
public class Car
{
public static int GetWheels()
{
return 4;
}
public int GetFuel()
{
return fuel;
}
}You can happily count the wheels, because that is common to all cars - they all have the same number of wheels.
...
Console.WriteLine("All cars have {0} wheels", Car.GetWheels());
...But if you try to get the fuel, the compiler complains that you "need an instance". What it is saying is that a car could be diesel, petrol, or electric. GetFuel() cannot be static, because it is not shared by all cars. You can however refer to instances of cars:
...
Car myCar = new Car("Mitsubishi", "Diesel");
Car yourCar = new Car("Ford", "Petrol");
Car hisCar = new Car("Toyota", "Hybrid");
...and then it is fine to refer to the specific car and it's fuel type:
Console.WriteLine("Your car is {0}", yourCar.GetFuel());
Console.WriteLine("My car is {0}", myCar.GetFuel());
Console.WriteLine("His car has {0} wheels", hisCar.GetWheels());Does that make sense?
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
I am relatively new to C# windows programming and have lots to learn. I want to call a function in a child window from the parent window in an MDI application. If I make the function STATIC the parent form can see it, however a control(textbox)in the child function is not recognized, 'MainWindow.FileForm.textBox1' denotes a 'field' where a 'class' was expected'. If I make the function non-static I cannot call it from the parent form. How do I accomplish the call? Thank you..
I like OriginalGriff's analogy! Just in case it's not clear, and without the car thing... Somewhere in your parent you are creating an instance of FileForm. Maybe something like this:
FileForm fileForm = new FileForm();
The instance
fileForm
is what you need to access that instance. If you need to, you can use the Parent's MdiChildren property to iterate over all child windows and find the instance you need.foreach (Form form in MdiChildren)
{
if (form is FileForm)
{
FileForm fileForm = form as FileForm;
fileForm.YourMethod();
break;
}
}I notice you are trying to access
FileForm.textBox1
. Control instances in a form are by default added as private and should stay that way. If you want to do something with a control, add a method or property in the FileForm class and use that to manipulate the control.public void SetTextBox1(string text)
{
textBox1.Text = text;
}fileForm.SetTextBox1("Hello World!");
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
I like OriginalGriff's analogy! Just in case it's not clear, and without the car thing... Somewhere in your parent you are creating an instance of FileForm. Maybe something like this:
FileForm fileForm = new FileForm();
The instance
fileForm
is what you need to access that instance. If you need to, you can use the Parent's MdiChildren property to iterate over all child windows and find the instance you need.foreach (Form form in MdiChildren)
{
if (form is FileForm)
{
FileForm fileForm = form as FileForm;
fileForm.YourMethod();
break;
}
}I notice you are trying to access
FileForm.textBox1
. Control instances in a form are by default added as private and should stay that way. If you want to do something with a control, add a method or property in the FileForm class and use that to manipulate the control.public void SetTextBox1(string text)
{
textBox1.Text = text;
}fileForm.SetTextBox1("Hello World!");
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)Thank for taking the time to answer. Your suggestions worked! I mistakingly thought that instantiating the FileForm in the parent form would make FileForm labeled "FileWindow" visible. FileForm FileWindow=new FileForm(); FileWindow.MdiParent=this; FileWindow.Show(); As I'm an engineer an not a professional programmer I will never learn everything necessary to be competent. Just enough to solve a problem. If there are any books or references that you may know of that focus on applications and you have the time would you please respond. Regards, Bill C.
-
I am relatively new to C# windows programming and have lots to learn. I want to call a function in a child window from the parent window in an MDI application. If I make the function STATIC the parent form can see it, however a control(textbox)in the child function is not recognized, 'MainWindow.FileForm.textBox1' denotes a 'field' where a 'class' was expected'. If I make the function non-static I cannot call it from the parent form. How do I accomplish the call? Thank you..
i don know what kind of design you have in mind for your application :doh: you can also use usercontrols like pages create a single form that you fill with the usercontrols designed like page, then bring to front the page you want. You can do this insead of creating child forms, if you don´t want to open new windows and run all the show in the same window you have to manage delegates here
nelsonpaixao@yahoo.com.br trying to help & get help
modified on Thursday, August 6, 2009 8:05 PM
-
Thank for taking the time to answer. Your suggestions worked! I mistakingly thought that instantiating the FileForm in the parent form would make FileForm labeled "FileWindow" visible. FileForm FileWindow=new FileForm(); FileWindow.MdiParent=this; FileWindow.Show(); As I'm an engineer an not a professional programmer I will never learn everything necessary to be competent. Just enough to solve a problem. If there are any books or references that you may know of that focus on applications and you have the time would you please respond. Regards, Bill C.
alias bill wrote:
I will never learn everything necessary to be competent
Don't sell yourself short! C# isn't *that* hard. I don't know everything by a long way and still ask questions here myself from time to time, but I consider myself competent. I don't have any C# books, so I can't recommend any personally. All my knowledge has been gained from studying articles/blogs/MSDN - and with the help of CodeProject, I've learned enough to know when what I am reading is rubbish or is good stuff. Just keep at it, and with an internet connection you have all you need, though a good book will help if you can find any recommendations.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)