i have textbox with text:
1234 YYMM 1057316895 12, AB 6386 ABC
where YYMM is in this case Year and Month. What i'd like to do is to search if in this textbox exist YYMM, and highlight this part of text, or somehow show that in this specified textbox exist not fully completed field.
So when i rewrite this string with 1203 instead of YYMM error will not be received.
And! This YYMM can be in any place of string in textbox, so i can't do something like
if (textbox1.Text.Substring(x,4)=="YYMM) {}
where x is index of YYMM location.
Tnx
Here a sample pseudocode that can help you; (Put in your validating event)
int pos = textbox1.Text.IndexOf("YYMM");
if(pos != -1)
{
textbox1.SelectionStart = pos;
textbox1.SelectionLength = 4;
// MessageBox("Error");
}
This will assign the start index and the length of the selection, but it will not make it visible. To ensure visibility, I would recommend to add
textbox1.ScrollToCaret();
textbox1.HideSelection = false;
Related
I have a TextBoxnamed PercentageText. I used TextChanged event to Append "%" to text typed inside the TextBox. The code inside the TextChanged event is given below
if (skipTextChange)
skipTextChange = false;
else
{
skipTextChange = true;
if (PercentageText.Text =="")
{
PercentageText.Text = " ";
}
if (PercentageText.TextLength == 1)
{
if (PercentageText.Text != "%")
{
PercentageText.Text =""+ PercentageText.Text.Trim() + "%";
}
}
}
and initiallized SkipTextChange=false; out side the TextChanged Event Block. My Problem is When I Type Anything the first character goes all the way to the end of the text, for an example, if I type 152 it Shows 521 and When I cleared the TextBox using keyboard(Back Space key), and Type again it works Perfactly.
Instead of going for all this troubles I suggest you to simply add a label to the right of the textbox and put a % as the label's text.
However, if you really want to go for the TextChanged path then you need to test if your input ends with the % char and add it only if not. Also you need to set the position where the next char should be typed.
if (skipTextChange)
skipTextChange = false;
else
{
skipTextChange = true;
if (PercentageText.Text == "")
{
PercentageText.Text = " ";
}
if (!PercentageText.Text.EndsWith("%"))
{
PercentageText.Text = "" + PercentageText.Text.Trim() + "%";
PercentageText.SelectionStart = PercentageText.TextLength - 1;
}
}
Consider to test extensively with this approach. Copy/Paste, Delete and BackSpace behavior should be verified and insertion of multiple spaces or with the case of a % char typed directly by the user. Of course, if this textbox is supposed to contain only numbers a more complex verification code is required. If this is the context then I suggest to use NUmericUpDown control and the label trick to its right.
i have a Trackbar and want it to add the Current Value to a richtextbox Text without replacing the whole Text Line
richTextBox1.Rtf = richTextBox1.Rtf.Replace("aimbot_aimtime=85.000000", "aimbot_aimtime=" + trackbarpercent.Text + ".000000");
(i get the Value from my Label)
Thats what im using right now but it only Replaces it if the Text is "aimbot_aimtime=85.000000"
i want it to add the new Value after "aimbot_aimtime=NEWVALUE" but i cant get it to work atm
#Marc Lyon
I think a better way for me is to Replace the Line itself cause its always Line 7
Got it working, thanks to all who helped :)
void changeLine(RichTextBox RTB, int line, string text)
{
int s1 = RTB.GetFirstCharIndexFromLine(line);
int s2 = line < RTB.Lines.Count() - 1 ?
RTB.GetFirstCharIndexFromLine(line + 1) - 1 :
RTB.Text.Length;
RTB.Select(s1, s2 - s1);
RTB.SelectedText = text;
}
private void trackbarpercent_Click(object sender, EventArgs e)
{
changeLine(richTextBox1, 7, "aimbot_aimtime=" + aimtimetrackbar.Value + ".000000");
}
You have to know what the value is in order to replace it, which is why it only works when the value is your default value of 85.
In order to replace the text with the new text, you will have to track the previous value somewhere to use in your replacement. This means a field in your form, a property in some class. Let's say you create an int field on your form (myForm) called oldAimbot_aimtime. Every time the slider changes, put old value into this field. now your code becomes:
var prompt = "aimbot_aimtime=";
var oldvalue = string.Format("{0}{1}", prompt, myForm.oldAimbot_aimtime);
var newvalue = string.Format("{0}{1}", prompt, {NEWVALUE}.Format("#.######");
richTextBox1.Rtf = richTextBox1.Rtf.Replace(oldvalue, newvalue);
This code is off the top of my head and may not work exact, but it should replace the value. What is the value of using a richtextbox on a config screen? Can you post a screenshot?
OK, I see the screenshot. Ethics aside (not sure there is such a thing as a legit aimbot). You are using the richtextbox presumably because it was the easiest control for you to style...
Where you use the richtextbox is probably better suited to a GridView, ListBox, maybe even a treeview where you have finer control over each element.
If you want to use the richtext, write code which emits each option, then you can obtain exact values to use in rtf.Replace()commands
Hope this helps.
I'm using a RichTextBox for coloured text. Let's assume I want to use different colours for different portions of the text. This is working fine so far.
I'm currently having a problem with the SelectionStart property of the RichTextBox. I've set some text to the Text property of the RichTextBox. If the text contains \r\n\r\n the SelectionStart Position won't match the position of characters with the assigned String.
Small example (WinformsApplication. Form with a RichTextBox):
public Form1()
{
InitializeComponent();
String sentence1 = "This is the first sentence.";
String sentence2 = "This is the second sentence";
String text = sentence1 + "\r\n\r\n" + sentence2;
int start1 = text.IndexOf(sentence1);
int start2 = text.IndexOf(sentence2);
this.richTextBox1.Text = text;
String subString1 = text.Substring(start1, sentence1.Length);
String subString2 = text.Substring(start2, sentence2.Length);
bool match1 = (sentence1 == subString1); // true
bool match2 = (sentence2 == subString2); // true
this.richTextBox1.SelectionStart = start1;
this.richTextBox1.SelectionLength = sentence1.Length;
this.richTextBox1.SelectionColor = Color.Red;
this.richTextBox1.SelectionStart = start2;
this.richTextBox1.SelectionLength = sentence2.Length;
this.richTextBox1.SelectionColor = Color.Blue;
}
The RichTextBox looks like this:
As you can see, the first two characters of the second sentence are not coloured. This is the result of an offset produced by \r\n\r\n.
What is the reason for this? Should I use another control for colouring text?
How do I fix the problem in a reliable way? I've tried replacing the "\r\n\r\n"with a String.Empty, but that produces other offset problem.
Related question:
Inconsistent behaviour between in RichTextBox.Select with SubString method
It seems that the sequence \r\n counts for one character only when doing selections. You can do the measurements in a copy of the string where all \r\n are replaced by \n.
Just for completeness (I'll stick to linepogls answer for now):
I've found another way to get indices for the SelectionStart property. The RichTextBox offers a Find method, that can be used to retrieve index positions based on a specified string.
Be aware of the fact, that the text you want to highlight might not be unique and occur multiple times. You can use an overload to specify a start position for the search.
I have two Multi-Line Text Boxes and one arrow button in my application and what I want is that when a user selects any one or many lines from Multi-Line textbox 1 ,it should update the status of that line from 0 to 1 and then I want to load the rows whose status is 1 into Multi-Line textbox 2.I have tried but didn't know what should I do next?
Code:
for (int i = 0; i < txtNewURLs.Lines.Length; i++)
{
if (txtNewURLs.Lines[i].Select)
{
}
}
Can any body please help me or give some suggession to do this task?
Assuming you are using a Multiline TextBox similar to MSDNS's How to: Create a Multiline TextBox Control, you can utilize the SelectedText property to retrieve the text that the user has selected. The lines will be separated by \r\n
i.e.
If I have the below (inbetween the page lines):
test0
test1
And I selected lines test0 and test1, then SelectedText would be test0\r\ntest1.
You could then split on the \r\n and retrieve each selected line.
// Retrieve selected lines
List<string> SelectedLines = Regex.Split(txtNewURLs.SelectedText, #"\r\n").ToList();
// Check for nothing, Regex.Split returns empty string when no text is inputted
if(SelectedLines.Count == 1) {
if(String.IsNullOrWhiteSpace(SelectedLines[0])) {
SelectedLines.Remove("");
}
}
// Retrieve all lines from textbox
List<string> AllLines = Regex.Split(txtNewURLs.Text, #"\r\n").ToList();
// Check for nothing, Regex.Split returns empty string when no text is inputted
if(AllLines.Count == 1) {
if(String.IsNullOrWhiteSpace(AllLines[0])) {
AllLines.Remove("");
}
}
string SelectedMessage = "The following lines have been selected";
int numSelected = 0;
// Find all selected lines
foreach(string IndividualLine in AllLines) {
if(SelectedLines.Any(a=>a.Equals(IndividualLine))) {
SelectedMessage += "\nLine #" + AllLines.FindIndex(a => a.Equals(IndividualLine));
// Assuming you store each line status in an List, change status to 1
LineStatus[AllLines.FindIndex(a => a.Equals(IndividualLine));] = 1;
numSelected++;
}
}
MessageBox.Show((numSelected > 0) ? SelectedMessage : "No lines selected.");
Hi I was wanting to display the number of characters entered into a textbox and I want it to update as I type how can I go about this?
Here is what I have:
int kk = textBox1.MaxLength;
int jj = //This is what I need to know.
string lol = jj.ToString() + "/" + kk.ToString();
label2.Text = lol;
How about
int jj = textBox1.Text.Length;
Or am I missing something?
The text of the text box will be a string, so it has a Length property, i.e.:
textBox1.Text.Length
TextBoxobject.Text.Length will give you the length of textbox value.
you can use the OnTextChanged event of the Textbox on the client side in Javascript and compute the increment from.