I'm writing an very simple text editor with highlight of digitis, in some keywords and special chars. Now I'm implementing the line numbers,for do it I'm using a ListView, that is updated to each new line added.
The problem that is when I down scroll using cursor,the number of lines is not updated,then if I jump to 30 line, the listView remains where it was, in line 10, for example for this reason I'm looking an way to get scroll the coordinates of RichTextBox to sincronize with scroll of listView.
Instead of developing your own text editor, you can try SharpDevelop's editor. You don't have to fully install it. Just download its source and compile only ICSharpCode.AvalonEdit
It has a property ShowLineNumbers :)
TextPointer textPointer = MyRichTextBox.Document.ContentStart.GetPositionAtOffset(8);
if (textPointer != null)
{
MyRichTextBox.CaretPosition = textPointer;
MyRichTextBox.Focus();
MyRichTextBox.Selection.Select(MyRichTextBox.CaretPosition, MyRichTextBox.CaretPosition);
}
Related
I have a content control in word, let's call it docHeader. When the text inside that control gets long, the MSWord automatically continues from next line (wraps the text?). This causes some other controls to change their position, what I don't want.
My question is, is it possible to detect it somehow, that the control's value has multiple lines?
ContentControl contControl = document.GetDocumentContentControlByName("docHeader");
//Check if contControl.Value is longer than one line
//Probably also depends on font size, margins, etc.
//This is what I finally need:
contControl.HasMultipleLines();
Thank you.
I have a RichTextBox in a WPF application that serves as an output container for status updates. Each line added is colored based on it's informational level (warning-yellow, info-gray and so on).
Paragraph currentStatus = new Paragraph(new Run("ERROR: Couldn't find stuffs."));
currentStatus.Foreground = System.Windows.Media.Brushes.Red;
List myList = new List();
myList.ListItems.Add(new ListItem(currentStatus));
rtbStatus.Document.Blocks.Add(myList); // existing rich textbox
Though it is technically working, after hours of digging I still have a few formatting problems I can't seem to over-come or research out:
I want the list to be inverted, with the most recent 'post' at the top. I have been able to achieve this a couple of different ways, but each at a cost of losing the previous color formatting to the default foreground color of the control (the app has a visual buffer of about 10 lines when spacing is ideal that needs to retain the color applied).
I want the line spacing to be normal, w/o padding between lines. There is enough room for almost 2 more lines between each 'post' when using a list and I am looking for something resembling a textblock's multi-line spacing (see screen linkage below).
I'd love to get rid of the bullet points if a list is the way to go.
A couple notes: This all has to be done on the back-end, and I would like to look at a smooth auto-scrolling animation as a future feature release, though I haven't researched it yet (off-thread topic).
Now, everything I am reading leads me to believe a richTextBox>flowDocument>list is my best solution as I couldn't figure out how leverage the AppendText() method with a line break (environment.NewLine works, but has an even greater amount of padding between lines) nor work out the color dynamics when using other controls, but I am a novice in the C# world.
Please tell me if I am doing this the hard way first and foremost. But if anyone has ideas on how to achieve the above it'd be greatly appreciated.
Image of the above syntax:
Image of the desired spacing results using textblock:
Thanks in advance.
I found some properties that allowed me to accomplish this, so I removed the list and after some tweaking came up with the below:
// Status Update
public void UpdateStatus(string eventLevel, string message)
{
Paragraph currentStatus = new Paragraph(new Run(message));
if (eventLevel == "info")
currentStatus.Foreground = System.Windows.Media.Brushes.Gray;
if (eventLevel == "warning")
currentStatus.Foreground = System.Windows.Media.Brushes.Yellow;
if (eventLevel == "error")
currentStatus.Foreground = System.Windows.Media.Brushes.Red;
if (eventLevel == "highlight")
currentStatus.Foreground = System.Windows.Media.Brushes.CornflowerBlue;
currentStatus.LineHeight = 1;
rtbStatus.Document.Blocks.InsertBefore(rtbStatus.Document.Blocks.FirstBlock, currentStatus);
}
and can now append colored lines to the 'top' with a minimal line space:
UpdateStatus("error", "My custom error message");
I've got a RichTextBox containing lines of text (each line a paragraph), and I have a line number that represents an error. I want to set the caret to be at the start of that line. How?
I haven't got a working installation of C# on my machine at the moment, but from looking at the docs it seems you should be able to first locate the paragraph you want (probably by enumerating through them, there does not seem to be an index function) in yourFlowDocument.Blocks and then:
TextPointer paragraphStart = paragraph.ElementStart;
richTextBox.CaretPosition = paragraphStart;
I am programmatically creating a FlowDocument, then translating it to Rich Text, so a user can edit it in a Rich Text box. I cannot figure out how to insert a horizontal line in the FlowDocument that will display across the whole width of the edit box (and subsequent rendered PDF).
I found this thread which (allegedly) shows how to do exactly what I want, in XAML:
Simple (I think) Horizontal Line in WPF?
And this one:
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/bcd334c7-8e2b-4e59-a344-4376b30cf847/
I have attempted to replicate this programmatically, like this:
Line pLine = new Line();
pLine.Stretch = Stretch.Fill;
pLine.Stroke = Brushes.Black;
pLine.X2 = 1;
para.Inlines.Add( pLine );
But, this ends up displaying nothing at all in the resulting RTF edit box. The rest of the Rich Text is there. Just no line at all.
I also tried creating a Table and inserting the text I want to come after the horizontal line into a cell. Then I set a custom border, just for the top of the Table. That gives me a horizontal line, but it doesn't go all the way across the screen (after it's converted to RTF and shown in the RTF edit box), and it doesn't show at all after I render it into a PDF.
I have resorted to the complete hack of just inserting a Run of 60 underscore ('_') characters and a LineBreak. That "works", but it sucks. Can anybody tell me how to do this correctly?
i am using this little monster to insert a line at the current cursor position in a RichTextBox.
var ruler = new Line { X1 = 0, Y1 = 0, X2 = 1000, Y2 = 0, Stroke = new SolidColorBrush(Colors.Black), StrokeThickness = 2 };
new InlineUIContainer(ruler, richtextbox.CaretPosition.GetInsertionPosition(LogicalDirection.Forward));
I think you forgot to set the StrokeThickness.
And even if you set Stretch.Fill your line is only one device independent unit long.
My experience with Otto's method was that it draws a nice horizontal line in the editor but the line is not saved in the RTF file. My handy little O'Reilly "RTF Pocket Guide" yielded a block of RTF that does both. Here is the click handler for my "Insert Line Break" toolbar button...
private void InsertLineBreak_Click(object sender, RoutedEventArgs e)
{
MemoryStream stream =
new MemoryStream(ASCIIEncoding.Default.GetBytes(#"{\pard\brdrb\brdrs\brdrw10\brsp20\par}"));
rtbEditor.Selection.Load(stream, DataFormats.Rtf);
}
I am creating a Word document on the fly as a C# VS 2010 Office Word project for a client who wants to be able to generate a document that will allow the appropriate number of signatory locations for a particular deal going down. There is a table that will need to be generated with sufficient rows and then later in the doc I have to produce prefab blocks for personal info per signatory.
I am working on the table part now and have almost everything as I want it, but the text in all of the cells is vertically top aligned. I have visited EVERY site in the ENTIRE internet in the past few days for up-to-date information on Word automation that is current for .Net 4, VS 2010 and Office 2010. I have syntax that compiles w/o error but fails to bottom align as I desire. I have even stabbed about with IntelliSense to see if I could find another solution.
This code focuses on a single row:
tbl.Range.Rows[1].Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalBottom;
This runs but the text stays helium-filled.
Any Word automation wizards out there?
I was unable to reproduce the problem. This code works just fine:
using Microsoft.Office.Interop.Word;
class Program
{
static void Main(string[] args)
{
DocumentClass document = new DocumentClass();
object defaultTableBehavior = null, autoFitBehavior = null;
Table tbl = document.Content.Tables.Add(document.Content, 2, 2, ref defaultTableBehavior,
ref autoFitBehavior);
tbl.Rows[2].Cells[2].Range.InsertAfter("This is a test.");
tbl.Rows[2].Cells[2].Range.ParagraphFormat.SpaceAfter = 0f;
tbl.Rows[2].Cells.Height = 50f;
tbl.Range.Rows[2].Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom;
}
}
I suspect that some other problem must be in play, like the paragraph spacing after, or perhaps the wrong range is selected?
The text is probably centered vertically, but it incudes a paragraph spacing other than "0." So, Word is viewing the extra line as additional text that needs to be included in the vertical centering.
To get around this, simply highlight the text you want to be vertically centered (or the entire table if that is what you want). Then go to "Page Layout" and reduce the "Spacing" "After" to "0." If you also have a space on the top of your text, you will need to reduce the "Spacing" "Before" to "0" as well. With no spacing before or after the text, the actual text will now be centered.
This is for an old question, but I just ran into the same problem and a fix for this. Add this to your table:
tbl.Range.ParagraphFormat.SpaceAfter = 1; // change the 1 to how much space you want extra in your cell
Just if anybody is following up this post, my text also was arranged at the top of the cells.
So for me the following did what I needed in a qick way
oTable.Range.ParagraphFormat.SpaceAfter = 6;
oTable.Range.ParagraphFormat.SpaceBefore = 6;
Space can be adjusted if needed.
The following worked for me in Word 2011 for Mac- nothing else suggested in numerous sites seemed change the vertical cell alignment for me. Found this out by trial and error.
I highlighted the cells I wanted vertically align to bottom right and changed the line spacing (in my case it was 1.5 for the table) to 1 for those cells. It finally worked. Hope it helps.
Highlight text within the table
--> go to Line Spacing Options
--> in the Spacing Before/After section set Before and After to zero px (or equal).
All the alignment options should now work.