Here is my current code:
private void searchBtn_Click(object sender, EventArgs e)
{
//get the search term from the textbox
String searchTerm = textBox.Text;
//if the column index is 1 the we search by code and 2 if we search by name
int columnIndex = 0;
if (codeRadioBtn.Checked)
columnIndex = 1;
else
columnIndex = 2;
gridView.ClearSelection();
int firstIndex = 0;
bool found = false;
for (int i = 0; i < gridView.Rows.Count; i++)
{
//change background color to DarkOrange for the rows that contain the searched value
if (gridView.Rows[i].Cells[columnIndex].Value.ToString().Contains(searchTerm, StringComparison.OrdinalIgnoreCase))
{
//gridView.Rows[i].Selected = true;
gridView.Rows[i].DefaultCellStyle.BackColor = Color.DarkOrange;
found = true;
if (firstIndex < 1)
{
firstIndex = i;
}
}
}
//display message if no item was found
if (!found)
MessageBox.Show("The search term was not found", "Warning");
else
// scroll grid to first highlighted row
this.gridView.Rows[firstIndex].Cells[0].Selected = true;
this.gridView.CurrentCell = this.gridView.Rows[firstIndex].Cells[0];
this.gridView.FirstDisplayedCell = this.gridView.CurrentCell;
}
I am trying to make it, onClick of the search button a second time it will set the current selected to the next highlighted row.
I have been trying for a while and can't figure it out. Any help would be greatly appreciated.
Use a List or Array to store all matches.
List<int> matchList = new List<int>();
e.g.
Whilst you are highlighting the rows, why not also add that row into a list.
gridView.Rows[i].DefaultCellStyle.BackColor = Color.DarkOrange;
matchList.add(i);
To work out whether it is the first click i.e. to load and highlight matches set a int outside of the button click to keep track of how many times you clicked the button.
int clickcount= 0;
Then after you have found and highlighted set clickcount to 1 and make sure you don't highlight again using an if statement, but instead run the below loop.
if (clickcount != 0 && clickcount !=matchList.Count-1)
{
this.gridView.CurrentCell = this.gridView.Rows[clickcount].Cells[0];
clickcount++;
}
else
{
clickcount = 0;
}
Full Example
int clickcount = 0;
List<int> matchList = new List<int>();
protected void searchBtn_Click(object sender, EventArgs e)
{
if (clickcount == 0)
{
//get the search term from the textbox
String searchTerm = textBox.Text;
//if the column index is 1 the we search by code and 2 if we search by name
int columnIndex = 0;
if (codeRadioBtn.Checked)
columnIndex = 1;
else
columnIndex = 2;
gridView.ClearSelection();
int firstIndex = 0;
bool found = false;
for (int i = 0; i < gridView.Rows.Count; i++)
{
//change background color to DarkOrange for the rows that contain the searched value
if (gridView.Rows[i].Cells[columnIndex].Value.ToString().Contains(searchTerm, StringComparison.OrdinalIgnoreCase))
{
//gridView.Rows[i].Selected = true;
this.gridView.CurrentCell = gridView.Rows[0].Cells[0];
gridView.Rows[i].DefaultCellStyle.BackColor = Color.DarkOrange;
matchList.Add(i);
found = true;
if (firstIndex < 1)
{
firstIndex = i;
}
}
}
//display message if no item was found
if (!found)
{
MessageBox.Show("The search term was not found", "Warning");
}
//add one to the count to stop the search happing again.
clickcount = 1;
}
else
{
//if clickcount = 1+ or your've reached the end of your match list count
if (clickcount != 0 && clickcount != matchList.Count - 1)
{
//gridView.Rows[clickcount].DefaultCellStyle.BackColor = Color.Red;
this.gridView.CurrentCell = gridView.Rows[matchList[clickcount]].Cells[0];
clickcount++;
}
else
{
MessageBox.Show("No More Found");
clickcount = 0;
matchList.Clear();
}
}
}
Related
I have a DataGridView where I have made the functions for MoveUp and MoveDown for the rows. But when I swap two rows and change the selected rows, the arrow of selected row stayed at the previous place although I set the selected row on the swapped row. Can I change the arrow on the correct row? It's just a small detail but I want to know if it's possible to do it.
I have this code:
private void moveUp()
{
if (dataGridView1.RowCount > 0)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int rowCount = dataGridView1.Rows.Count;
int index = dataGridView1.SelectedCells[0].OwningRow.Index;
if (index == 0)
{
return;
}
var temp = listApp[index];
var temp2 = listApp[index - 1];
listApp.Remove(temp);
listApp.Remove(temp2);
listApp.Insert(index-1, temp);
listApp.Insert(index, temp2);
dataGridView1.DataSource = null;
dataGridView1.DataSource = listApp;
dataGridView1.SelectedCells[0].Selected = false;
//dataGridView1.Rows[index].Selected = false;
dataGridView1.Rows[index-1].Selected = true;
}
}
}
And here the screen after swapping two rows.
Or is there a better way to do it?
Okay, I found a solution myself. It was enough to set dataGridView1.CurrentCell. Maybe it will be useful to someone.
The solution is here:
private void moveUp()
{
if (dataGridView1.RowCount > 0)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int rowCount = dataGridView1.Rows.Count;
int index = dataGridView1.SelectedCells[0].OwningRow.Index;
if (index == 0)
{
return;
}
var temp = listApp[index];
var temp2 = listApp[index - 1];
listApp.Remove(temp);
listApp.Remove(temp2);
listApp.Insert(index-1, temp);
listApp.Insert(index, temp2);
dataGridView1.DataSource = null;
dataGridView1.DataSource = listApp;
dataGridView1.SelectedCells[0].Selected = false;
dataGridView1.Rows[index-1].Selected = true;
dataGridView1.CurrentCell = dataGridView1[1, index - 1];
}
}
}
Is there a way to convert it, from exact value text become consist of? So I don't need to type Ballet instead of Bal.
Here's the code:
private void button6_Click_1(object sender, EventArgs e)
{
ColumnView View = gridControl1.MainView as ColumnView;
View.BeginUpdate();
try
{
int rowHandle = 0;
DevExpress.XtraGrid.Columns.GridColumn col = View.Columns["genre"];
while (true)
{
// // Locate the next row
rowHandle = View.LocateByValue(rowHandle, col, textBox6.Text);
// // Exit the loop if no row is found
if (rowHandle == DevExpress.XtraGrid.GridControl.InvalidRowHandle)
break;
//// Perform specific operations on the found row
gridView1.FocusedRowHandle = rowHandle;
rowHandle++;
}
}
finally { View.EndUpdate(); }
}
for (int i = 0; i < gridView1.VisibleRowCount; i++)
{
var row = gridView1.GetDataRow(i);
var genre = row["ColumnName"].ToString(); //ColumnName is your genre Column name
if(genre.StartsWith(textBox6.text)){
//here you can set row sellected
}
}
I dont have experience with devexpress but you can try it like this.
i dont if this what u seek, but it solved my own problem
for (int i = 0; i < gridView1.RowCount; i++)
{
var rosw = gridView1.GetDataRow(i);
var genre = rosw["genre"].ToString();
int tmpg = 0;
// //tmpg = genre.IndexOf(textBox8.Text, StringComparison.OrdinalIgnoreCase);
if (genre.IndexOf(textBox8.Text, StringComparison.OrdinalIgnoreCase) >= 0)
{
//if (tmpg >= 1)
// MessageBox.Show(genre);
gridView1.FocusedRowHandle = i;
break;
}
}
In the listView selected index i select item and display the file content in richTextBox:
void lvnf_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListViewCostumControl.lvnf.SelectedItems.Count > 0)
{
richTextBox1.Text = File.ReadAllText(ListViewCostumControl.lvnf.Items[ListViewCostumControl.lvnf.SelectedIndices[0]].Text);
int resultsnumber = 0;
int start = richTextBox1.SelectionStart;
int startIndex = 0;
int index = 0;
string word = textBox1.Text;
Color selectionColor = richTextBox1.SelectionColor;
word = textBox1.Text.Replace("\r\n", "\n");
while ((index = richTextBox1.Text.IndexOf(word, startIndex)) != -1)
{
richTextBox1.Select(index, word.Length);
richTextBox1.SelectionColor = Color.Yellow;
startIndex = index + word.Length;
resultsnumber ++;
}
richTextBox1.SelectionStart = start;
richTextBox1.SelectionLength = 0;
richTextBox1.SelectionColor = selectionColor;
label16.Text = resultsnumber.ToString();
label16.Visible = true;
numericUpDown1.Maximum = resultsnumber;
numericUpDown1.Enabled = true;
}
}
And i have the numericUpDown changedvalue event
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
}
for example if i have in the numericUpDown the maximum value of 4 that's 4 results in the richTextBox text.
Each result is in another place in the text.
What i want to do is when i move up down with the numericUpDown it will jump in the richTextBox to the result location in the text.
I think it should be that if the first result started in index 0 and ended in index 20 then jump to this result show this result.
If the third result is started in index 76 and ended in index 83 so if i selected in the numericUpDown the value 3 jump to this result location.
Ok this is what i did now it was quite easy but i have a problem reseting the variable:
It was easy to do it but i have a problem reseting the variables when selecting another item each time in the listView. I want to reset the List and also to set the numericUpDown1 to value 0 when selecting other item. But then it's going to the numericUpDown1_ValueChanged item before it's adding items to the List and throw exception since the List Count and the numericUpDown1 value are 0.
In top of form1 created a new List
private List<int> results = new List<int>();
In the listView selectedindex event:
void lvnf_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListViewCostumControl.lvnf.SelectedItems.Count > 0)
{
richTextBox1.Text = File.ReadAllText(ListViewCostumControl.lvnf.Items[ListViewCostumControl.lvnf.SelectedIndices[0]].Text);
results = new List<int>();
numericUpDown1.Value = 0;
int resultsnumber = 0;
int start = richTextBox1.SelectionStart;
int startIndex = 0;
int index = 0;
string word = textBox1.Text;
Color selectionColor = richTextBox1.SelectionColor;
word = textBox1.Text.Replace("\r\n", "\n");
while ((index = richTextBox1.Text.IndexOf(word, startIndex)) != -1)
{
richTextBox1.Select(index, word.Length);
richTextBox1.SelectionColor = Color.Yellow;
startIndex = index + word.Length;
resultsnumber ++;
results.Add(startIndex);
}
richTextBox1.SelectionStart = start;
richTextBox1.SelectionLength = 0;
richTextBox1.SelectionColor = selectionColor;
label16.Text = resultsnumber.ToString();
label16.Visible = true;
numericUpDown1.Maximum = results.Count -1;
numericUpDown1.Enabled = true;
richTextBox1.SelectionStart = results[(int)numericUpDown1.Value];
richTextBox1.ScrollToCaret();
}
}
In the numericupdown1 valuechanged event
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
if (results.Count == 0)
{
MessageBox.Show("Results Count Value is 0 Check It !!!!!");
}
else
{
richTextBox1.SelectionStart = results[(int)numericUpDown1.Value];
richTextBox1.ScrollToCaret();
}
}
When selecting first item in the listView it's fine.
But then when selecting any other item i want that the results List and the numericUpDown will be reset to empty and 0.
The problem is when i set the numericUpDown1 Value to 0 it's jumping to the valuechanged event before it's adding the results to the List so it's giving the exception.
Hi i am new to programming in c# wpf i just need to create a datagrid of some details i made it and the one major task is the while adding the serial number will be incremented as 1,2,3,4,etc.as when i remove an record from the datagrid the serial number to rearrange according to it eg:sno:1,2,3,4,5,6.
after removing the 3rd row it should be 1,2,3,4,5 instaed of 1,2,4,5,6.
similarly for inserting a row between 1,2,3 after adding it should be 1,2,3,4 task is row nust be added between 2 and 3 the new row help need
void mbtninsertstep_Click(object sender, RoutedEventArgs e) {
int rowindex = mdatagridedit.Items.IndexOf(mdatagridedit.CurrentCell);
if (rowindex >= 0) {
int rowcount = programtable.Rows.Count;
msteps.Add(new Steps { mStepno = count, mPosition = "0",
mRepeat = "NONE", mCount = "1", mAftercut = "NONE" });
int p = rowindex + 1;
for (int i = 0; i < rowcount + 1; i++) {
programtable.Rows[i][0] = p++;
} edited = true;
}
}
delete button code
insert button code.
void mbtndeletestep_Click(object sender, RoutedEventArgs e)
{
int deleterow;
DataGridView dg = new DataGridView();
// msteps.Remove((Steps)mdatagridedit.SelectedItem);
int rowindex = dg.CurrentRow.Index;
if (rowindex >= 0) {
int rowcount = programtable.Rows.Count;
int temp = dg.CurrentCell.RowIndex;
programtable.Rows.RemoveAt(temp);
int p = temp + 1;
for (int i = rowindex; i < rowcount - 1; i++) {
programtable.Rows[i][0] = p++;
}
int RowCountAfterDeleting = programtable.Rows.Count;
}
//int p = mdatagridedit.Items.Count;
// if (mdatagridedit.SelectedItem == null) {
// System.Windows.Forms.MessageBox.Show("Select an row");
// } else {
//msteps.RemoveAt(mdatagridedit.SelectedIndex);
//int p = mdatagridedit.SelectedIndex + 1;
// for(int i = mdatagridedit.SelectedIndex;
i < mdatagridedit.Items.Count - 1; i++){
// Steps step = new Steps();
// step.mStepno = p - 1;
// } int p1 = programtable.Rows.Count;
//}
}
In my RichtextBox, if I have written as below.
This is my pen,
his pen is beautiful.
Now I search word "is" then
output would be as below.
All "is" should be highlighted.
What about:
static class Utility {
public static void HighlightText(this RichTextBox myRtb, string word, Color color) {
if (word == string.Empty)
return;
int s_start = myRtb.SelectionStart, startIndex = 0, index;
while((index = myRtb.Text.IndexOf(word, startIndex)) != -1) {
myRtb.Select(index, word.Length);
myRtb.SelectionColor = color;
startIndex = index + word.Length;
}
myRtb.SelectionStart = s_start;
myRtb.SelectionLength = 0;
myRtb.SelectionColor = Color.Black;
}
}
Looks like this would do it.
http://www.dotnetcurry.com/ShowArticle.aspx?ID=146
int start = 0;
int indexOfSearchText = 0;
private void btnFind_Click(object sender, EventArgs e)
{
int startindex = 0;
if(txtSearch.Text.Length > 0)
startindex = FindMyText(txtSearch.Text.Trim(), start, rtb.Text.Length);
// If string was found in the RichTextBox, highlight it
if (startindex >= 0)
{
// Set the highlight color as red
rtb.SelectionColor = Color.Red;
// Find the end index. End Index = number of characters in textbox
int endindex = txtSearch.Text.Length;
// Highlight the search string
rtb.Select(startindex, endindex);
// mark the start position after the position of
// last search string
start = startindex + endindex;
}
}
public int FindMyText(string txtToSearch, int searchStart, int searchEnd)
{
// Unselect the previously searched string
if (searchStart > 0 && searchEnd > 0 && indexOfSearchText >= 0)
{
rtb.Undo();
}
// Set the return value to -1 by default.
int retVal = -1;
// A valid starting index should be specified.
// if indexOfSearchText = -1, the end of search
if (searchStart >= 0 && indexOfSearchText >=0)
{
// A valid ending index
if (searchEnd > searchStart || searchEnd == -1)
{
// Find the position of search string in RichTextBox
indexOfSearchText = rtb.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
// Determine whether the text was found in richTextBox1.
if (indexOfSearchText != -1)
{
// Return the index to the specified search text.
retVal = indexOfSearchText;
}
}
}
return retVal;
}
// Reset the richtextbox when user changes the search string
private void textBox1_TextChanged(object sender, EventArgs e)
{
start = 0;
indexOfSearchText = 0;
}
This will show all the searched criteria at the same time.
Using: 1 Textbox (to enter the text to search for) and 1 Button (to Run the Search).
Enter your search criteria inside the textbox and press search button.
// On Search Button Click: RichTextBox ("rtb") will display all the words inside the document
private void btn_Search_Click(object sender, EventArgs e)
{
try
{
if (rtb.Text != string.Empty)
{// if the ritchtextbox is not empty; highlight the search criteria
int index = 0;
String temp = rtb.Text;
rtb.Text = "";
rtb.Text = temp;
while (index < rtb.Text.LastIndexOf(txt_Search.Text))
{
rtb.Find(txt_Search.Text, index, rtb.TextLength, RichTextBoxFinds.None);
rtb.SelectionBackColor = Color.Yellow;
index = rtb.Text.IndexOf(txt_Search.Text, index) + 1;
rtb.Select();
}
}
}
catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); }
}
}
}
If you only want to match the whole word you can use this, note that this ignores case and also the |s\b means that plurals get highlighted e.g. Cat matches cats but not caterpiller :
public static void HighlightText(RichTextBox myRtb, string word, Color color)
{
if (word == string.Empty)
return;
var reg = new Regex(#"\b" + word + #"(\b|s\b)",RegexOptions.IgnoreCase);
foreach (Match match in reg.Matches(myRtb.Text))
{
myRtb.Select(match.Index, match.Length);
myRtb.SelectionColor = color;
}
myRtb.SelectionLength = 0;
myRtb.SelectionColor = Color.Black;
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
for (int i = 0; i < richTextBox1.TextLength; i++)
{
richTextBox1.Find(textBox1.Text, i, RichTextBoxFinds.None);
richTextBox1.SelectionBackColor = Color.Red;
}
}
else
{
for (int i = 0; i < richTextBox1.TextLength; i++)
{
richTextBox1.SelectAll();
richTextBox1.SelectionBackColor = Color.White;
}
}
}[lets make it!][1]
I would do it like that because all the other answers highlight the text, but doesnt change it back after you searched again.
Use the RichText Find Method to find the starting index for the searching word.
public int FindMyText(string searchText, int searchStart, int searchEnd)
{
int returnValue = -1;
if (searchText.Length > 0 && searchStart >= 0)
{
if (searchEnd > searchStart || searchEnd == -1)
{
int indexToText = richTextBox1.Find(searchText, searchStart, searchEnd, RichTextBoxFinds.MatchCase);
if (indexToText >= 0)
{
returnValue = indexToText;
}
}
}
return returnValue;
}
Use a Button or TextChangeListener and Search for your word.
private void button1_Click(object sender, EventArgs e)
{
// Select the first char in your Richtextbox
richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = richTextBox1.TextLength;
// Select until the end
richTextBox1.SelectionColor = Color.Black;
// Make the Text Color black
//Use an Inputfield to add the searching word
var word = txtSearch.Text;
//verify the minimum length otherwise it may freeze if you dont have text inside
if (word.Length > 3)
{
int s_start = richTextBox1.SelectionStart, startIndex = 0, index;
while ((index = FindMyText(word, startIndex, richTextBox1.TextLength)) != -1)
{
// goes through all possible found words and color them blue (starting index to end)
richTextBox1.Select(index, word.Length);
richTextBox1.SelectionColor = Color.Blue;
startIndex = index + word.Length;
}
// Color everything between in color black to highlight only found words
richTextBox1.SelectionStart = startIndex;
richTextBox1.SelectionLength = 0;
richTextBox1.SelectionColor = Color.Black;
}
}
I would highly recommend to set a minimum word length to avoid freezing and high memory allocation.