Posted 8 September 2017, 5:56 am EST
hello,
i find a problem that how to set the cell's text with superscript and subscript
Forums Home / Spread / Spread for WinForms
Posted by: unclechao on 8 September 2017, 5:56 am EST
Posted 8 September 2017, 5:56 am EST
hello,
i find a problem that how to set the cell's text with superscript and subscript
Posted 8 September 2017, 5:56 am EST
Hello,
Using RichTextEditor Class' s SelectionCharOffset property, you should be able to set if you want the text to be appear as a subscript or superscript the same way it is done with RichTextBox. Here is a sample code that implements the same:
Dim rtb As New System.Windows.Forms.RichTextBox
rtb.Text = "e=mc2"
rtb.SelectionStart = 4
rtb.SelectionLength = 1
rtb.SelectionCharOffset = Font.Height / 2
rtb.SelectedText = "2"
FpSpread1.ActiveSheet.Cells(0, 0).Renderer = New FarPoint.Win.Spread.CellType.RichTextCellType()
FpSpread1.ActiveSheet..Cells(0, 0).Value = rtb.Rtf
rtb.Dispose()
Hope it will help you. Please let me know if you have any queries further.
Thanks,
Manpreet Kaur
Posted 8 September 2017, 5:56 am EST
thank you,
but if you double click the cell , it show some code like this
{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fnil\fcharset134 \'cb\'ce\'cc\'e5;}}
\viewkind4\uc1\pard\lang2052\f0\fs18 e=mc\dn10 2\up0\par
}
Posted 8 September 2017, 5:56 am EST
Hello,
You are setting the renderer for the cell, which only paints the value in the specified format into the cell, however the value of the cell is still the rich text assigned to it, and you see the same value when the cell enters the edit mode. You can set the celltype of the cell to RichTextCellType so that the cell displays and contains the same value. Below is the code for the same:
Dim rtb As New System.Windows.Forms.RichTextBox
rtb.Text = "e=mc2"
rtb.SelectionStart = 4
rtb.SelectionLength = 1
rtb.SelectionCharOffset = -(Font.Height / 2)
rtb.SelectedText = "2"
FpSpread1.ActiveSheet.Cells(0, 0).CellType = New FarPoint.Win.Spread.CellType.RichTextCellType()
FpSpread1.ActiveSheet..Cells(0, 0).Value = rtb.Rtf
rtb.Dispose()
The code posted in the last post is used to set the superscript while the code posted here is used to set the subscript, all you need to do is to provide a negative value to the CharOffset property. Hope it will help you. Please let me know if you have any queries further.
Thanks,
Manpreet Kaur
Posted 8 September 2017, 5:56 am EST
thank you ,
you still don't know what I mean,if i double click the cell,
it show some other code,
I only want it show the value with superscript or subscript
Posted 8 September 2017, 5:56 am EST
Hello,
If you set the celltype of the cell to RichTextCellType, it would display the same value even on the double click. The code I provide to you in the last post sets the celltype, and hence displays the same value on double click. Kindly use the code posted in the last response to resolve your issue. Hope it will help you. Please let me know if you have any queries further.
Thanks,
Manpreet Kaur
Posted 4 March 2020, 5:44 pm EST
I want to make H20 (2 is subscript) in the cell[1,1] of fpspread1 on c#
how can I do?
Sorry I have read all thread but I do not understand “Using RichTextEditor Class’ s SelectionCharOffset” and how can you it.
Thank you
Posted 5 March 2020, 7:41 pm EST
Hi,
Try the following code to show subscript using Forms.RichTextBox:
System.Windows.Forms.RichTextBox rtb = new System.Windows.Forms.RichTextBox();
rtb.Text = "H2O";
rtb.SelectionStart = 1;
rtb.SelectionLength = 1;
rtb.SelectionCharOffset = -Font.Height/2;
rtb.SelectedText = "2";
fpSpread1.ActiveSheet.Cells[1, 1].CellType = new FarPoint.Win.Spread.CellType.RichTextCellType();
fpSpread1.ActiveSheet.Cells[1, 1].Value = rtb.Rtf;
rtb.Dispose();
Thanks,
Ruchir