开发者

TextBox.Lines property, signature is read write but I get read only and no helpful errors

开发者 https://www.devze.com 2023-04-12 14:40 出处:网络
In my quest to smoothly update TextBox characters programmatically, I happened upon the TextBox.Lines property. From its signature

In my quest to smoothly update TextBox characters programmatically, I happened upon the TextBox.Lines property. From its signature

[LocalizableAttribute(true)] 
public:
property array<String^>^ Lines {
    array<String^>^ get ();
    void set (array<String^>^ value);
}

it appears to offer read-write access, however when I update a single line I don't see the update apply within the debugger nor on-screen. Why does MicroSoft publish misleading headers when it could simply omit the set accessor from the property declaration?

The MSDN entry http://msdn.microsoft.com/en-US/library/system.windows.forms.textboxbase.lines%28v=VS.80%29.aspx offers some confusing advice:

NoteNote

By default, the collection of lines is a read-only copy of the lines in the TextBox. To get a writable collection of lines, use code similar to the following: textBox1.Lines = new string[] { "abcd" };

But that is not getting any lines at all but instead is setting them. Does that code snippet have further significance in allowing individual lines to be updated? If so, what am I missing? EDIT Jon, here's how I thought I had updated it

for ( int counter = 0; counter < lineStrideInBytes - NEWLINECHARCNT; counter++ )
{
    int srcI = LineBeingWrittenI * (lineStrideInBytes) + counter;
    replacedLine->Append(TextBoxStr[srcI]);
}
repl开发者_如何转开发acedLine->AppendLine(); //NEWLINECHARS
replacedLine[col] = chr; //Updated char
textBox1->Lines[LineBeingWrittenI] = replacedLine->ToString();


Basically when you read the property, it will make a copy. If you change the returned array, that won't do anything in itself but you can always set the value back. For example (C#):

string[] lines = textBox.Lines;
lines[5] = "Updated!";
textBox.Lines = lines;

The important thing here is that you're writing to the property. Remember that a property is just a getter/setter pair; it's calling the setter that changes the lines in the text box.


Think of it this way; look more closely at the signature... what is that property? an array of strings. That array is read/write.

But as Jon notes, when you read it, you are getting a copy of it, so setting one line as you are doing basically has no effect. You need to set the whole array, after altering the copy.

0

精彩评论

暂无评论...
验证码 换一张
取 消