How to change already entered lines in a RichTextBox? - winforms

How does one change already appended or entered lines on the RichTextBox control?
I want to programmaticly insert a Timestamp in front of each line of input. TextBox1.Lines[] does not allow changes. I attempted to set my own array to Lines[] but didn't seems to work.

Use the RichTextBox.GetFirstCharIndexFromLine() method to find out where to insert the text. For example:
int prev = richTextBox1.SelectionStart;
int cnt = richTextBox1.Lines.Length;
for (int line = 0; line < cnt; line++) {
richTextBox1.SelectionStart = richTextBox1.GetFirstCharIndexFromLine(line);
richTextBox1.SelectionLength = 0;
richTextBox1.SelectedText = DateTime.Now.ToString() + ": ";
}
richTextBox1.SelectionStart = prev;

Related

How should I delete part of string from beginning to first space?

I'm using C++/CLI and I was trying to delete part of string from beginning to first space for a long time.
My code that doesn't work is:
String^ ns = gcnew String("Hello world!");
int temp1 = ns->IndexOf(" ");
int temp2 = ns->Length;
for (int i =temp1 +1; i < temp2; i++) {
ns+= ns[i];
}
What is the problem?
Why don't you count where is the first space, then use this function?
str = str->Remove( CoordsStart , CoordsEnd-CoordsStart );
You said from beginning to first space but it isn't what your program seems to be doing.
is :
String^ ns = gcnew String("Hello world!");
int temp1 = ns->IndexOf(" ");
for (int i = 0; i < temp1; i++) {
ns[&] = " ";
}
what you look for ?
Simple is best.
string ns ="Hello world!";
int temp1 = ns.IndexOf(" ") + 1;
ns = ns.Substring(temp1);
//Console.WriteLine(ns);

Items and Subitems in List-View control

I'm want to use a List-View control to display results of an LDAP search in a "grid". I've written some test code to see how it works, but it's not being displayed as I want. As I understand it, each Item is equivalent to a "row" (using LVS_REPORTstyle), and the Subitem is equivalent to a "column" (e.g. for each item I can display a number of subitems, each in a separate column on the same row).
Here's my test code, currently set to create four columns, with a single Item and four Subitems (corresponding to the four columns). Two functions: one to create the columns, the other to insert items.
int CreateColumns(HWND *hwndlistbox)
{
wchar_t *cnames[100];
LVCOLUMN lvc;
int i;
cnames[0] = L"column1";
cnames[1] = L"column2";
cnames[2] = L"column3";
cnames[3] = L"column4";
cnames[4] = NULL;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
for (i = 0; cnames[i] != NULL; i++)
{
lvc.iSubItem = i;
lvc.pszText = cnames[i];
lvc.cx = 100;
lvc.fmt = LVCFMT_LEFT;
ListView_InsertColumn(*hwndlistbox, i, &lvc);
}
return i;
}
void InsertItems(HWND *hwndlistbox, int *columncount)
{
LVITEM lvi;
wchar_t *items[100];
int i, j;
items[0] = L"text1";
items[1] = L"text2";
items[2] = L"text3";
items[3] = L"text4";
items[4] = NULL;
lvi.mask = LVIF_TEXT;
lvi.iItem = 0;
for (i = 0; i < *columncount; i++)
{
lvi.pszText = items[i];
lvi.iSubItem = i;
ListView_InsertItem(*hwndlistbox, &lvi);
}
}
I expect this to generate a single row (lvi.iItem = 0;) with a text string under each column (lvi.iSubItem = i;). This is what it displays instead:
Changing lvi.iSubItem = i to lvi.iSubItem = 0 results in each text string being displayed as a new row in the first column:
I've played around with it, hardcoding the numbers on both iItem and iSubItem, changing both to i, but I can't get it to display the text anywhere other than the first column. What am I doing wrong?
First of all, your cnames and items arrays are declared as array of pointers, but you are not allocating memory for them; you would need to declare them as an array of strings, like wchar_t cnames[100][40];.
Secondly, you need to use ListView_InsertItem to insert an item and set the value for the first column, then use ListView_SetItem to add additional columns, like
lvi.pszText = items[0];
lvi.iSubItem = 0;
ListView_InsertItem(*hwndlistbox, &lvi);
for (i = 1; i < *columncount; i++)
{ lvi.pszText = items[i];
lvi.iSubItem = i;
ListView_SetItem(*hwndlistbox, &lvi);
}
Each row shows a single item so you cannot populate the columns by adding items.
As the documentation says:
"You cannot use ListView_InsertItem or LVM_INSERTITEM to insert subitems. The iSubItem member of the LVITEM structure must be zero. See LVM_SETITEM for information on setting subitems."
The LVM_SETITEM documentation explains how to set the text of a sub-item.

How to add space in textbox until max length reached?

i want to add a space ( ) in text box until the maximum length reached (1000)
and i want to write text in the textbox concat the space so the text is first and after the text finished the space start until max length of textbox reached for example.
like below :
textbox1.text = "mytext" + " ";
but i want the space just to fill the textbox until max length (1000) reached.
and the other thing i want is if the text in the textbox is bigger than the max length
then remove the excess (the text after 1000)
please help
You can use the string.PadRight() method.
textbox1.Text = textbox1.Text.PadRight(textbox1.MaxLength, ' ');
First check if the length is greater than the maximum permitted, if it is then use Substring to cut it down to size. If the length is less than max, then you can use PadRight to pad the text...
string text = textbox1.Text;//get the text to manipulate
int max = 1000;
if(text.Length > max)//If the current text length is greater than max
text = text.Substring(0, max);//trim the text to the maximum allowed
else
text = text.PadRight(max, ' ');//pad extra spaces up until the correct length
//text will now be the same length as max (with spaces if required)
textbox1.Text = text;//set the new text value back to the TextBox
NOTE: Because you have asked about trimming the text to the maximum length, then I have assumed you have not used the MaxLength property of the TextBox - as this would already prevent adding more than the limit, I would recommend using that instead then you don't have to worry about trimming yourself, you could then just do:
textbox1.Text = textbox1.Text.PadRight(textbox1.MaxLength, ' ');
I think that's all you wanted right?
public String writeMax(String myText, int maxLenght)
{
int count = myText.Length;
String temp = "";
if(count >= maxLength)
{
temp = myText.substring(0, maxLength);
}
else
{
for(int i = 0; i < maxLength - count; i++)
{
temp += " ";
}
temp = myText + temp;
}
return temp;
}

Extract text from caret position textarea

I am having trouble to extract text from textarea using WPF code behind.
Example:
Sunny day in London
if the cursor is set on [d*ay] it should return day.
* for the cursor.
Any help will be appreciated.
This seems to work but I am not sure how you want it to behave when the caret is in the middle of whitespace. As is, it basically returns the nearest token that is touching the caret. For example, the phrase "Sunny day in London" has four tokens: "Sunny", "day", "in", and "London".
string selection;
if (txtBox.Text.Length > 0)
{
int startIndex = 0;
for (int i = txtBox.CaretIndex - 1; i >= 0; i--)
{
if (String.IsNullOrWhiteSpace(txtBox.Text[i].ToString()))
{
startIndex = i;
break;
}
}
int length = txtBox.Text.Length - startIndex;
for (int i = startIndex; startIndex + i <= txtBox.Text.Length - 1; i++)
{
if (String.IsNullOrWhiteSpace(txtBox.Text[startIndex + i].ToString()))
{
length = i;
break;
}
}
selection = txtBox.Text.Substring(startIndex, length);
}
else
{
selection = "";
}

Loop Iteration Efficient

You have a loop that iterates over 1,000 items. You want to add a newline to every four items. The items are in an array structure that have numeric index starting at 0. How do you do it?
FWIW:
for (int i = 0; i < list.size(); ++i) {
// you want to 'do it' with list[i] here
if (0 == (i+1)%4))
{
// 'you want to add a new line' here
}
}
Just in case what you are really trying to ask is "How do I print these items, four to a line?" here's one way
int nOnLine = 0;
for (i = 0; i < 1000; i++){
// print item i
nOnLine++;
if (nOnLine >= 4){
// print newline
nOnLine = 0;
}
}
if (nOnLine > 0){
// print newline
nOnLine = 0;
}
for (int i = 0; i < list.size(); i += 4) {
// add to the item
}
The above iterates over every fourth item instead of every single item.
for(i=3;i<len;i=i+4) { // where len is the length of your array
ary[i]+='\n'; // use string append operator of your language.
}
which will add a newline to every fourth item, i.e. items 3, 7, 11, etc.
EDIT
Changed to fulfill the OP's criteria.

Resources