Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have letters, symbols and numbers. Each one is in its own label. I have a click event on each label which adds whatever is in the label to a multi line textbox.

The problem is no matter where the caret is, when I edit the text by deleting something, and then click a label to add a new item to the textbox, it always puts it at the end of the last line rather than where the caret is.

I know this is because I'm telling it to through the click event like this:

private void lblCbore_MouseClick(object sender, MouseEventArgs e)
        {
            txtFCF.Text += lblCbore.Text;
        }


How can I tell it to place the item I click where the caret is, instead of at the end of the last line?

What I have tried:

All I've done is use the multiline textbox. I didn't expect it to not work as a single line textbox. I've searched google, and on here for an answer.
Posted
Updated 29-May-19 2:13am
v2
Comments
CHill60 28-May-19 11:10am    
When you say "Edit it" do you mean manually type into it or do you mean programmatically. If the latter you will need to show us the code
Smeezy 28-May-19 11:14am    
Programmatically. I was editing my comment to show the code as you replied. I realized why it was happening right after I submitted my question. I'm still not sure how to correct it though. I need to tell it to insert my item wherever the caret is.

It's pretty simple: the Caret position in a textbox is given by the SelectionStart property:
C#
private void MyButton_Click(object sender, EventArgs e)
    {
    int pos = myTextBox.SelectionStart;
    string str = "The new text. ";
    myTextBox.Text = myTextBox.Text.Insert(pos, str);
    myTextBox.SelectionStart = pos + str.Length;
    }
 
Share this answer
 
Comments
Smeezy 28-May-19 12:05pm    
I did try both the code you provided, and also this,

private void lblY_MouseClick(object sender, MouseEventArgs e)
{
txtFCF.Text = txtFCF.Text.Insert(txtFCF.SelectionStart, lblY.Text);
};

They both worked the same way. The caret however reverts to the end of the last line after I delete something, rather than staying where the item was deleted. Is there a way to prevent that?
OriginalGriff 28-May-19 12:16pm    
You have to remember that strings are immutable: the only way to add or remove text from a string is to create a new one and use that.

But the caret is controlled by the StartPosition property, and unless you specifically set it - as my code does - it will always be placed at the end of the string when you set the Text property. So save it first - as I did - and set it after - as I did.
Smeezy 28-May-19 12:25pm    
I have no doubt that you know what you're talking about, and are obviously light years ahead of me on this, but as you can see below, I did put your code in, and it's working exactly the same as the single line I commented out above each of your code sets. When I delete an item, the caret goes to the end of the last line.

private void lblLeft_MouseClick(object sender, MouseEventArgs e)
{
//txtFCF.Text = txtFCF.Text.Insert(txtFCF.SelectionStart, lblLeft.Text);

int pos = txtFCF.SelectionStart;
string str = lblLeft.Text;
txtFCF.Text = txtFCF.Text.Insert(pos, str);
txtFCF.SelectionStart = pos + str.Length;
}

private void lblMid_MouseClick(object sender, MouseEventArgs e)
{
//txtFCF.Text = txtFCF.Text.Insert(txtFCF.SelectionStart, lblMid.Text);

int pos = txtFCF.SelectionStart;
string str = lblMid.Text;
txtFCF.Text = txtFCF.Text.Insert(pos, str);
txtFCF.SelectionStart = pos + str.Length;
}

private void lblRight_MouseClick(object sender, MouseEventArgs e)
{
//txtFCF.Text = txtFCF.Text.Insert(txtFCF.SelectionStart, lblRight.Text);

int pos = txtFCF.SelectionStart;
string str = lblRight.Text;
txtFCF.Text = txtFCF.Text.Insert(pos, str);
txtFCF.SelectionStart = pos + str.Length;
}
OriginalGriff 28-May-19 12:34pm    
None of that code deletes anything!
So look at your "delete" method, and use the SelectionStart.
Smeezy 28-May-19 12:39pm    
Sorry, I might have said it in a confusing way. It's not deleting anything. When I delete, for example the letter "A" from line two, the caret automatically jumps to the end of the last line, whether that's line 3 or 4. Can that be prevented? Other than that, it's working as it should.
txtFCF.Text += lblCbore.Text;

That's not how you "insert"; you're explicitly "appending".

Consider this:

How do I find the position of a cursor in a text box? C# - Stack Overflow[^]
 
Share this answer
 
Comments
Smeezy 28-May-19 11:26am    
I just tried this, but it's not working

txtFCF.Text += txtFCF.Text.Insert(txtFCF.SelectionStart, lblFinish.Text);

Each item I click concatenates to form what's referred to as a "feature control frame".

It works as needed if I use just one line, but would be more useful if I could get this working.
Smeezy 28-May-19 11:32am    
OK, I think I just figured it out. I need to eliminate the += and just use =. Is that correct?
CHill60 28-May-19 11:39am    
No - you will overwrite the text - see Solution 2 - use Text.Insert
Smeezy 29-May-19 8:14am    
I surely appreciate your help OriginalGriff, but if I knew what code was giving me the problem, and why, I wouldn't need to ask for help. Originally my question was simply to ask about the multiline textbox. I’m not familiar with it at all. I didn’t know if this is just how it works. I’m not a professional either. I know just enough to get by, and this is a personal use item to make my real job more efficient. This is basically all of my code. There are about 40 similar statements like the first two. I only posted two to condense it. I’m going to try what you laid out for me. In the meantime, can you tell me where my problem is in this code? Even if I get your code to work as I need, I’d like to know what I did wrong in mine so I can learn something here. I don’t expect it to be done for me, that’s not going to help me learn. The only things missing from this are the close button, and a newline button, both of which work fine. I thought they would just clutter things. So my problem has to be somewhere in the code below.

private void lblCbore_MouseClick(object sender, MouseEventArgs e)
{
txtFCF.Text = txtFCF.Text.Insert(txtFCF.SelectionStart, lblCbore.Text);
}

private void lblDiameter_MouseClick(object sender, MouseEventArgs e)
{
txtFCF.Text += lblDiameter.Text;
}

private void btnCopy_Click(object sender, EventArgs e)
{
if(txtFCF.Text == "")
{
//this is just to prevent an error. No message required.
}
else
Clipboard.SetText(txtFCF.Text);
txtFCF.Text = "";
txtFCF.Focus();
}

private void btnBackspace_Click(object sender, EventArgs e)
{
if(txtFCF.Text == "")
{
//this is just to prevent an error. No message required.
}
else
txtFCF.Text = txtFCF.Text.Remove(txtFCF.Text.Length - 1, 1);
}
// An explanation of the below code. Prior to finding this solution through a search, the caret would revert to the beginning of the first line every time I entered text. It would put the entered text at the end, but the caret would remain at the beginning. Still though, if I deleted any letter, it would add the next letter to the end of the last line (same problem I’m having now). All the code below did was prevent the caret from staying at the beginning of the first line.
private void txtFCF_TextChanged(object sender, EventArgs e)
{
txtFCF.SelectionStart = txtFCF.Text.Length;
txtFCF.SelectionLength = 0;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900