Partial Class For a Form Problem

Hello!

When I create a partial class for my form the "View Designer" button is shown for my partial class. For the MyForm.Designer.cs partial class it isn't shown and that's the kind of behaviour I want and what I expected, especially as the "View Designer" button show another form, not the form that my partial class extends, but at the same time it overwrites entries in the MyForm.Designer.cs file, destroying my original Form.

I looked in the .csproj file, and changed the settings for my partial class to the way that it were set up for MyForm.Designer.cs, like this:

    <Compile Include="MyForm.Behind.cs">
      <DependentUpon>MyForm.cs</DependentUpon>
    </Compile>

But this only moved the file in under MyForm.cs and when the project is saved VS has automatically added the entry <SubType>Form</SubType>, which isn't added for the MyForm.Designer.cs file.

My question is if it is possible to tell the VS IDE not to show the "View Designer" button for my form So I won't destroy my original form by accident.
< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Best Regards,
Lani



Answer this question

Partial Class For a Form Problem

  • Shaolin13

    Hi Karen,

    Unfortantly your suggestion did not only apply to my partial class, instead it applied to the whole form class, hence it was removing the designer option for the whole form, not just my partial part of the form.

    Regards,
    Lani


  • kul

    Hi,

    Thank you for your reply!

    No the problem is not related.

    To reproduce the problem:

    1. Create a new windows application.
    2. Add a new class named Form1.Behind.cs
    3. Add the partial keyword in front of the "class Form1" declaration in the added class.
    4. Save everything.
    5. Close Visual Studio 2005.
    6. Open the project file, .csproj, in Notepad.
    7. Find the following node:

    [code]
        <Compile Include="Form1.Behind.cs">
          <SubType>Form</SubType>
        </Compile>
    [/code]

    Change it to this:
    [code]
        <Compile Include="Form1.Behind.cs">
          <DependentUpon>Form1.cs</DependentUpon>
        </Compile>
    [/code]

    8. Save the file.
    9. Open the project in Visual Studio 2005.
    10. Double click on Form1.cs in the Solution Explorer.
    11. Add a button to the form.
    12. Double click the Form1.Behind.cs file in the Solution Explorer.
    13. Add a checkbox to the new form and save it.

    The Form1.Designer.cs is now overwritten and if the Design for Form1.cs is opened it contains the checkbox and no title.

    I agree that editing the project file maybe isn't the safest thing to do. And I guess that is why the Form1.Designer.cs is being overwritten.

    But if you add a partial class of Form1, without editing the .csproj file, double-clicking it will open up a totally new Form! Although no new Form has been declared in code! The original Form1.Designer.cs is however not overwritten in that case.

    As I see it both these scenarios could be solved just by making the Solution Explorer hide the option to show the Design view of partial classes for forms. So that it gets the same behaviour as the Form1.Designer.cs file, which is what I expected.

    But maybe I'm totally lost here and should have used a different approach

    Anyway, I hope that it is easier to understand what I meant now.

    Best Regards,
    Lani


  • Steve vB

    Hi,

    Yes, that is my intention, but I would like the files grouped together in the editor so that you clearly can see that they are tied to each other. So if I for example as step 5 renames my file to Form1.MyCode.cs the problem still persists.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    To the moderators: I think that you are a little to trigger happy with the "Mark as answer button" we know how it works and could well be given more than 3 days to be able to read and reply to a post

     

    (To reply regarding this, please do so here)

     

    Regards,
    Lani


  • kenny08

    Hi Karen,

    Thank you very much for that information, I haven't tested it yet but I take your word for it, and it was exactly what I was looking for.

    Best regards,
    Lani

  • J Quick

    Lani -
    One way you could tell the IDE not to show the form in the designer would be to use the DesignerCategory Attribute.

    For instance, if you added this attribute on any part of the partial class, you would no longer get the option to open it in the Designer.

    [System.ComponentModel.DesignerCategory("code")]
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    }

    HTH,
    Karen



  • Mark

    Hi Peter!

    Thanks for your answer, exactly what I was looking for. I can also live with the .Designer added to my filename, but I must say that I think it's a really ugly solution made by Microsoft, it's what we in Sweden call a "fulhack" ( ugly hack)

    Thanks again!

    /Lani


  • warnerra

    Trying like this:

    1) Create new Windows Application with "Form1.cs" and "Form1.Designer.cs"

    2) Add new class item "PanelControl.Designer.cs"

    3) Change the class name in "PanelControl.Designer.cs" to a Form1 partial class like this

         partial class Form1

    4) Close "PanelControl.Designer.cs" then double click on it again from solution panel.

    It should be your intention.

     

     

     


  • CraigL

    Lani,

    Is this Thread related to the question you posted next, regarding the Settings file overwriting the Designer for the Form
    If that's the case then I'll merge the two threads.

    Else can you elobrate your problem a little more as to what steps did you do Since its not very clear right now.


    Regards,
    Saurabh Nandu
    www.MasterCSharp.com

  • Todd2000

    Hi! Lani!

    I've searched what makes Form1.Designer.cs file different from Form1.MyCode.cs and there is only one thing (.Designer)... so I've tryed to rename my Form1.MyCode.cs to Form1.MyCode.Designer.cs and it works...

    Hope you will be satisfyed with this sollution... I know I am.

  • Partial Class For a Form Problem