473,395 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Adding stringcollection to richtextbox text

Hi,
Is there an easy/nice way to assign the contents of a stringcollection to
richtextbox.lines rather than doing the following please?

foreach(string str in _EmailErrors)
{
rtb.Text += str;
rtb.Text += Environment.NewLine;
}

thanks
Claire
Oct 31 '07 #1
7 5533
Not any nicer, but a lot more efficient:

StringBuilder sb = new StringBuilder();
foreach (string s in yourCollection) {
sb.AppendLine(s);
}
rtb.Text = sb.ToString()

You could refactor most of this into a helper function. to avoid
repeating the code.

Marc
Oct 31 '07 #2
On 2007-10-31 03:58:23 -0700, "Claire" <cc@nospam.comsaid:
Hi,
Is there an easy/nice way to assign the contents of a stringcollection to
richtextbox.lines rather than doing the following please?
Marc's example is more efficient than building up the Text property one
string at a time. However, you asked specifically about assigning to
the Lines property.

IMHO, at the very least it is likely to be more efficient, as compared
to the code you posted, to copy the strings to an array explicitly with
a foreach loop, and possibly even more efficient than the StringBuilder
code Marc posted. However, you might find the following simpler to
read as well:

string[] rgstr = new string[_EmailError.Count];

_EmailError.ICollection.CopyTo(rgstr, 0);
rtb.Lines = rgstr;

That does the same basic copying, but without having to write the loop
yourself.

Pete

Oct 31 '07 #3
Thanks to Marc but also thanks Peter. That's what I was looking for :)
I knew it was possible, just couldnt remember the required interface. I can
change my collection type easily enough

Claire
>
string[] rgstr = new string[_EmailError.Count];

_EmailError.ICollection.CopyTo(rgstr, 0);
rtb.Lines = rgstr;

That does the same basic copying, but without having to write the loop
yourself.

Pete

Nov 1 '07 #4
Complete aside, and an implementation detail only - but I looked up
the TextBoxBase.Lines[] setter in reflector... I cite this only
because it made me chuckle ;-p Of course, it also removes the need for
a string-array, but in the grand scheme that isn't really an issue...
On the other hand, it is often useful to know which of two approaches
is the simple get/set, and which is essentially a funtion. In this
case, .Text is the direct approach, and .Lines is the shim.

public string[] Lines
{
get {...} // the reverse...
set
{
if ((value != null) && (value.Length 0))
{
StringBuilder builder = new StringBuilder(value[0]);
for (int i = 1; i < value.Length; i++)
{
builder.Append("\r\n");
builder.Append(value[i]);
}
this.Text = builder.ToString();
}
else
{
this.Text = "";
}
}
}
Nov 1 '07 #5
Lol, you don't surprise me :)
Nov 1 '07 #6
it might surprise Pete, though:
<q>and possibly even more efficient than
the StringBuilder code Marc posted</q>

;-p

Nov 1 '07 #7
On 2007-11-01 08:28:00 -0700, Marc Gravell <ma**********@gmail.comsaid:
it might surprise Pete, though:
<q>and possibly even more efficient than
the StringBuilder code Marc posted</q>

;-p
It doesn't surprise me. That's why I used the word "possibly". I
accepted the possibility that the Lines property is simply a wrapper
and took that into account in my reply.

And even though it turns out that the current implementation is no
different, that does not rule out a possible change in the future. The
fact is that storing all of the text in the control as a single string
array creates performance trade-offs, and it's always possible that in
the future it will be changed to allow better performance for the
line-oriented access methods and properties of the control.

It's well and good that you can inspect the current implementation
based on reflection, but IMHO it's a bad idea to use that information
as a determining factor in implementing one's own code. There's a
reason the implementation is normally opaque: the client of the
implementation shouldn't be depending on the specific implementation.

Finally, I will note that the Lines implementation _is_ more efficient
than the code you posted, even if only slightly. So :p right back
atchya.

Pete

Nov 1 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: kangoo | last post by:
Hi, I'm trying to remove the last charater in a richTextBox. I though richTextBox.Text.Remove(richTextBox.Text.length-1, 1); would work, but it does nothing (eg richTextBox.Text += "some new...
2
by: Dan | last post by:
Hi, Is it possible to iterate through a StringCollection ? I have the following code but "row" always seems to contain the values of rowValues2 Thanks for any input, Danny
8
by: Naz | last post by:
Hi, I am new in C# and converting a program from delphi to C#. I have to store a list of strings in memory and also need to save the strings to disk at the end. I use StringCollection and my...
6
by: Just Me | last post by:
I need to create a library sub that adds event handlers to a control. Public Shared Sub AddEvent(ByVal qq As Control, By?? ControlMouseMove As ??) So I need to pass the address of the handler or...
0
by: Vincent | last post by:
Dear all, I have implemented a class to export the content of RichTextBox to image in WYSISYG mode so that line breaks on the screen are the same as exported. C# Code: public struct...
9
by: James Wong | last post by:
Hi, I use the RichTextBox in my program. It will use different language in this RichTextBox (chinese and english characters), and it set the "DualFont" and use different fonts. By the way, how...
3
by: michael sorens | last post by:
The documentation for the RichTextBox is sketchy at best. I want to do a very simple task but I cannot find information on this. I am using a RichTextBox as an output window. Some text I want to...
0
by: Vimalathithan | last post by:
I just developing a editor. I have provide the options like Bold, Italic, underlin, font change, font size change. These font options are keep in with one toolstripbutton. the toolstripbar keep...
4
by: Adam Right | last post by:
How can i add a collection to another collection ? For example : --------------------------------------------- StringCollection strColl= new StringCollection(); //string series Hashtable...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.