How do get the current line text to cursor from a Silverlight 4 RichTextBox Control - silverlight-4.0

In the Winforms RichTextBox control I have previously used the GetLineFromCharIndex method and the GetFirstCharIndexOfCurrentLine to work out the start and end points of the typed text on he current line.
I am struggling with the new RichTextBox control in Silverlight 4 as there doesn't appear to be equivalent methods. GetPositionFromPoint is available but seems a but clunky.
Cheers.
Updated...I have gone someway to making this work but this requires me to use the Select method of the control, this feels very wrong...
private string GetCurrentLine()
{
TextPointer prevSelStart = richTextBox1.Selection.Start;
Point lineStart = new Point(0, prevSelStart.GetCharacterRect(LogicalDirection.Forward).Y);
TextPointer prevSelEnd = richTextBox1.Selection.End;
TextPointer currentLineStart = richTextBox1.GetPositionFromPoint(lineStart);
//need to find a way to get the text between two textpointers
//other than performing a temporary selection in the rtb
richTextBox1.Selection.Select(currentLineStart, prevSelStart);
string text = richTextBox1.Selection.Text;
//revert back to previous selection
richTextBox1.Selection.Select(prevSelStart, prevSelEnd);
return text;
}

I don't think you can't avoid the selection, it's a proper way to do it (the "selection" is just a logical one), but you can avoid GetPositionFromPoint with TextPointer.GetNextInsertionPosition(LogicalDirection ): Start from richTextBox1.Selection.Start and move towards the beginning of the line (char != '\n')

I've needed to figure out when I was on the top line or bottom line of the RTB. To do this I used the GetCharacterRect methods then compared the tops to see if it was on the last line or first line.
You could do the same and use a text pointer to move through the text and number of times the tops don't match.
Here's code to see if the cursor is on the first or last lines:
private bool IsCursorOnFirstLine()
{
TextPointer contentStart = this.ContentStart;
TextPointer selection = this.Selection.End;
Rect startRect = contentStart.GetCharacterRect(LogicalDirection.Forward);
Rect endRect = selection.GetCharacterRect(LogicalDirection.Forward);
return startRect.Top == endRect.Top;
}
private bool IsCursorOnLastLine()
{
TextPointer start = this.Selection.Start;
TextPointer end = this.ContentEnd;
Rect startRect = start.GetCharacterRect(LogicalDirection.Forward);
Rect endRect = end.GetCharacterRect(LogicalDirection.Backward);
return startRect.Top == endRect.Top;
}

Related

Workbook cell style in POI/NPOI doesn't work properly with multiple styles in workbook

I'm running into strange problem with .Net version of POI library for Excel Spreadsheets. I'm rewriting from text files to Excel 97-2003 documents and I'm like to add some formatting programmatically depend on some values gather at the begging of the program.
At the beginning, in the same method where I was creating a new cell from given value I was creating also a new Workbook CellStyle which was wrong, because I was running out of the styles very quickly (or I was just thought it was the cause of the problem).
Constructor of the class responsible for Excel Workbook:
public OldExcelWriter(TextWriter logger) : base(logger)
{
_workbook = new HSSFWorkbook();
_sheetData = _workbook.CreateSheet("sheet1");
_creationHelper = _workbook.GetCreationHelper();
}
Method that is calling all the chains of operations:
public void Write(string path, Data data)
{
FillSpreadSheetWithData(data, _sheetData);
SaveSpreadSheet(_workbook, path);
}
Long story short, in FillSpreadSheetWithData I have method for creating a row inside which I'm have a loop for each cell, so basically I'm iterating thru every column, passing IRow references to a row, column value, index and formatting information like this:
for (int j = 0; j < column.Count; j++)
{
CreateCell(row, column[j], j, data.Formatting[j]);
}
and while creating a new styles (for first shot I was trying to pass some date time values) I had situation like this in my rewrited Excel: screenshot of excel workbook
So formatting was passed correctly (also Horizontal Aligment etc.) but it get ugly after 15th row (always the same amount).
DateTime dataCell = DateTime.MaxValue;
var cell = row.CreateCell(columnIndex);
_cellStyle = _workbook.CreateCellStyle();
switch (format.Type)
{
case DataType.Date:
_cellStyle.DataFormat = _creationHelper.CreateDataFormat().GetFormat("m/dd/yyyy");
if (value.Replace("\n", "") != string.Empty)
{
dataCell = DateTime.ParseExact(value.Replace("\n", ""), "m/dd/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
}
break;
}
switch (format.HorizontalAlignment)
{
case Enums.HorizontalAlignment.Left:
_cellStyle.Alignment = HorizontalAlignment.LEFT;
break;
case Enums.HorizontalAlignment.Center:
_cellStyle.Alignment = HorizontalAlignment.CENTER;
break;
}
if (dataCell != DateTime.MaxValue)
{
cell.CellStyle = _cellStyle;
cell.SetCellValue(dataCell);
dataCell = DateTime.MaxValue;
}
else
{
cell.CellStyle = _cellStyle;
cell.SetCellValue(value);
}
(It's not the cleanest code but I will don refactor after getting this work).
After running into this issue I thought that maybe I will create _cellStyle variable in the constructor and only change it's value depends on the case, because it's assigned to the new cell anyway and I see while debugging that object values are correct.
But after creating everything, it won't get any better. Styles was override by the last value of the style, and dates are spoiled also, but later: screnshoot of excel workbook after creating one instance of cell style
I'm running out of ideas, maybe I should create every combination of the cell styles (I'm using only few data formats and alignments) but before I will do that (because I'm running out of easy options right now) I wonder what you guys think that should be done here.
cell format is set to custom with date type
I am using this code to create my custom style and format. Its for XSSF Format of excel sheet. but it will work for HSSF format with some modification.
XSSFFont defaultFont = (XSSFFont)workbook.CreateFont();
defaultFont.FontHeightInPoints = (short)10;
defaultFont.FontName = "Arial";
defaultFont.Color = IndexedColors.Black.Index;
defaultFont.IsBold = false;
defaultFont.IsItalic = false;
XSSFCellStyle dateCellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
XSSFDataFormat dateDataFormat = (XSSFDataFormat)workbook.CreateDataFormat();
dateCellStyle.SetDataFormat(dateDataFormat.GetFormat("m/d/yy h:mm")); //Replace format by m/dd/yyyy. try similar approach for phone number etc.
dateCellStyle.FillBackgroundColor = IndexedColors.LightYellow.Index;
//dateCellStyle.FillPattern = FillPattern.NoFill;
dateCellStyle.FillForegroundColor = IndexedColors.LightTurquoise.Index;
dateCellStyle.FillPattern = FillPattern.SolidForeground;
dateCellStyle.Alignment = HorizontalAlignment.Left;
dateCellStyle.VerticalAlignment = VerticalAlignment.Top;
dateCellStyle.BorderBottom = BorderStyle.Thin;
dateCellStyle.BorderTop = BorderStyle.Thin;
dateCellStyle.BorderLeft = BorderStyle.Thin;
dateCellStyle.BorderRight = BorderStyle.Thin;
dateCellStyle.SetFont(defaultFont);
//Apply your style to column
_sheetData.SetDefaultColumnStyle(columnIndex, dateCellStyle);
// Or you can also apply style cell wise like
var row = _sheetData.CreateRow(0);
for (int cellIndex = 0;cellIndex < TotalHeaderCount;cellIndex++)
{
row.Cells[cellIndex].CellStyle = dateCellStyle;
}

uwp how to delete a line

public void CreateALine(double x1, double y1)
{
// Create a Line
Line redLine = new Line();
redLine.X1 = x + 20;
redLine.Y1 = y + 20;
redLine.X2 = x1 + 20;
redLine.Y2 = y1 + 20;
SolidColorBrush gBrush = new SolidColorBrush();
gBrush.Color = Colors.Green;
redLine.StrokeThickness = 2;
redLine.Stroke = gBrush;
// Add line to the Canvas
canvas2.Children.Add(redLine);
}
Here is my code, now I want to delete this line, can anyone tell me how to do this?
thanks.
The Children property is nothing more than a UIElementCollection object attached to the Canvas control.
The UIElementCollection object has some methods that allow you to remove objects from itself, which may vary depending on the programming language.
In your case, the Remove and RemoveAt should solve your problem. All you need to do is to hold a reference of the item you want to remove and pass it as a parameter to the appropiate remove method unless you know its index in the collection; in which case you can remove it using that instead.

Monogame windows - make a textview scrollable

I am trying to do an appication in monogame windows. I have a long text to be displayed on screen. I tried to render it on screen using spriteBatch.Drawstring, was succesful to an extent. But, the text did not fit into a required area. I had followed this tutorial. I need a vertical scroll to be implemented to have the entire text inside my desired area. Can anyone suggest some help. This is my current code :
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
_boxTexture = new SolidColorTexture(GraphicsDevice, Color.Red);
_borderRectangle = new Rectangle(100, 100, 500, 500);
_textboxRectangle = new Rectangle(105, 105, 490, 490);
_font = Content.Load<SpriteFont>("Rockwell");
_text = "He determined to drop his litigation with the monastry, and relinguish his claims to the wood-cuting and fishery rihgts at once. He was the more ready to do this becuase the rights had becom much less valuable, and he had indeed the vaguest idea where the wood and river in quedtion were.";
}
private String parseText(String text)
{
String line = String.Empty;
String returnString = String.Empty;
String[] wordArray = text.Split(' ');
foreach (String word in wordArray)
{
if (font.MeasureString(line + word).Length() > textBox.Width)
{
returnString = returnString + line + '\n';
line = String.Empty;
}
line = line + word + ' ';
}
return returnString + line;
}
and inside draw function :
spriteBatch.DrawString(font, parseText(text), new Vector2(textBox.X, textBox.Y), Color.White);
You can do it in the draw method instead.
Then just do what you're doing now, but instead of creating a string that you return, you just call
spriteBatch.DrawString(font, line, textPostion, Color.White);
instead. Where textPosition is just equal to the textbox position, to begin with and then you increase the Y position with font.MeasureString(line).Y for each iteration:
textPosition.Y += font.MeasureString(line).Y;
Then you check for
if(font.MeasureString(line).Y + textPosition.Y < textBox.Y + textBox.Height
|| textPosition.Y > textBox.Y)
{
continue;
}
Then just look for input of the keyboard arrows for instance (or create some buttons for scrolling up and down), and increase or decrease the textPosition.Y accordingly. Then you will have vertically scrolling textbox.
You can then make some lock by defining a minimum Y value for the position, such that the text stop when scrolling to the bottom or to the top.

Cocoa Touch - UISegmentedControl title is not working in IF/ELSE statement

I'm trying to write a basic DST converter. I have a segmented control with 3 choices, their titles (surprisingly) are Distance, Speed and Time. I have 2 input text fields and a calculate button, as well as 2 labels for each text field with the type of measurement required and it's units. Making a selection on the segmented control should update the view accordingly. The variables have all been declared as IBOutlets, #property, #synthesize, and the code sits in an IBAction method, which is connected to the segmented control. The following code does not work, am I missing something completely obvious? (NSLog shows the correct title)
NSString *choice;
choice = [dstChoiceSegmentedControl titleForSegmentAtIndex: dstChoiceSegmentedControl.selectedSegmentIndex];
NSLog(#"Choice |%#|", choice);
if (choice == #"Distance") {
firstLabel.text = #"Speed:";
firstUnitsLabel.text = #"kts";
secondLabel.text = #"Time:";
secondUnitsLabel.text = #"hrs";
answerUnitsLabel.text = #"nm";
} else if (choice == #"Speed") {
firstLabel.text = #"Distance:";
firstUnitsLabel.text = #"nm";
secondLabel.text = #"Time:";
secondUnitsLabel.text = #"hrs";
answerUnitsLabel.text = #"kts";
} else if (choice == #"Time") {
firstLabel.text = #"Distance:";
firstUnitsLabel.text = #"nm";
secondLabel.text = #"Speed:";
secondUnitsLabel.text = #"kts";
answerUnitsLabel.text = #"hrs";
}
Thanks for your help (and I hope it's not some silly error that is staring me right in the face)!
You cannot compare strings this way. You need to do:
[choice isEqualToString:#"Distance"];
But if I were you, I'd check for the indicies instead.
edit: To explain it further: what you're doing with choice == #"Distance" is comparing a pointer with a string, which will not work. You need to call the string objects comparing method as shown above.

Insert text into flex 3 textarea

I have a textArea and a list. When a user double clicks a list item, the label of the selected item should be inserted into the textarea. When a text is selected in the textArea, it should be replaced, otherwise the text just needs to be inserted into the existing text at the caret point.
I've managed to get the text and everything, I just can't manage to insert it at the caret point. Does anyone know how to do this?
It's actually not JavaScript but Adobe Flex 3. Thanks for the help though, it did push me in the right direction. This is the way its done in Flex 3:
var caretStart:int = textArea.selectionBeginIndex;
var caretEnd:int = textArea.selectionEndIndex;
textArea.text = textArea.text.substring(0,caretStart)
+ newText
+ textArea.text.substr(caretEnd);
The accepted answer works great if you do not have existing HTML formatting. In my case, I inserted a new button into the editor that the user could click to put in a key word. I kept losing all HTML formatting until I dug around in the actual class and sided with a TextRange object:
public function keyWord_Click(event:Event) : void
{
var caretStart:int = txtEditor.textArea.selectionBeginIndex;
var caretEnd:int = txtEditor.textArea.selectionEndIndex;
var newText : String = "[[[KEYWORD]]]";
var tf:TextRange = new TextRange(txtEditor,true,caretStart,caretEnd);
tf.text = newText;
}
The nice thing about this approach is, you can also apply conditional formatting to that TextRange object as needed.
You can use txtarea.selectionStart and txtarea.selectionEnd to get Selected text position.
After that, You delete txt and add new selected text.
I don't known much about Javascript, so I wrote it for U.
You can search on google with keywords:
"Javascript Selected Text TextArea"
"Javascript add text at position"
Sample code:
function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
caretPos = doGetCaretPosition(myField);
alert(caretPos);
setCaretPosition(myField,caretPos-3);
}