I am trying to do very simple thing which should work but unfortunately its not working.
I have a RichTextBox component on my Winform.
I am trying to change text value of some lines of the RichTextBox but its not changing the value. Here is my code:
private void button1_Click(object sender, EventArgs e)
{
for(int i=0; i < richTextBox1.Lines.Length; i++)
{
if(richTextBox1.Lines[i] == "ok")
{
richTextBox1.Lines[i] = "Done";
}
}
}
I put break point and I notice that it executes
richTextBox1.Lines[i] = "Done";
But it does not change the value at all.
Can anyone explain it? Why its not modifying the value?
Is there way to change/modify the value as per Line?
Thanks and Regards
According to MSDN (TextBoxBase.Lines Property):
By default, the collection of lines is a read-only copy of the lines in the TextBox. To get a writable collection of lines, use code similar to the following: textBox1.Lines = new string[] { "abcd" };
so you better go for:
for (int i = 0; i < richTextBox1.Lines.Length; i++)
{
if (richTextBox1.Lines[i] == "ok")
{
string[] lines = richTextBox1.Lines;
lines[i] = "done";
richTextBox1.Lines = lines;
}
}
UPDATE: another way of doing this (that I do not recommend though):
string line = richTextBox1.Lines[i];
richTextBox1.Find(line);
richTextBox1.SelectedText = "done";
Related
I am trying to make a suggestion in a textbox based on the input. I am trying to input a number (50 for example) and make the textbox suggest what I am trying to type. I have the following data contained in vars.str variable:
AU-60-ST-F
AU-60-CA-F
AU-61-ST-F
Here is the code that I am using
private void textBox2_TextChanged(object sender, EventArgs e)
{
AutoCompleteStringCollection col2 = new AutoCompleteStringCollection();
//
for (int i = 0; i < vars.str.Count; i++)
{
if(vars.str[i].IndexOf(textBox2.Text) != -1)
{
label1.Text = "TRUE";
string match = Regex.Replace(vars.str[i], #"-[a-zA-Z][a-zA-Z]-[MF]", "");
col2.Add(match);
}
}
textBox2.AutoCompleteCustomSource = col2;
}
The suggestion should be AU-60 if I put 60 in the textbox, AU-61 if I put 61 in the textbox
The code seems to work - my testing label changes to TRUE but I do not get any suggestions below the textbox. Any ideas?
I have created two string arrays from a text file and populated a combobox with array1. What I would like to understand is, how do I get a textbox to show the index of array2 that matches the selected index of the combobox (array1)?
I thought something like this may work:
if(phoneComboBox.Text == cPhone[index])
{
nameTextBox.Text = cName[index]; //show equal index item to cPhone/phoneComboBox
}
But that doesn't seem to work. I've also tried a foreach loop, maybe I'm just doing it wrong. I have the reading of the text file and arrays in the window_loaded event and don't know if that's the issue. I've seen SelectedIndexChanged event mentioned a lot in similar questions, but I do not have that event to use, just SelectionChanged.
Can someone please point me on the right track with this? I know arrays may not be the best use here, but they are what I have used, so please help me to understand it properly.
This is how I've read the arrays:
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
//read file on start
int counter = 0;
string line;
StreamReader custSR = new StreamReader(cFileName);
line = custSR.ReadLine();
while (line != null)
{
Array.Resize(ref cPhone, cPhone.Length + 1);
cPhone[cPhone.Length - 1] = line;
counter++;
line = custSR.ReadLine();
Array.Resize(ref cName, cName.Length + 1);
cName[cName.Length - 1] = line;
counter++;
line = custSR.ReadLine();
phoneComboBox.Items.Add(cPhone[cPhone.Length - 1]);
}
custSR.Close();
/*string changeAll = string.Join(Environment.NewLine, cPhone);
string allOthers = string.Join(Environment.NewLine, cName);
MessageBox.Show(changeAll + allOthers);*/
//focus when program starts
phoneComboBox.Focus();
}
In the if you are comparing text strings, so you should use the function ".Equals" in stead of
"=="
if(phoneComboBox.Text.Equals(cPhone[index]))
{
nameTextBox.Text = cName[phoneComboBox.SelectedIndex];
}
You don't need to check the condition, just get the selected index:
nameTextBox.Text = cName[phoneComboBox.SelectedIndex];
and in WPF you have SelectionChanged.
SelectedIndexChanged is for winform.
Best Practice:
But I would suggest you a better way to achieve this using Tag property. You wont have to change a lot of code for it.
//Create "ComboBoxItem" instead of "array"
while (line != null)
{
//initialize
ComboBoxItem cmItem = new ComboBoxItem();
//set Phone as Display Text
cmItem.Content = line; //it is the Display Text
//get Name
line = custSR.ReadLine();
//set Tag property
cmItem.Tag = line; //it is the attached data to the object
//add to "Items"
phoneComboBox.Items.Add(ComboBoxItem);
}
and now its very simple to get the selected item in SelectionChanged event:
void phoneComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
nameTextBox.Content = (e.AddedItems[0] as ComboBoxItem).Tag;
}
You don't need to handle those arrays anymore.
Thanks for the other answers, this is the answer I came up with that works. Fixes the indexOutOfRange exception I was getting.
private void phoneComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (phoneComboBox.SelectedIndex != -1)
{
nameTextBox.Text = cName[phoneComboBox.SelectedIndex];
}
else
{
nameTextBox.Text = string.Empty;
}
}
I have a window form application with a button and a label.
After a push of the button, the label needs to display the square of the numbers 1 till 10. This needs to be displayed line by line.
This is the code I have so far:
private void button1_Click(object sender, EventArgs e)
{
int[] sqrNumbers = new int[10];
for (int i = 1; i != sqrNumbers.Length; i++)
{
sqrNumbers[i] = i * i;
lblSquares.Text = (sqrNumbers[i]).ToString()+ Environment.NewLine;
}
Unfortunately this doesn't work. As I am new to programming, I can't seem to figure this out. I scoured the net to find an answer bt haven't found what I was looking for. Thanks for helping me out on this.
Kr,
Jay
Try this instead: (Edit this is probably safer)
private void button1_Click(object sender, EventArgs e)
{
int[] sqrNumbers = new int[10];
String text = "";
for (int i = 1; i != sqrNumbers.Length; i++)
{
sqrNumbers[i] = i * i;
text += (sqrNumbers[i]).ToString()+ Environment.NewLine;
}
lblSquares.Text = text;
}
lblSquares.Text = (sqrNumbers[i]).ToString()+ Environment.NewLine;
overwrites the value of lblSquares.Text
lblSquares.Text += (sqrNumbers[i]).ToString()+ Environment.NewLine;
Will preserve the value and add to it.
https://msdn.microsoft.com/en-us/library/sa7629ew.aspx
I'm trying to implement a custom text-editor in C# using the ScintillaNET component. I've got most of it right until now, but stuck at one point. I want to give the user the ability to block comment/uncomment the selected text. I tried a lot, but cannot find any examples online. The only thing I seem to get from the control's Selection object are the Start and End positions, but that isn't much help
private void commentBlockToolStripMenuItem_Click(object sender, EventArgs e)
{
if (txtSQL.Selection.Text.Length > 0)
{
String start = txtSQL.Selection.Start.ToString();
String end = txtSQL.Selection.End.ToString();
MessageBox.Show(start + "::" + end);
}
}
Were any of you able to successfully implement this using the ScintillaNET control?
EDIT:
After some improvization, I'm able to do it somehow, but after block is commented, last line moves out of selection!
private void commentBlockToolStripMenuItem_Click(object sender, EventArgs e)
{
if (txtSQL.Selection.Text.Length > 0)
{
Range range = txtSQL.Selection.Range;
int f = range.StartingLine.Number;
int t = range.EndingLine.Number;
int endpos = txtSQL.Selection.End;
for (int i = f; i <= t; i++)
{
//txtSQL.GoTo.Line(i);
string tstr = txtSQL.Lines[i].Text.Replace(Environment.NewLine, "");
txtSQL.Lines[i].Text = "--" + tstr;
}
}
}
After a bit of experimentation, I found a way to accomplish this. Though I doubt if it is the most elegant of solutions!
private void commentBlockToolStripMenuItem_Click(object sender, EventArgs e)
{
if (txtSQL.Selection.Text.Length > 0)
{
Range range = txtSQL.Selection.Range;
int f = range.StartingLine.Number;
int t = range.EndingLine.Number;
for (int i = f; i <= t; i++)
{
txtSQL.InsertText(txtSQL.Lines[i].StartPosition,"--");
}
txtSQL.Selection.Start = txtSQL.Lines[f].StartPosition;
txtSQL.Selection.End = txtSQL.Lines[t].EndPosition;
}
}
Actually I found a very simple solution to this. To block comment do
scintilla1.Lexing.LineComment();
And to block uncomment do
scintilla1.Lexing.LineUncomment();
I have a ListView with a couple of items in it. When the ListView looses focus, the last selected ListViewItem is still "selected" with a gray background.
I would like to achieve that on ListView.FocusLost, the selection is gone and therefore the ListView.SelectedIndexChanged event will occur.
Any ideas?
I am using .NET CF 3.5.
Suppose you are accessing the ListView from a parent form/control.
You can add this piece of code in the form's/control's constructor/load event:
this.myListView.LostFocus += (s, e) => this.myListView.SelectedIndices.Clear();
Ok, so in your case, you would replace that delegate with:
if (this.myListView.SelectedIndices.Count > 0)
for (int i = 0; i < this.myListView.SelectedIndices.Count; i++)
{
this.myListView.Items[this.myListView.SelectedIndices[i]].Selected = false;
}
You can give the code a nicer form, btw.
myListView.SelectedItems.Clear();
I know this is late but in case someone else needed the solution I would like to add to the solution.
You need to set the Focused property to false to avoid deselected items having focus.
for (int i = 0; i < this.myListView.SelectedIndices.Count; i++)
{
this.myListView.Items[this.myListView.SelectedIndices[i]].Selected = false;
this.myListView.Items[this.myListView.SelectedIndices[i]].Focused = false;
}
This is easier.
this.myListView.SelectedIndex = -1;
this.myListView.Update();
Another effective way to approach this would be:
foreach (ListViewItem i in myListView.SelectedItems)
{
i.Selected = false;
}
You can try it:
MyList.ItemSelected += (sender, e) => {
((ListView)sender).SelectedItem = null;
};
or if you have the OnSelection created in your View code behind(xaml.cs):
private void OnSelection(object sender, SelectedItemChangedEventArgs e)
{
((ListView)sender).SelectedItem = null;
}
Regards
If you are using EditItemTemplate, rather than ItemTemplate, you may have been trying to figure out why ListView1.SelectedIndex = -1; hasn't been working. It's because you need to use ListView1.EditIndex = -1;
if (listView1.SelectedItems.Count > 0)
for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
listView1.SelectedItems[i].Selected = false;
}