13 Replies - 9311 Views - Last Post: 11 July 2011 - 12:10 PM Rate Topic: -----

#1 phpa80  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 04-July 11

access to form1 textbox from form2 textbox ?

Posted 10 July 2011 - 03:06 PM

hi there.
i want to know how can i access to form 1 text box from form2 text box ?
for example when i write some characters in text box on form 2 instantly same characters wrote in form1 text box.
i want to access directly . i mean without creating instant from form1 in form2.
Is This A Good Question/Topic? 0
  • +

Replies To: access to form1 textbox from form2 textbox ?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: access to form1 textbox from form2 textbox ?

Posted 10 July 2011 - 04:39 PM

People spend a lot of time writing up tutorials for frequently asked questions. You should read that and appreciate the effort that goes into their work.
Was This Post Helpful? 3
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: access to form1 textbox from form2 textbox ?

Posted 10 July 2011 - 07:09 PM

The tutorials below walk through making an application including inheritance, custom events and custom controls, object serialization and more.
Bulding an application - Part 1
Building an application - Part 2
Quick and easy custom events
Separating data from GUI - PLUS - serializing the data to XML
Passing values between forms/classes
Was This Post Helpful? 1
  • +
  • -

#4 phpa80  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 04-July 11

Re: access to form1 textbox from form2 textbox ?

Posted 11 July 2011 - 01:37 AM

View PosttlhIn`toq, on 10 July 2011 - 07:09 PM, said:

The tutorials below walk through making an application including inheritance, custom events and custom controls, object serialization and more.
Bulding an application - Part 1
Building an application - Part 2
Quick and easy custom events
Separating data from GUI - PLUS - serializing the data to XML
Passing values between forms/classes


can you give me straight answer ?
Was This Post Helpful? -4
  • +
  • -

#5 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: access to form1 textbox from form2 textbox ?

Posted 11 July 2011 - 02:05 AM

Make a static object of your form2 in in your form2
// Create a static object for Form2
public static Form2 frm2 = new Form2();

// Other codes ....
private void Form2_Load(object sender, EventArgs e)
//...



Set your TextBox Modifiers to Public and you can use it in form one using frm2 object

MessageBox.Show(Form2.frm2.textBox1.Text);


This post has been edited by noorahmad: 11 July 2011 - 02:09 AM

Was This Post Helpful? -1
  • +
  • -

#6 phpa80  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 04-July 11

Re: access to form1 textbox from form2 textbox ?

Posted 11 July 2011 - 02:38 AM

View Postnoorahmad, on 11 July 2011 - 02:05 AM, said:

Make a static object of your form2 in in your form2
// Create a static object for Form2
public static Form2 frm2 = new Form2();

// Other codes ....
private void Form2_Load(object sender, EventArgs e)
//...



Set your TextBox Modifiers to Public and you can use it in form one using frm2 object

MessageBox.Show(Form2.frm2.textBox1.Text);


recently an experienced c# developer wrote for me 1line code that i cannot remember it now .According to his statement he does it by Casting.can anyone knows this method ?
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: access to form1 textbox from form2 textbox ?

Posted 11 July 2011 - 03:09 AM

That to me, using a Cast, suggests to me that really need the job.

Don't think about form as something magical it still just a class.
How do relate one class to another class? Create a reference.
Was This Post Helpful? 0
  • +
  • -

#8 Milto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 139
  • Joined: 10-February 11

Re: access to form1 textbox from form2 textbox ?

Posted 11 July 2011 - 06:18 AM

Create the reference in form1.
TextBox textbox1 = new TextBox(); 
//similar to what noorahmad said except its only referencing the textbox itself



then in form 2 you can simply reference it though
Form1.textBox1... //then do whatever you need to do



Hope this helped
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: access to form1 textbox from form2 textbox ?

Posted 11 July 2011 - 06:25 AM

It sounds to me like the OP wants something a little different than what we expect:

Quote

for example when i write some characters in text box on form 2 instantly same characters wrote in form1 text box.


so above and beyond what was stated above, it seems that form 1 would need to subscribe to the appropriate key events from the textbox in form 2.
Was This Post Helpful? 1
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4468
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: access to form1 textbox from form2 textbox ?

Posted 11 July 2011 - 06:53 AM

View Postnoorahmad, on 11 July 2011 - 04:05 AM, said:

Make a static object of your form2 in in your form2
// Create a static object for Form2
public static Form2 frm2 = new Form2();

// Other codes ....
private void Form2_Load(object sender, EventArgs e)
//...



Set your TextBox Modifiers to Public and you can use it in form one using frm2 object

MessageBox.Show(Form2.frm2.textBox1.Text);



Terrible, terrible advice. This is the way to produce garbage code, with one form affecting another form's GUI controls directly. It leads to unmaintainable code, since you never know where a change is actually coming from.

There's a reason I wrote the tutorial Jack linked in the first post.
Was This Post Helpful? 2
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: access to form1 textbox from form2 textbox ?

Posted 11 July 2011 - 07:50 AM

View Postphpa80, on 11 July 2011 - 02:37 AM, said:

View PosttlhIn`toq, on 10 July 2011 - 07:09 PM, said:

The tutorials below walk through making an application including inheritance, custom events and custom controls, object serialization and more.
Bulding an application - Part 1
Building an application - Part 2
Quick and easy custom events
Separating data from GUI - PLUS - serializing the data to XML
Passing values between forms/classes

can you give me straight answer ?


What could be any more straight of an answer than tutorials where an experienced coder has invested several hours in creating examples, clear screen shots, and a sample project?

If by "straight answer" you were expecting someone to write your code for you, then you came to the wrong site. The people here will spend tons of time helping you learn the craft of coding, but won't waste a minute helping you become a copy/paste monkey.

Curtis is completely write about NOT making your GUI controls public and changing them from outside of their own form. For that matter I don't even modify my GUI elements from within their own form except in one AND ONLY ONE property for them. For example

bad coding:
void SomeMethod()
{
textbox1.Text = newFirstname;
}

void SecondMethod()
{
   // Add salutation
   textbox1.Text = "Mr. " + textbox1.Text;
}

void ThirdMethod()
{
   // Strip out salutation
   textbox1.Text = textbox1.Text.Split(' ')[1];
}



Better habits:
string _firstname = string.empty;
public string FirstName
{   
   get { return _firstname; }
   set
      {
         _firstname = value;
         textboxFirstName.Text = value;
      }
}

void SomeMethod()
{
    FirstName = newFirstname;
}
// blah blah



Do you see how even within the form you don't make dozens of direct references to the textbox?
This way if you change the name of the textbox, or change the logic of the application there is only one place that you have to update: The FirstName property. If you want to validate the name, you only add the validation code to the property. If you change the name of textbox you don't have to scrub through all the code for references.

I urge you to take an hour or two and actually work the tutorials that were linked in previous posts. Learning to do it write will take an hour or two. But it will save you hundreds of hours of headaches, plus make your code decent instead of "garbage code" (or "India code" as I call it) as Curtis CORRECTLY described the idea of making your GUI controls public.

Milto: Please don't ever suggest that sort of horrible coding practice again. We have fought long an hard, written many tutorials for good coding practices in an effort to help educate people into doing things the right way. We pride ourselves on the quality of our advice not looking like it came from India or a first year student. I urge you to work through those tutorials as well.
Was This Post Helpful? 3
  • +
  • -

#12 phpa80  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 04-July 11

Re: access to form1 textbox from form2 textbox ?

Posted 11 July 2011 - 11:55 AM

View PostAdamSpeight2008, on 11 July 2011 - 03:09 AM, said:

That to me, using a Cast, suggests to me that really need the job.

Don't think about form as something magical it still just a class.
How do relate one class to another class? Create a reference.


and how can i do that? do you know how can i use cast to access form 1 textbox from form2 textbox ?
Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4468
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: access to form1 textbox from form2 textbox ?

Posted 11 July 2011 - 12:05 PM

You can't. That doesn't make any sense at all.
Was This Post Helpful? 0
  • +
  • -

#14 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: access to form1 textbox from form2 textbox ?

Posted 11 July 2011 - 12:10 PM

View Postphpa80, on 11 July 2011 - 12:55 PM, said:

View PostAdamSpeight2008, on 11 July 2011 - 03:09 AM, said:

That to me, using a Cast, suggests to me that really need the job.

Don't think about form as something magical it still just a class.
How do relate one class to another class? Create a reference.


and how can i do that? do you know how can i use cast to access form 1 textbox from form2 textbox ?


You don't. Look up cast on MSDN and see what it does and you'll understand why.
It is for converting similar types.
decimal frank = 100m;
int bob = (int)frank; // Casts the decimal to an int. Possible loss from fraction

That is what a cast is.

Please stop trying to take short cuts and doing it the 'India Code' way.
Work the tutorials we linked to you and do it the right way.
Or switch jobs because you don't *want* to learn how to do it right.

This post has been edited by tlhIn`toq: 11 July 2011 - 12:12 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1