Errors in extreme adventure programs in C #?

I'm trying to work my way through Ron Jeffries Extreme Programming Adventures in C #. However, I am stuck in Chapter 3 because the code does not, and cannot do what the author says it does.

Basically, the text says that I should write the text in the textbox with the ad included. If you then move the cursor to an intermediate line and press Enter, the code should redisplay the lines before the cursor, add a couple of lines and a set of HTML paragraph tags, and then add the rest of the lines. The code doesn't match the text because it uses the textbox.lines property. Well, no matter how many lines enclosed in text there are in the text box, there is only one line in the Lines property until you hit the carriage return. So, the statement that the code should "Copy the rest of the lines to the clipboard" seems wrong to me.

I would appreciate if I have experience with a book telling me what I am reading or doing wrong!

Thank.

Eoraptor

0


source to share


3 answers


Try direct email to Ron Jeffries. I have a book - somewhere, but I don't remember that it didn't work. His email address is ronjeffries at acm dot org and puts [Ron] in the subject line.



(And for those wondering - his email was straight from his Welcome page )

+1


source


I also just started this book and had the exact same problem, although the code you included looks further away than where I am. The "index out of range" happened for two reasons: first, Ron explains that he was just testing, and therefore returned a solid value of 3, before he wrote the CursorLine () function, which means I think at least 4? lines of text that you say should be inserted or maybe set text to that value before starting, also as you say they should have carriage returns in order for txtbox.Lines to return an array of strings. The second reason comes up even after CursorLine () has been implemented, but only happens when the textbox is empty as txtbox.Lines returns string [0], but I think Ron implements "User Story" which saysthat when text has been entered and user taps enter, so not sure if he will fix it later, but probably will!

The author states that they are learning C # and will show the wart and everything, which is one of the reasons I decided to study this book as I think it encourages me to develop projects. I'm also trying to do the code first before looking at his solutions to see if I think the same, but maybe I know C # is a little better than I credit myself, or I'm totally shit, but I noticed a few of things, at first he says that overriding OnKeyDown () doesn't work, but I think he must have gotten confused and tried to do it in the Form, instead of fetching from the TextBox and overriding there. This was my code when reading the "User Stories":

int curPos = txtbox.SelectionStart;
string Wrd = Environment.NewLine + "<P></P>" + Environment.NewLine;              
txtbox.SelectedText = Wrd;
int pl = Environment.NewLine.Length + 3; // "<P>" length is 3
// Put text cursor inbetween <P> tags
txtbox.SelectionStart = curPos + pl;

      

It works differently with Ron's code, but it was my interpretation of the User Stories and wasn't sure how to proceed if the text is selected or could be split into a line if the text cursor is in the middle, etc.

Also in the "My Adventures" section in extreme adventure programs in C #

txtbox.GetLineFromCharIndex(txtbox.SelectionStart)

      



gets the position of the cursor line and doesn't matter if the caret doesn't return or change, as far as I can tell, I did a little test with:

txtbox.GetLineFromCharIndex(txtbox.TextLength)

      

which returns the total number of rows that will change when the textbox is resized.

With C #, I'm always looking for solutions that already exist and people can use that for me, but I think MS has created a great language with great components that do what you expect them to do, so no need to re-create the wheel everytime. Although, as I said at the beginning of this book, and maybe these simple solutions are not extensible enough, and maybe Ron takes that into account, although he only mentioned getting it to work and then worry about it later rather the XP way.

Warren.

+1


source


print("using System;

      

using System.Collections; using System.Collections.Generic; using System.Text;

namespace NotepadOne {

public class TextModel {

private String[] lines;
private int selectionStart;
private int cursorPosition;

public TextModel() {

}

public String[] Lines {
  get {
    return lines;
  }
  set {
    lines = value;
  }
}

public int SelectionStart {
  get {
    return selectionStart;
  }
  set {
    selectionStart = value;
  }
}

public int CursorPosition {
  get {
    return cursorPosition;
  }
  set {
    cursorPosition = value;
  }
}

public void InsertControlPText() {
  lines[lines.Length - 1] += "ControlP";
}

public void InsertParagraphTags() {
  int cursorLine = CursorLine();
  String[] newlines = new String[lines.Length + 2];
  for (int i = 0; i <= cursorLine; i++) {
    newlines[i] = lines[i];
  }
  newlines[cursorLine + 1] = "";
  newlines[cursorLine + 2] = "<P></P>";
  for (int i = cursorLine + 1; i < lines.Length; i++) {
    newlines[i + 2] = lines[i];
  }
  lines = newlines;
  selectionStart = NewSelectionStart(cursorLine + 2);
}

private int CursorLine() {
  int length = 0;
  int lineNr = 0;
  foreach (String s in lines) {
    if (length <= SelectionStart && SelectionStart <= length + s.Length + 2) {
      break;
      length += s.Length + Environment.NewLine.Length;
      lineNr++;
    }
    lineNr++;
  }
  return lineNr;
}

private int NewSelectionStart(int cursorLine) {
  int length = 0;
  for (int i = 0; i < cursorLine; i++) {
    length += lines[i].Length + Environment.NewLine.Length;
  }
  return length + 3;
}

      

}} ");

The InsertParagraphTags method is invoked by pressing the enter key in a text box.

By the way, the break here is that there is a below range index error if you try to hit enter at the end of the text. I'm sure I can figure out how to get around this, but then my code won't look like his; and this is what I am trying to learn.

Randy

0


source







All Articles