Click here to Skip to main content
15,887,336 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
using vsstudio (c#)..
given the sample code below is my form property(form1), i want to use it to my (form2).. please help.. TIA..

What I have tried:

public void frmproperty()
       {
           this.CenterToScreen();
           this.Text = "frmlogin";
           this.MaximizeBox = false;
           this.MinimizeBox = false;
       }
Posted
Updated 14-Apr-17 4:28am

This is not a Property (of course it belongs to the form) - this is a Method.
If you want to access something on Form1 from Form2 you should create a reference to Form1.

For example :http://stackoverflow.com/questions/20286176/how-to-call-function-from-another-form
 
Share this answer
 
v3
Comments
akosisugar 14-Apr-17 10:29am    
yes sir.. a method called 'frmproperty'. i call this method in form2 in form_load event.. but it didnt work..
Ralf Meier 14-Apr-17 10:31am    
See the Link I have posted an also the Links in then 2nd Solution from OG ...
akosisugar 14-Apr-17 10:36am    
private void frmmain_Load(object sender, EventArgs e)
{
Form1 f = new Form1();

f.frmproperty();
}

code in my form2
Ralf Meier 14-Apr-17 10:41am    
OK ... and now ? What happens ? Is this what you want to get ?
akosisugar 14-Apr-17 10:45am    
the form1 property did not copied in form2
frmproperty is a method, and it's public - so all you need is the form instance in order to call it. And there's the rub - how do you get the form instance? should you get the form instance? Difficult to tell, without knowing more about how your forms are related.
There are three different scenarios:
1) Form1 creates and opens the Form2 instance (Form1 is the "parent", Form2 is the "child")
2) Form2 creates and opens the Form1 instance (Form2 is the "parent", Form1 is the "child")
3) Form3 creates and opens both Form1 and Form2. (Form3 is the "parent", Form1 and Form2 are the "children")

How this should be handles depends on which scenario your software is using, because "children" should not know that "parents" even exist, let alone what class they are and thus what properties and methods they contain.
See one of these:
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
They mostly deal with properties, but it's exactly the same for methods.

Quote:
so this means my idea is impossible to work?

Basically, yes - in the form you are trying to do it, anyway.
You're trying to do something that isn't right. There are ways to do what you want:
1) Create a static method (or an extension method) for the Form class and pass it the Form instance you want to "adjust" - this will only work if the method sticks to standard Form properties and methods, it can't do anything specific to a derived class.
C#
public static class GeneralMethods
   {
   public static void Adjust(Form f)
      {
      f.CenterToScreen();
      f.Text = "frmlogin";
      f.MaximizeBox = false;
      f.MinimizeBox = false;
      }
   }
...
   GeneralMethods.Adjust(this);

2) Create a base class for all your forms that contains the method, and derive your forms from that. The base method is available directly in all derived forms without any reference to the base class.
C#
public class MyForm : From
   {
   public void Adjust()
      {
      CenterToScreen();
      Text = "frmlogin";
      MaximizeBox = false;
      MinimizeBox = false;
      }
   }
...
public class Form1 : MyForm
   {
   ...
      Adjust();
   ...
   }
public class Form2 : MyForm
   {
   ...
      Adjust();
   ...
   }
In reality, MyForm would be an abstract form, but that gives problems with the Visual studio designer, so for the moment, just leave it at that!
 
Share this answer
 
v2
Comments
akosisugar 14-Apr-17 10:33am    
is just call the method 'frmproperty' to my form2. but it didnt work..
OriginalGriff 14-Apr-17 10:50am    
Of course it didn't - it's a Form1 method, so it acts on instances of Form1, it doesn;t even know Form2 exists, much less what properties it has!
akosisugar 14-Apr-17 10:57am    
so this means my idea is impossible to work?
OriginalGriff 14-Apr-17 11:10am    
Answer updated
akosisugar 14-Apr-17 11:50am    
can i use method instead of a class?
some form properties are not working..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900