none
changing left margin for InfoPath column using c# code RRS feed

  • Question

  • Hi,

    I have a calculated column in my form. 

    I would like to increase the left margin by 2 px using the cs (c#) code.

    How to achieve this?

    Thank you
    Thursday, January 19, 2012 7:48 AM

Answers

  •  

    OK...  There is a way.  Invoking dark magics of XSL (you have read the disclaimer).  This is totally unsupported, but fun.

     

    1. Create a new form, add a number field "n1".

    2. Put this field onto the design area within a section 

    3. create a rule on the section, "if n1 > 0", make the background red.

     

    (these steps are useful to set up the form to a stage where we can add additional rules).

     

    4. save your form as "MarginForm.xsn", we're about to do unsupported magic.  

    5. go to File | Publish | Export | Export Source Files - save the component files of your form into a blank folder, e.g. "C:\Temp\MarginForm\"

    6. close InfoPath designer, go to your folder, and look for the XSL file for your view, typically, it's <view name>.xsl.  For me, this is: C:\Temp\MarginForm\view1.xsl

    7. Open the xsl in a text editor - preferably one that understands XSL to give you syntax highlighting.

    8. Find our formatting rule, it looks like this:

     

     

     <xsl:attribute name="style">BORDER-BOTTOM: 0pt; BORDER-LEFT: 0pt; WIDTH: 100%; MARGIN-BOTTOM: 0px; BORDER-TOP: 0pt; BORDER-RIGHT: 0pt;<xsl:choose>

     <xsl:when test="../my:n1 &gt; 0">BACKGROUND-COLOR: #ff6600; caption: Rule 1;</xsl:when>

     </xsl:choose>

     </xsl:attribute>

     

     

    9. add additional formatting for margin-left, like this:

     

     <xsl:attribute name="style">BORDER-BOTTOM: 0pt; BORDER-LEFT: 0pt; WIDTH: 100%; MARGIN-BOTTOM: 0px; BORDER-TOP: 0pt; BORDER-RIGHT: 0pt;<xsl:choose>

     <xsl:when test="../my:n1 &gt; 0">BACKGROUND-COLOR: #ff6600; caption: Rule 1; MARGIN-LEFT: <xsl:value-of select="../my:n1"/>px;</xsl:when>

     </xsl:choose>

     </xsl:attribute>

     

     

    10. save your XSL, close it.  Open  the whole thing back in InfoPath designer from manifest.xsf

    11. test it and save it back into MarginForm.xsn file.

    12. Once you are comfortable and understand what's going on, clean up the rule in xsl (don't use the designer) and get rid of the rule for "red".

     

    RESULT:

     

    My xsn file is here:  http://johnliu.net/storage/MarginForm.xsn

     

     


    jliu - http://johnliu.net - http://sharepointgurus.net
    • Edited by John Liu Monday, January 23, 2012 7:16 AM removed "sigh" - I don't usually like to talk about techniques that very tricky though...
    • Marked as answer by Venkatzeus Wednesday, January 25, 2012 7:41 AM
    Monday, January 23, 2012 7:07 AM

All replies

  • Can't be done.

    You can, however, do this:

     

    1. create a empty expression box.  width 2px

    2. set a formatting rule that says if my:show2px is false, then hide

    3. in C#, put "true" into the field /my:show2px


    jliu - http://johnliu.net - http://sharepointgurus.net
    Thursday, January 19, 2012 8:51 PM
  • Hi, Thank you very much for the reply. If I want to dynamically increase the width based on incoming data, is there any alternate way of doing it?
    Monday, January 23, 2012 5:54 AM
  •  

    OK...  There is a way.  Invoking dark magics of XSL (you have read the disclaimer).  This is totally unsupported, but fun.

     

    1. Create a new form, add a number field "n1".

    2. Put this field onto the design area within a section 

    3. create a rule on the section, "if n1 > 0", make the background red.

     

    (these steps are useful to set up the form to a stage where we can add additional rules).

     

    4. save your form as "MarginForm.xsn", we're about to do unsupported magic.  

    5. go to File | Publish | Export | Export Source Files - save the component files of your form into a blank folder, e.g. "C:\Temp\MarginForm\"

    6. close InfoPath designer, go to your folder, and look for the XSL file for your view, typically, it's <view name>.xsl.  For me, this is: C:\Temp\MarginForm\view1.xsl

    7. Open the xsl in a text editor - preferably one that understands XSL to give you syntax highlighting.

    8. Find our formatting rule, it looks like this:

     

     

     <xsl:attribute name="style">BORDER-BOTTOM: 0pt; BORDER-LEFT: 0pt; WIDTH: 100%; MARGIN-BOTTOM: 0px; BORDER-TOP: 0pt; BORDER-RIGHT: 0pt;<xsl:choose>

     <xsl:when test="../my:n1 &gt; 0">BACKGROUND-COLOR: #ff6600; caption: Rule 1;</xsl:when>

     </xsl:choose>

     </xsl:attribute>

     

     

    9. add additional formatting for margin-left, like this:

     

     <xsl:attribute name="style">BORDER-BOTTOM: 0pt; BORDER-LEFT: 0pt; WIDTH: 100%; MARGIN-BOTTOM: 0px; BORDER-TOP: 0pt; BORDER-RIGHT: 0pt;<xsl:choose>

     <xsl:when test="../my:n1 &gt; 0">BACKGROUND-COLOR: #ff6600; caption: Rule 1; MARGIN-LEFT: <xsl:value-of select="../my:n1"/>px;</xsl:when>

     </xsl:choose>

     </xsl:attribute>

     

     

    10. save your XSL, close it.  Open  the whole thing back in InfoPath designer from manifest.xsf

    11. test it and save it back into MarginForm.xsn file.

    12. Once you are comfortable and understand what's going on, clean up the rule in xsl (don't use the designer) and get rid of the rule for "red".

     

    RESULT:

     

    My xsn file is here:  http://johnliu.net/storage/MarginForm.xsn

     

     


    jliu - http://johnliu.net - http://sharepointgurus.net
    • Edited by John Liu Monday, January 23, 2012 7:16 AM removed "sigh" - I don't usually like to talk about techniques that very tricky though...
    • Marked as answer by Venkatzeus Wednesday, January 25, 2012 7:41 AM
    Monday, January 23, 2012 7:07 AM
  • Hi,

    Thank you very much for the reply

    Wednesday, January 25, 2012 7:41 AM