Jump to content

Editing Objects On Other Forms, and diffrent methods on calling upon Forms

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Kierien

Kierien

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Hey,

I've been having a problem with forms lately.

First of is there any other way of calling upon a form, apart from

Form1 Lawl = new Form1();

Lawl.Show()

Cause that launches a new form, which i dont want, is there a work around on this?

The other thing would be editing objects on other forms.

In vb.net you could easily just

Form1.TextBox1.Text = "Lawl"

How do you do this in C#?

Any help would be greatly appreciated.

~kierien

#2
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
I believe this should help with your second issue. The first thing you need to do is to make sure that the actual control you are trying to change properties of (in your example it would be TextBox1) is declared as public. If you are using VS and used the form designer, then look in the solution explorer under Form1 for a file called Form1.Designer.cs. If you expand all of the code blocks (press the little grey + signs on the left of the actual code) in this file you will find where the controls are declared, usually near the bottom of the file. Change the accesibility modifier on the control from private to public.

After this is done you need an actual instance of the form to modify. This can be achieved by creating a static variable to hold the original form1 instance and then using the variable to modify the instance. Here is the code for the second part:
form1:

//create static variable for holding referance to forms not being currently shown

public static Form1 staticVar = null;
the actual declaration of the static variable can be anywhere outside of a method in the class.

staticVar = this;

this.Hide();

Form2 form2 = new Form2();

form2.Show();
form2:

Form1.staticVar.TextBox1.Text = "it worked";

//close this form and open up original form

this.Close();

Form1.staticVar.Show();
This should work for you. If you have any problems or further questions post them and Ill do my best to help out. As for the first issue you have, perhaps you could explain what you dont like about the other way of calling a form and maybe I can help with a way around it.

#3
Kierien

Kierien

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Thanks for the help :)

Much appreciated,

But as for the first issue, by calling upon i meant creating a variable so the form is accessible, to close the form. But im guessing that by declaring the form as public it would be accessible. Am i wrong?

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
You have found the awful truth - that while VB works with static members, C# works with instance members. You cannot open a Form1 if no Form1 has been created in memory - that is why you use the "new" keyword to create an object with the class Form1, and then show it.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
Kierien

Kierien

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts

Xav said:

You have found the awful truth - that while VB works with static members, C# works with instance members. You cannot open a Form1 if no Form1 has been created in memory - that is why you use the "new" keyword to create an object with the class Form1, and then show it.

Even if the form already exists?

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
If the form already exists, then you must have created an instance of it somewhere. Just refer to that instance, and there you have it.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#7
imp0steur

imp0steur

    Newbie

  • Members
  • Pip
  • 8 posts
You are forgetting that WinForms are nothing but classes .. so what do you do if you want to get reference to an existing object?? store it somewhere?

Either use global variables to store form collection .. thats not a good thing to do ..

But if you want only one instance of the form per application .. use few patterns like monostatic/singleton ..

Again this is just an idea .. but the same thing can be achieved without using the patterns too