C# Application

Associate
Joined
7 Aug 2008
Posts
302
Hey Lads,

I'm currently trying to pass data from one form to the other in C# (VS 2010).

The data in the first form contains generica data; firstname, lastname etc and I am trying to make it so when I press an edit button on Form1, it will load up the second form (Form2) and load the data that is currently in the textboxes in Form1 but enabled (so we can edit them as they are disabled by default to just enable browsing on Form1).

What is the easiest way to do this?

I've been trying to create an instance of a form and messing around with that but can't get it to work atm.

Any pointers?

Thanks,
Tom.
 
You could

use the same screen. Everything readonly / disabled

then upon edit just enable them.

The values will stay within the page viewstate.

make sure you add validation to the edit screens.
 
^What he said.

Either have one form and use Readonly Textboxes to display the data - set Readonly to false when you click Edit.

Or

If you just want to display the data in labels, add to panels to the page ( one for View and one for Edit)

Display the data in labels in the View Panel - when you click Edit, disable the View panel and Enable the Edit panel (making sure you populate the textboxes in the Edit panel on the click event)

good luck!
 
Hey Lads,

I'm currently trying to pass data from one form to the other in C# (VS 2010).

The data in the first form contains generica data; firstname, lastname etc and I am trying to make it so when I press an edit button on Form1, it will load up the second form (Form2) and load the data that is currently in the textboxes in Form1 but enabled (so we can edit them as they are disabled by default to just enable browsing on Form1).

What is the easiest way to do this?

I've been trying to create an instance of a form and messing around with that but can't get it to work atm.

Any pointers?

Thanks,
Tom.

you could pass the item as part of the form constructor if you wanted.

ie rather than having protected void form() you have protected void form(myobject)

another way is to have a public property on the edit form and assign the form that object after initial construction ie


code from form 1 (for example):

private void edit_form()
{
myform2 = new myform2();
myform2.MyObject = someObjectFromThisForm;
myform2.show();
}

then when the form is shown you then assign the textboxes with the data required on the form load presuming that validation has been done beforehand to ensure a valid object exists.

Personally I prefer the first option as it is cleaner and ensures you have the object there before loading the form.

As others have said you could just use the one form and have an edit/read-only version.

If you have it on one screen then don't clutter it with labels for the read only version and then have controls for editing, just enable/disable the edit controls depending on what state you want the form in. (personal opinion here.)

Not sure if you have thought about it/what your experience of coding is but it is something I am trying to do more and more is seperate your logic code from the presentation side of things. Using design practices from MVC (Model-View-Controller) has helped me a huge amount to develop better apps both web (still webforms at the moment but hoping to move to MVC soon) and winforms.

It also makes debugging a lot easier to fault find when something goes wrong. Just a thought there, may be a step too far at the moment if you are starting out with c#


if you need a hand with anything give me a shout and I'll try and help out as best I can.
 
Hey lads.

Thanks for the replies.

I had to use seperate forms unfortunately as it was a requirement. However, I do agree it would probably make more sense to have them disabled by default then enabled when the Edit button was pressed. Got it sorted now anyway with me being able to save the new changes to a text file etc.

I used public static TextBox First = new TextBox();

As part of the code to swap data from one form to another if anyone was interested.

Cheers,
Tom.
 
you want to be careful with static like this, static makes the instance of the textbox shared between every user of the page so you can end up overwriting another users information doing it like this.

Best ways are probably to use viewstate if on the same page or put it in and out of session to go between pages.
 
Passing information from one form to another can be very situational, and I will either use the forms constructor to receive the data or I will set up an event to pick up on the information, purely depends on what is appropriate for the situation.

Using a static TextBox sounds a bit messy and "wrong", if you wanted to go down that route you could just create a static class with a static string on it, so the parent form can set the string and the child form can read from it. Alternatively you can use properties like davetherave2 said, but you could get into issues if the form is disposed.

So if it was me I would start reading up on delegates and events and seeing how I can use them to solve the problem.

The above is for WinForms, don't know much/enough about ASP or ASP.NET to discuss what you could do there.
 
Id have public properties on the form or construct a class encapsulating all the information and pass it across. Static textbox isn't right and delegates not right for this either imo.
 
some of the worst advice i've ever heard in my life in this thread!

this is blatantly a winforms application - ASP.Net devs move along immediately as you've not got a clue what you're on about.

the OP should just do something like this:

NextForm nextForm = new nextForm();
nextForm.Surname = currentForm.Surname;
etc......

nextForm.ShowDialog();

make sure there are text boxes that read their values from the appropriate properties in NextForm - and ensure those text boxes are not readonly
 
:confused: You've just compared chalk to cheese, and then to bananas... Where the hell have you picked up he needs to use a DB, and a Singleton, when all he is doing is switching between read only and editable fields?

I think he's referring to the persistence of data from one form to the next (ie. persist it in a database). Probably to do with a web form rather than a windows form, and probably because he doesn't know what Viewstate is or what it's used for.

And probably a Singleton because it was fashionable about 7-8 years ago to have a singleton pattern to return a database connection. Soon after this people realised that this was a phenomenally bad idea and stopped doing it; it's hard to test, many db drivers are not thread-safe (ie. access to the singleton, even though it is thread-safe, must be serialized - probably by some cumbersome locking mechanism).

The correct answer is as above, either a writeable textbox on the target form. Or if it makes sense, add it as a parameter to a constructor on the target form, and set the text box in the constructor as well.
 
this is blatantly a winforms application - ASP.Net devs move along immediately as you've not got a clue what you're on about.

:D aha I realised that after I posted my comment.

You should rewrite it all in Reactive UI 2 and program everything fully async to be coolz really.
 
As a simple WinForms App, which requires you to use two forms to carry out this task I would look at a simple class that could be used to store the data and then just send it across to the new form?

May be a bad Idea but my WinForm knowledge in C# is limited.
 
Back
Top Bottom