We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Copying and pasting CENTERED text

We have some celltypes that are textbox. When we get data to load these text boxes, the data is centered. So for example a text box might come up as " Test1 ". There is almost always going to be 2 spaces in the front and 2 at the end of the text. The problem I am having is that when i copy and paste a cell its not copying the spaces. So in my sample the first 2 cols are celltype textbox and the second 2(3,4) are static. I want to know how using a text box can I do a copy and paste and have it grab the spaces when it copies and then have it paste the spaces so the data is still centered. It works how i think it should and how i want it to in the static cell type in cols 3 and 4. Any ideas on this?? copypasteTEXTcenter_9987.zip

7 Replies

AD Administrator Syncfusion Team October 26, 2004 02:04 PM UTC

Thank you for the sample. Can you explain a little more about how to see the problem? If I copy and paste within the grid, I see the same things whether colamns A&B are copied and pasted or columns C & are copied and pasted. The spaces are retained in this case. (This is because you have commented out the code that would turn off the Styles copy which is allowing this to happen). Now if I copy from the grid to Notepad, then I do see the spaces being stripped off. But again, it does not matter whether I am copying from A&B or C & D. This is because going into Notepad makes this a Text paste and not a style paste. The grid has Trim call to strip off leading or trailing spaces in this case to conserve clipboard memory. There is no property setting to control this. If you do not want this behavior, then you would have to handle the ClipboardCopy event, and do teh work yourself, copying the seelcted cells into a string and include the leading spaces that you want. Here is a forum thread that shows the code the grid uses (including the Trim call that is causing this behavior). You could follow teh approach suggested there to avoid the Trim. http://www.syncfusion.com/Support/Forums/message.aspx?MessageID=7382

PB Philip Bishop October 26, 2004 02:32 PM UTC

Ok sorry i did have that line commented out. We need that in our code but i was testing some stuff right before i sent it and thats why i was getting those results. So yes I just want to be able to copy leading and trailing spaces. I want it to work like excel does. If u go to excel and type in " Test "(minus the quotes) then paste it, you get 2 spaces then Test and then 2 spaces. So i would of thought your cell type or one of your cell types would work like that? This isnt the case? If not, then is what u said before about handling it myself my only option?

AD Administrator Syncfusion Team October 26, 2004 02:49 PM UTC

Yes, you will have to handle this yourself. I can make adding a property to not include the Trim a feature request, but do not know when we would get to it. It will not be in the 3.0 release as that code base is feature frozen at this point.

PB Philip Bishop October 26, 2004 03:10 PM UTC

Any chance u have that sample in VB code??

AD Administrator Syncfusion Team October 26, 2004 03:29 PM UTC

Here is some code you can try. It is a sample that handles ClipboardPaste to avoid copying hidden columns. You can probably use it as is, or you can remove the hidden column checks. FYI - here is a web link that does a reasonable job of converting C# to VB. http://authors.aspalliance.com/aldotnet/examples/translate.aspx
Private Sub gridControl1_ClipboardCopy(sender As Object, e As GridCutPasteEventArgs)
   Dim range As GridRangeInfo = e.RangeList.ActiveRange
   If Not range.IsEmpty Then
      range = range.ExpandRange(1, 1, Me.gridControl1.RowCount, Me.gridControl1.ColCount)
      Dim s As String = ""
      Dim data As GridData = Me.gridControl1.Data
      Dim row As Integer
      While row <= range.Bottom
         If Not Me.gridControl1.Rows.Hidden(row) Then
            Dim firstCol As Boolean = True
            Dim col As Integer
            While col <= range.Right
               If Not Me.gridControl1.Cols.Hidden(col) Then
                  If Not firstCol Then
                     s += ControlChars.Tab
                     firstCol = False
                  End If
                  Dim style As New GridStyleInfo(data(row, col))
                  s += style.Text
               End If
            End While
            s += Environment.NewLine
         End If
      End While
      e.Handled = True
   End If
End Sub ''gridControl1_ClipboardCopy

PB Philip Bishop October 26, 2004 03:55 PM UTC

Hey thanks for the cool conversion link. I didnt know anything like that was out there. I had your code in and was messing with it and i cant see as it every does anything. If paste it in my code it actually gets stuck in an endless loop in that while because there is no hidden col..but in looking at that code i cant see how it ever works because the COL variable never gets set so its always at zero, unless i am missing something.

AD Administrator Syncfusion Team October 26, 2004 04:10 PM UTC

I used the cool conversion tool to convert C# to the VB that you see above. One short-coming of teh tool is that it changes for-loops to while loops an dthat is why the VB code is not handling the col variable properly. You probably want to change both the while loops to for-loops. Here is another snippet to give you the idea.
For intRow = objRange.Top To objRange.Bottom
      If (Not objGrid.Rows.Hidden(intRow)) Then
              bolFirstCol = True
              For intCol = objRange.Left To objRange.Right
                    If Not bolFirstCol Then
                          strClipBoard += vbTab
                          bolFirstCol = False
                    End If
                    Dim objStyleInfo As New GridStyleInfo(objData(intRow, intCol))
                            strClipBoard += objStyleInfo.Text

            '' add in a new line after each row
            strClipBoard += Environment.NewLine
       End If

Live Chat Icon For mobile
Up arrow icon