Determine what line user is editing in textbox - c#

I want to be able to determine what line the user is editing in a multi-line text-box.
So could anyone suggest how i get the line number that the user is editing.
Also, is it possible to have textboxes side by side which when the user takes a new line in one, we can programatically take a new line on the other one?

You can use:
label_currentLine.Text = Textbox1.GetLineFromCharIndex(Textbox1.SelectionStart) + 1
Plus 1 because index starts at 0.

Related

How can I know the index of duplicated text from a selectedtext? [duplicate]

Ok, I'm trying to do something a little specific here. I want to get the location of the selected text in a textbox.
To elaborate- I can use location to select text. If I have a textBox1 I could do:
textBox1.SelectionStart = 1;
textBox1.SelectionLength = 4;
That would start at the second letter and select 4 letters.
What I want to do is the opposite: when the user selects text, I want to find out what the start is and what the length is (or what the start is and what the end is. Either will work).
I thought about just searching the string for the selectedtext (textBox1.SelectedText). The problem comes if it is a common word or a string that is used multiple times. For instance.
This is a cat. This is a cat. This is a cat.
If they select the second sentence, using SelectedText to search the string for that specific sentence does me no good. It could be either of the 3.
So, my question is: When the user clicks a button, how do I determine the exact elements that are selected by the user, so that I can later manipulate those specific elements? Important to note the later part- I likely will not only want to manipulate the text when the button is pressed. I will also want to manipulate it later, at a time when the text may no longer be highlighted. This means I'll want to store SOMETHING to tell me what specific parts of the sentence I'm dealing with. If that solution isn't viable, is there a solution you can think of where, in the above "this is a cat" example, the user could select the second sentence, hit a button, and then later I know which sentence was selected when he hit that button?
According to the documentation, SelectionStart and SelectionLength can be both set and read. Just use those.
You dont even need to know the position of selected text to manipulate them, to edit the text that you have selected in the text you can simple set the SelectedText property to the new edited value.
// if textBox1.text = "Hello World World"; with first "World" selected
textBox1.SelectedText = textBox1.SelectedText.Replace("World", "Raj");
// then it becomes "Hello Raj World"

How to check if/else statement for multiple textboxes and variables?

I have a Windows form with 5 textboxes on it. In these textboxes the user can fill in 3 values (500,555 or 610). Every value needs to be multiplied with a constant. For every value there is another constant. Below is my code for one textbox (txtSectie1).
In stead of copying this code 5 times (for every textbox) I think there is a way to use a loop so I don't have to copy the code 4 times, but I can't figure it out. For each of the 5 text-boxes I want to store a value in a variable (dblGewichtPaneel1 to dblGewichtPaneel5). All the textboxes and variables have the same name, only the last character changes from 1 to 5.
Solution 1:
Put the entire code in to a method which accepts TextBox and Panel as input parameters.
Put all your textboxes and panels in a list and then run a loop on the list, repeatedly calling the method created in point 1.
Solution 2:
Run a loop 1 to 5, wrapping the above code.
Find the controls using
Textbox control = (TextBox) this.Controls["txtSectie" + i]; //this being Form or container control
if(control!=null)
{
//do work
}

C# - How to deselect table cell in Word

I'm currently trying to produce the opposite effect of the Selection.SelectCell method.
Here is my code:
public void getCellContent()
{
Word.Selection currentSelection = Globals.ThisAddIn.Application.Selection;
currentSelection.SelectCell();
newKey = currentSelection.Text; //global variable
currentSelection.Collapse();
}
Collapse Method set the cursor to the start of the cell.
Even if I set Collapse direction parameter to the end, the cursor going to the next cell.
My purpose is to be able to save content of a word table cell each time I type up in it (wihtout cell changing).
best regards,
Chefty.
You won't be able to set the cursor at the end of a text inside the cell of a Word table. Not without moving from a cell to another.
The only way to do that would have been using the following code:
currentSelection.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
currentSelection.MoveLeft(Word.WdUnits.wdCharacter, 1);
Don't do it. Given the fact this would get called each time you type a character, it won't be fast and efficient enough and you will end up typing characters in the next cell.
You should save the content of your cell just once, when you change the selection on your own. When you type your first character, use the following code :
currentCell = currentSelection.Range.Cells[1]; //index always starts at 1, not 0
And when you click on another cell (you will maybe have to use Win32 API to catch such an event), don't forget to do :
currentCell.Select();
string currentCellContent = currentSelection.Text;
After that, you still end up with a selected cell, and still need to use currentSelection.Collapse(), but at least you got the content of your cell, and this by performing one save per edit.

textboxcell on datagridview allow only one char by default

I have a datagridview with textboxcell. When I enter the cell and type a string it only shows the last char of the string. It replaces the last char with the new char.
In order to write a complete string I need to click on other cell of any other part in the form, and then double click the textbox cell again and I can write a whole string.
Does anyone have an idea why is that?
I recently ran into this behavior due to executing datagridview1.CommitEdit(DataGridViewDataErrorContexts.Commit) in the grid's CurrentCellDirtyStateChanged event handler. Any time I typed, it would only display one character, and I'd have to click out of the cell and back in to add additional text beyond the 1st character.
In my case, I'd added this code a while back to solve a problem that no longer existed, and had forgotten to remove it. Removing it resolved this behavior.

Masked TextBox how to not show underlines in text box and allow user to enter 3 digits

I am having some troubles with a masked textbox.
How can I hide those underlines which are shown in the textbox by default?!
Also I want the user be able to enter up to 3 digits (he/she can enter atleast 1 and maximum 3 digits). how to set that?
Set empty space(leave one space) in Promptchar property of masked textbox.
and second answer you already get. or you can put validation on Leave event for that.
I think you'd be better off using a regular TextBox for this. Setting it to allow only digits and max 3 digits are done with properties. Then you just have to check manually that at least one digit exists.

Categories

Resources