locked
Copying a form fra one app to another ? RRS feed

  • Question

  • What is the OFFICIAL MICROSOFT way to "use" a form in one application in another application - simply to make a copy useable in the other application.

    If you search the net multible methods emerges - but the people who designed VS must have done the design - or ... ?

    IF IT'S NOT added as a feature - what is the best practice ?


    Best regards KSor, Denmark
    Tuesday, February 8, 2011 12:20 PM

Answers

  • Hi Ksor,

    How about right-click the project, then click the "add"->"existing item.." and choose the .cs file you want to add? But also, you should modify something if you want to use the .cs file in the current project.

    Sincerely,
    Vin Jin


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Ksor Thursday, February 17, 2011 10:12 AM
    Thursday, February 10, 2011 7:24 AM

All replies

  • I don't think there is an official way, but I prefer this way, depending on the situation (your description is a little vague):

    http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/47956890-c539-4eed-a96a-7f2a1a28ee35

    Regards,

    Fábio


    "To alcohol! The cause of and solution to all of life's problems." - Homer Simpson
    • Marked as answer by Aspen VJ Thursday, February 17, 2011 9:48 AM
    • Unmarked as answer by Ksor Thursday, February 17, 2011 10:10 AM
    • Marked as answer by Ksor Thursday, February 17, 2011 10:10 AM
    • Unmarked as answer by Ksor Thursday, February 17, 2011 10:11 AM
    • Unmarked as answer by Ksor Thursday, February 17, 2011 10:11 AM
    • Unmarked as answer by Ksor Thursday, February 17, 2011 10:11 AM
    Tuesday, February 8, 2011 12:43 PM
  • To share anything between two applications, you generally extract the shared bits into a library (dll) and reference this from each application. 

    A ward or caution: if your library is going to contain classes that will be passed between these applications, I'd suggest adding the shared dll into the Global Assembly Cache.  If you don't, then each application takes its own, local copy of the dll and (in my experience) you can't pass objects between the two (a class in a library in one application is a totally different Type to the same class in another copy of the same library in another application).

     


    Regards, Phill W.
    Tuesday, February 8, 2011 12:48 PM
  • I don't beleave there is "NO OFFICIAL WAY" or at least a "BEST PRACTICE" ... I think it's an error that you can right-click on the source-form, take a copy - but nothing happens when you try to paste it in the distination-form - remember Murphy ... what people can do they do !

    The VS-design team MUST have a solution - Mr/Mrs. Moderator what is it ?

     


    Best regards KSor, Denmark
    • Edited by Ksor Tuesday, February 8, 2011 1:29 PM
    Tuesday, February 8, 2011 1:09 PM
  • One thing is for sure. Copy and Paste is a bad practice. If you want to do that, you can copy the files (including the designer file, it's that simple). What I think it is wrong is that you need to do that. When you find yourself doing that, is because you're doing something wrong in the first place.

    When you want to take advantage of reusable code, you build libraries, that in turn get consumed by your applications. This is the way that .net framework is built, how their controls are built. You can do exactly the same. Do you have a form with a lot of good stuff implemented? Pack it into a library and add it as a reference in your project.

    That's not official, it's just common sense. And again, you didn't specify your scenario (that may have many different approaches depending on the situation), so it's very difficult to know what would be a best practice. You want an example of a Form packed to a library, take a look at my article . It does exactly that and packs common and reusable features of a form into a library. But again, that's one specific scenario.

    If you can explain a little better your scenario, perhaps the community can give you a better directed advice on the matter.


    "To alcohol! The cause of and solution to all of life's problems." - Homer Simpson
    Tuesday, February 8, 2011 1:29 PM
  • If it's NOT by design to use the copy/paste - why then present it when right-clicking on the source-form ? (remember Murphy's law !)

    Maybe they re-used the "right-click menu DLL" where it should have had a little "twist" = no copy/paste !

    Often I re-use forms from other projects with just a little "twist" so the DLL-solution is no good - a WORKING copy/paste would be good !

    When you search the net many have a "personal method" - more or less clumpsy - for doing "a copy" so many people COULD use it.

     


    Best regards KSor, Denmark
    Tuesday, February 8, 2011 4:37 PM
  • "If it's NOT by design to use the copy/paste - why then present it when right-clicking on the source-form ?"

    Murphy has nothing to do with it, it's the same thing as limiting a car to be able to reach only 60 mph. Just because a car can reach faster speeds, it doesn't mean you should. And as in any text editor, visual studio's text editor supports copy and paste, which comes in handy in many situations, but that doesn't mean you should copy the whole file and paste somewhere else expecting it to work.

    If you want to re-use forms from other projects, copy the Form.cs and Form.designer.cs file to your new project folder, and from the solution, right click the project, Add Existing Item..., select the copied Form.cs file and visual studio will add to your solution and automatically include the .designer.cs file. No hassles. Maybe this is what you're looking for.


    "To alcohol! The cause of and solution to all of life's problems." - Homer Simpson
    Tuesday, February 8, 2011 4:46 PM
  • Murphy has nothing to do with it, it's the same thing as limiting a car to be able to reach only 60 mph. Just because a car can reach faster speeds, it doesn't mean you should. And as in any text editor, visual studio's text editor supports copy and paste, which comes in handy in many situations, but that doesn't mean you should copy the whole file and paste somewhere else expecting it to work.

    that's simply not an argument - Murphy, me and many other have realized that what people CAN they do !

    We should do our best to JUST present what's useable for the user when we build our applications - so should the VS-people too.

    I'll try your copy/paset method and hope it will give no problems - thx.

    Still wondering why no Moderator is telling the OFFICIAL method.


    Best regards KSor, Denmark
    Tuesday, February 8, 2011 7:33 PM
  • If you really don't want to use the practical way (which is the way I already explained, and VS supports it just like that) you can go the "official" way. This doesn't mean you should use one over the other, it's just a matter of analyzing a situation and see what's more suitable for each situation. As I mentioned earlier, there is no official way to anything, there is common sense to use stuff for better productivity, sticking with this square view will not lead to productivity. But there are cases where the following solution is practical, specially when something is going to be used over and over:

    http://msdn.microsoft.com/en-us/magazine/cc188697.aspx


    "To alcohol! The cause of and solution to all of life's problems." - Homer Simpson
    Tuesday, February 8, 2011 7:50 PM
  • >> Fabio Franco

    Your method:

    "If you want to re-use forms from other projects, copy the Form.cs and Form.designer.cs file to your new project folder, and from the solution, right click the project, Add Existing Item..., select the copied Form.cs file and visual studio will add to your solution and automatically include the .designer.cs file. No hassles. Maybe this is what you're looking for."

    is a crible like so many others ;-)) ... when the files are copied in Explorer and added in the new project YOU HAVE TO CHANGE THE NAMESPACE NAME ... it works but a little slow - it seems like you donøt corrupt anything ... I hope !


    Best regards KSor, Denmark
    Tuesday, February 8, 2011 7:57 PM
  • No, it shouldn't corrupt anything, and yes, if you want to the form belong to the same namespace you should change the namespace, but that's not mandatory, you can still using the form by referencing the namespace. And you'll also need to include any dependencies (for example, if the form shows another form, you'll need to include that other form also).
    "To alcohol! The cause of and solution to all of life's problems." - Homer Simpson
    Tuesday, February 8, 2011 8:02 PM
  • Offcause there is an official way of doing things - IF they were designed/planed !

    In WORD (and many others applications) it was designed that opening a file should be done like this: File / Open file / "find it and clock open" !

    So IF facilities is designed there IS an official way of doing them - the UN-official way could be to do some VBA coding - and beleave me - one code-shark would surely come up with such VBA-code

    But never mind - let's see if a Moderator comes up with some explanation.

    (I wonder why you are so eager defending the lag of facility !)


    Best regards KSor, Denmark
    Tuesday, February 8, 2011 8:09 PM
  • I'm not defending lag of facility. I'm just saying visual studio is doing what it's supposed to do. As a programmer you should now that. If everything was automatic as you want, visual studio wouldn't exist because programming would be automatic. It's very easy to do what you want, really. Very easy. You could even develop a visual studio plug-in if you're not satisfied the way things are handled, if you think renaming a namespace is so much work. And if visual studio would automatically rename the namespace, then it would displease the many people who wish the namespace was kept the same.

    If microsoft Added every functionality everyone wanted, they wouldn't even have the first release of visual studio and still it wouldn't please everyone, because some features conflict with othres. The way Visual Studio is now, lets the user choose what happens. As I mentioned, you can build a template like on the link I provided, but that's much more work and I wouldn't bother if it wasn't nescessary. It can be done, but is it worth it?

    Remember, Visual Studio is not Excel or Word, it's not designed for the regular end user. It's designed for programmers, who can get around on simple stuff like that. It's not a program that makes programs, essentially it's a very advanced text editor, that's it's purpose, to help you write and compile code. A good thing is it automates lot of things so you get productivity on dragging and droping (instead of manually writing the code that designs a form). I think you would be very pleased if you started using other tools around. Try experimenting with the IDEs of other technologies and then you'll be upset.


    "To alcohol! The cause of and solution to all of life's problems." - Homer Simpson
    Tuesday, February 8, 2011 8:20 PM
  • Never mind - let's see if a Moderator comes up with some explanation.
    Best regards KSor, Denmark
    Tuesday, February 8, 2011 9:36 PM
  • Hi Ksor,

    How about right-click the project, then click the "add"->"existing item.." and choose the .cs file you want to add? But also, you should modify something if you want to use the .cs file in the current project.

    Sincerely,
    Vin Jin


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Ksor Thursday, February 17, 2011 10:12 AM
    Thursday, February 10, 2011 7:24 AM
  • Yeah, Vin Jin, I already proposed that to him, but he didn't like my answer, maybe now that you said it, he will accept an "offical" answer.

    Thank you.

    Regards,

    Fábio


    "To alcohol! The cause of and solution to all of life's problems." - Homer Simpson
    Thursday, February 10, 2011 12:07 PM
  • When you answer as a moderator I beleave this is the "official, best practice" - right ?
    Best regards KSor, Denmark
    Thursday, February 17, 2011 10:13 AM