5 Replies - 452 Views - Last Post: 22 September 2013 - 03:46 AM Rate Topic: -----

#1 prasad.weer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 10-November 11

textbox value change from other form

Posted 21 September 2013 - 07:39 AM

I want to change form1 textbox1 text from from2 variable.
This is the flow.

Show form1
click button in form1
then show form2 (form1 do not hide. It is still visible)
click button in form2 (form2 has variable and it set to form1 text box and hide form2)

(In my system form1 is always show)

This is my code in form1 for access textbox from other form

 //FIRST CODE BLOCK
    public string textRoomID
    {
    get
    {
    return this.txt_RoomID.Text;
    }
    set
    {
    this.txt_RoomID.Text = value;
    }
    }


This is the code of form2 button click

 //SECOND CODE BLOCK
    form1 obj_f1 = new form1(); //create the object of
    obj_f1.textRoomID = RoomID; //Assign variable
    this.Hide();


When I debugging the code set method is working well and it is not showing any error.
But not update my text box.

if I use this flow, above code worked.

form2 show
form2 button click (SECOND CODE BLOCK execute and additionaly added form1.show();)

But my requirment is first i mensioned flow.
How to do my work? Why it is not assigning value to text box?

Is This A Good Question/Topic? 0
  • +

Replies To: textbox value change from other form

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,440
  • Joined: 23-August 08

Re: textbox value change from other form

Posted 21 September 2013 - 08:00 AM

Read this.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

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

Reputation: 5441
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: textbox value change from other form

Posted 21 September 2013 - 08:26 AM

If you did this in a modern WPF design instead of Winforms...

You would have 3 things: Window1, Window2, a class holding the information.
You would then bind the two textboxes to the same property of the data class.
Its a far more OOP design. And a lot easier than having to manage all the change events bi-directionally yourself.
Was This Post Helpful? 0
  • +
  • -

#4 prasad.weer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 10-November 11

Re: textbox value change from other form

Posted 22 September 2013 - 02:19 AM

WoW. Thanks for showing me the article JackOfAllTrades. :bananaman:
Actually I have a problem. If do this with VB.NET (This project must have do with C#. That's why I develop my project with C#) I can simply do this work with this code.
form1.textbox1.text = "Some value"

Above code takes 10 seconds or less. But I stuck with C# for 2 days for this work. It should have to make a class, some coding of forms etc... Why is C# has this much restrictions? Bit difficult in coding? :gun_bandana:
Was This Post Helpful? 0
  • +
  • -

#5 CodingSup3rnatur@l-360  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: textbox value change from other form

Posted 22 September 2013 - 03:33 AM

Quote

Why is C# has this much restrictions?


The 'feature' you are referring to is called Default Form Instances, and they are built into VB.NET. I believe the main reason this was added to VB.NET was to appease VB programmers, and make it easier for them to upgrade old VB projects to VB.NET. It's rarely a good idea to use default form instances in new code.

VB.NET has a habit of trying to be helpful, and doing a lot of things automatically behind your back. Some of those things can be very useful; some can be harmful, particularly to new programmers who don't know any better (in my opinion). For example, another issue in your VB.NET code example is that you are accessing a control on a form directly from outside the form, which is a terrible practice. You couldn't do this by default in C#, because controls have the private access modifier (it is more restrictive) by default, so you may be more inclined to follow best practice, and use properties to access the controls, as you did in your first post.

The point is though, just because something is very quick and easy to do now, doesn't mean it's automatically better than the slightly longer alternative. Things that you throw in quickly and easily now, have the tendency to come back and bite you in the future. Restrictions are often there for good reason.




Anyway, if you are showing Form2 using the Show() method, you'll want to raise an event from Form2 when you want Form1 to update its text box, and then Form1 can listen for this event, and react accordingly by updating its text box. This is exactly how I implement this sort of communication in VB.NET too, for the record :)

I suggest reading over the tutorial JackOfAllTrades linked to again; focusing particularly on the description of using events for communication. Then, have a go at writing some code, and post that code here (with your problem) if you get stuck.

This post has been edited by CodingSup3rnatur@l-360: 22 September 2013 - 03:54 AM

Was This Post Helpful? 2
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3252
  • View blog
  • Posts: 10,904
  • Joined: 12-December 12

Re: textbox value change from other form

Posted 22 September 2013 - 03:46 AM

There is a useful table here providing a side-by-side comparison of C# to VB.NET.

Another difference is the way VB handles implicit type conversions. Adding Option Strict On to the top of your VB code (which I recommend) will cause VB to behave more closely to C# in this respect; you'l see a number of warnings that weren't there before.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1