I have a listBox1 object and it contains some items. I have a button to move selected item up and another to move selected item down. What should the code be to the two buttons?
public void MoveUp()
{
MoveItem(-1);
}
public void MoveDown()
{
MoveItem(1);
}
public void MoveItem(int direction)
{
// Checking selected item
if (listBox1.SelectedItem == null || listBox1.SelectedIndex < 0)
return; // No selected item - nothing to do
// Calculate new index using move direction
int newIndex = listBox1.SelectedIndex + direction;
// Checking bounds of the range
if (newIndex < 0 || newIndex >= listBox1.Items.Count)
return; // Index out of range - nothing to do
object selected = listBox1.SelectedItem;
// Removing removable element
listBox1.Items.Remove(selected);
// Insert it in new position
listBox1.Items.Insert(newIndex, selected);
// Restore selection
listBox1.SetSelected(newIndex, true);
}
UPD 2020-03-24: Extension class for simple reuse and it also supports CheckedListBox (if CheckedListBox is not needed for you, please remove appropriate lines of code). Thanks #dognose and #Chad
public static class ListBoxExtension
{
public static void MoveSelectedItemUp(this ListBox listBox)
{
_MoveSelectedItem(listBox, -1);
}
public static void MoveSelectedItemDown(this ListBox listBox)
{
_MoveSelectedItem(listBox, 1);
}
static void _MoveSelectedItem(ListBox listBox, int direction)
{
// Checking selected item
if (listBox.SelectedItem == null || listBox.SelectedIndex < 0)
return; // No selected item - nothing to do
// Calculate new index using move direction
int newIndex = listBox.SelectedIndex + direction;
// Checking bounds of the range
if (newIndex < 0 || newIndex >= listBox.Items.Count)
return; // Index out of range - nothing to do
object selected = listBox.SelectedItem;
// Save checked state if it is applicable
var checkedListBox = listBox as CheckedListBox;
var checkState = CheckState.Unchecked;
if (checkedListBox != null)
checkState = checkedListBox.GetItemCheckState(checkedListBox.SelectedIndex);
// Removing removable element
listBox.Items.Remove(selected);
// Insert it in new position
listBox.Items.Insert(newIndex, selected);
// Restore selection
listBox.SetSelected(newIndex, true);
// Restore checked state if it is applicable
if (checkedListBox != null)
checkedListBox.SetItemCheckState(newIndex, checkState);
}
}
private void UpClick()
{
// only if the first item isn't the current one
if(listBox1.ListIndex > 0)
{
// add a duplicate item up in the listbox
listBox1.AddItem(listBox1.Text, listBox1.ListIndex - 1);
// make it the current item
listBox1.ListIndex = (listBox1.ListIndex - 2);
// delete the old occurrence of this item
listBox1.RemoveItem(listBox1.ListIndex + 2);
}
}
private void DownClick()
{
// only if the last item isn't the current one
if((listBox1.ListIndex != -1) && (listBox1.ListIndex < listBox1.ListCount - 1))
{
// add a duplicate item down in the listbox
listBox1.AddItem(listBox1.Text, listBox1.ListIndex + 2);
// make it the current item
listBox1.ListIndex = listBox1.ListIndex + 2;
// delete the old occurrence of this item
listBox1.RemoveItem(listBox1.ListIndex - 2);
}
}
Did you try searching it in google? Move Items up/dowm in listbox control for example.
public class SmartListBox : ListBox
{
//Moves the selected items up one level
public MoveUp()
{
for(int i = 0; i < Items.Count; i++)
{
if (Items[i].Selected)//identify the selected item
{
//swap with the top item(move up)
if (i > 0 && !Items[i - 1].Selected)
{
ListItem bottom = Items[i];
Items.Remove(bottom);
Items.Insert(i - 1, bottom);
Items[i - 1].Selected = true;
}
}
}
}
//Moves the selected items one level down
public MoveDown()
{
int startindex = Items.Count -1;
for (int i = startindex; i > -1; i--)
{
if (Items[i].Selected)//identify the selected item
{
//swap with the lower item(move down)
if (i < startindex && !Items[i + 1].Selected)
{
ListItem bottom = Items[i];
Items.Remove(bottom);
Items.Insert(i + 1, bottom);
Items[i + 1].Selected = true;
}
}
}
}
}
Modified #Save code to allow for moving items that are data bound to a ListBox using DataSource property.
public void MoveItem(int direction)
{
// Checking selected item
if (listBox1.SelectedItem == null || listBox1.SelectedIndex < 0)
return; // No selected item - nothing to do
// Calculate new index using move direction
int newIndex = listBox1.SelectedIndex + direction;
// Checking bounds of the range
if (newIndex < 0 || newIndex >= listBox1.Items.Count)
return; // Index out of range - nothing to do
UnifyCamera selected = listBox1.SelectedItem as UnifyCamera;
// modify the data source list
inputData.Cameras.RemoveAt(listBox1.SelectedIndex);
inputData.Cameras.Insert(newIndex, selected);
// re-bind your data source
((ListBox)listBox1).DataSource = null;
((ListBox)listBox1).DataSource = this.inputData.Cameras;
((ListBox)listBox1).DisplayMember = "Name";
// Restore selection
listBox1.SetSelected(newIndex, true);
}
Where UnifyCamera is my custom class that is stored in a list inputData.Cameras that returns a List<UnifyCamera>.
Modified Desolator code above to pass the control as a parameter...reusable
private void MoveUp()
{
MoveItem(-1,listBox1);
}
private void MoveDown()
{
MoveItem(1,listBox1);
}
public void MoveItem(int direction,ListBox listBox)
{
// Checking selected item
if (listBox.SelectedItem == null || listBox.SelectedIndex < 0)
return; // No selected item - nothing to do
// Calculate new index using move direction
int newIndex = listBox.SelectedIndex + direction;
// Checking bounds of the range
if (newIndex < 0 || newIndex >= listBox.Items.Count)
return; // Index out of range - nothing to do
object selected = listBox.SelectedItem;
// Removing removable element
listBox.Items.Remove(selected);
// Insert it in new position
listBox.Items.Insert(newIndex, selected);
// Restore selection
listBox.SetSelected(newIndex, true);
}
public static void MoveUpOrDownSelectedItem(ListBox LisBox, bool MoveUp)
{
if (LisBox.SelectedIndex > 0 && MoveUp)
{
// add a duplicate item up in the listbox
LisBox.Items.Insert(LisBox.SelectedIndex - 1, LisBox.SelectedItem);
// make it the current item
LisBox.SelectedIndex = (LisBox.SelectedIndex - 2);
// delete the old occurrence of this item
LisBox.Items.RemoveAt(LisBox.SelectedIndex + 2);
}
if ((LisBox.SelectedIndex != -1) && (LisBox.SelectedIndex < LisBox.Items.Count- 1) && MoveUp == false)
{
// add a duplicate item down in the listbox
int IndexToRemove = LisBox.SelectedIndex;
LisBox.Items.Insert(LisBox.SelectedIndex + 2, LisBox.SelectedItem);
// make it the current item
LisBox.SelectedIndex = (LisBox.SelectedIndex + 2);
// delete the old occurrence of this item
LisBox.Items.RemoveAt(IndexToRemove);
}
}
All the other answers are fine, but you should also consider this one :)
The idea of it is close to the one from SwDevMan81 but this is real code (not pseudo)
and you could move more than one item (multiple selected items)
and with an improvement when moving down.
private void MoveUp_listBox_button_Click(object sender, EventArgs e)
{
listBox.BeginUpdate();
int numberOfSelectedItems = listBox.SelectedItems.Count;
for (int i = 0; i < numberOfSelectedItems; i++)
{
// only if it's not the first item
if (listBox.SelectedIndices[i] > 0)
{
// the index of the item above the item that we wanna move up
int indexToInsertIn = listBox.SelectedIndices[i] - 1;
// insert UP the item that we want to move up
listBox.Items.Insert(indexToInsertIn, listBox.SelectedItems[i]);
// removing it from its old place
listBox.Items.RemoveAt(indexToInsertIn + 2);
// highlighting it in its new place
listBox.SelectedItem = listBox.Items[indexToInsertIn];
}
}
listBox.EndUpdate();
}
private void MoveDown_listBox_button_Click(object sender, EventArgs e)
{
listBox.BeginUpdate();
int numberOfSelectedItems = listBox.SelectedItems.Count;
// when going down, instead of moving through the selected items from top to bottom
// we'll go from bottom to top, it's easier to handle this way.
for (int i = numberOfSelectedItems-1; i >= 0; i--)
{
// only if it's not the last item
if (listBox.SelectedIndices[i] < listBox.Items.Count - 1)
{
// the index of the item that is currently below the selected item
int indexToInsertIn = listBox.SelectedIndices[i] + 2;
// insert DOWN the item that we want to move down
listBox.Items.Insert(indexToInsertIn, listBox.SelectedItems[i]);
// removing it from its old place
listBox.Items.RemoveAt(indexToInsertIn - 2);
// highlighting it in its new place
listBox.SelectedItem = listBox.Items[indexToInsertIn - 1];
}
}
listBox.EndUpdate();
}
Vexe's answer worked the best for me, but I had to modify it to fix a couple of issues. This solution will highlight the correct object if the same object is in the list box multiple times. Also, this solution prevents multi selected objects from flip flopping when they hit the top or bottom of the list box and the button continues to be pressed multiple times.
private void btnMoveUp_Click(object sender, EventArgs e)
{
// find the lowest index of non selected items
int lowestIndexNotSelected = listBox.Items.Count - 1;
for (int i = listBox.Items.Count - 1; i >= 0; i--)
{
if (!listBox.SelectedIndices.Contains(i))
{
lowestIndexNotSelected = i;
}
}
listBox.BeginUpdate();
int numberOfSelectedItems = listBox.SelectedItems.Count;
for (int i = 0; i < numberOfSelectedItems; i++)
{
// only if it's not a lower inde than the lowest non selected index
if (listBox.SelectedIndices[i] > lowestIndexNotSelected)
{
// the index of the item above the item that we wanna move up
int indexToInsertIn = listBox.SelectedIndices[i] - 1;
// insert UP the item that we want to move up
listBox.Items.Insert(indexToInsertIn, listBox.SelectedItems[i]);
// removing it from its old place
listBox.Items.RemoveAt(indexToInsertIn + 2);
// highlighting it in its new place (by index, to prevent highlighting wrong instance)
listBox.SelectedIndex = indexToInsertIn;
}
}
listBox.EndUpdate();
}
private void btnMoveDown_Click(object sender, EventArgs e)
{
// find the highest index of non selected items
int highestIndexNonSelected = 0;
for (int i = 0; i < listBox.Items.Count; i++)
{
if (!listBox.SelectedIndices.Contains(i))
{
highestIndexNonSelected = i;
}
}
listBox.BeginUpdate();
int numberOfSelectedItems = listBox.SelectedItems.Count;
// when going down, instead of moving through the selected items from top to bottom
// we'll go from bottom to top, it's easier to handle this way.
for (int i = numberOfSelectedItems - 1; i >= 0; i--)
{
// only if it's not a higher index than the highest index not selected
if (listBox.SelectedIndices[i] < highestIndexNonSelected)
{
// the index of the item that is currently below the selected item
int indexToInsertIn = listBox.SelectedIndices[i] + 2;
// insert DOWN the item that we want to move down
listBox.Items.Insert(indexToInsertIn, listBox.SelectedItems[i]);
// removing it from its old place
listBox.Items.RemoveAt(indexToInsertIn - 2);
// highlighting it in its new place (by index, to prevent highlighting wrong instance)
listBox.SelectedIndex = indexToInsertIn - 1;
}
}
listBox.EndUpdate();
}
// Options is a list box
private void MoveUpButton_Click(object sender,EventArgs e) {
int index = Options.SelectedIndex;
if (index <= 0) return;
string item = (string)Options.Items[index - 1];
Options.Items.RemoveAt(index - 1);
Options.Items.Insert(index,item);
selectedIndexChanged(null,null);
}
private void MoveDnButton_Click(object sender,EventArgs e) {
int index = Options.SelectedIndex;
if (index + 1 >= Options.Items.Count) return;
string item = (string)Options.Items[index];
Options.Items.RemoveAt(index);
Options.Items.Insert(index + 1,item);
Options.SelectedIndex = index + 1;
}
// sent when user makes a selection or when he moves an item up or down
private void selectedIndexChanged(object sender,EventArgs e) {
int index = Selected.SelectedIndex;
MoveUpButton.Enabled = index > 0;
MoveDnButton.Enabled = index + 1 < Selected.Items.Count;
}
private void btnUp_Click(object sender, System.EventArgs e)
{
if (this.lbItems.SelectedIndex == -1 || this.lbItems.SelectedIndex == 0)
return;
Object select, previous, temp;
select = lbItems.Items[lbItems.SelectedIndex];
previous = lbItems.Items[lbItems.SelectedIndex-1];
temp = select;
select = previous;
previous = temp;
lbItems.Items[lbItems.SelectedIndex] = select;
lbItems.Items[lbItems.SelectedIndex-1] = previous;
lbItems.SelectedIndex--;
}
private void btnDown_Click(object sender, System.EventArgs e)
{
if (this.lbItems.SelectedIndex == -1 || this.lbItems.SelectedIndex == lbItems.Items.Count-1)
return;
Object select, next, temp;
select = lbItems.Items[lbItems.SelectedIndex];
next = lbItems.Items[lbItems.SelectedIndex+1];
temp = select;
select = next;
next = temp;
lbItems.Items[lbItems.SelectedIndex] = select;
lbItems.Items[lbItems.SelectedIndex+1] = next;
lbItems.SelectedIndex++;
}
For those who are looking for a generic way to deal with ListBox that could be bound to a DataSource here's a generic extension based on Save's answer that will handle regular and binded ListBox.
public static void MoveUp(this ListBox listBox)
{
listBox.MoveItem(-1);
}
public static void MoveDown(this ListBox listBox)
{
listBox.MoveItem(1);
}
public static void MoveItem(this ListBox listBox, int direction)
{
// Checking selected item
if (listBox.SelectedItem == null || listBox.SelectedIndex < 0)
return; // No selected item - nothing to do
// Calculate new index using move direction
int newIndex = listBox.SelectedIndex + direction;
// Checking bounds of the range
if (newIndex < 0 || newIndex >= listBox.Items.Count)
return; // Index out of range - nothing to do
//Find our if we're dealing with a BindingSource
bool isBindingSource = listBox.DataSource is BindingSource;
//Get the list
System.Collections.IList list = isBindingSource ? ((BindingSource)listBox.DataSource).List : listBox.Items;
object selected = listBox.SelectedItem;
// Removing removable element
list.Remove(selected);
// Insert it in new position
list.Insert(newIndex, selected);
// Restore selection
listBox.SetSelected(newIndex, true);
if (isBindingSource)
{
//Reset the binding if needed
((BindingSource)listBox.DataSource).ResetBindings(false);
}
}
Get a collection of the selected items and then move them as follows:
private void btnMoveUp_Click(object sender, EventArgs e)
{
HashSet<KeyValuePair<int, object>> ItemsToMove = new HashSet<KeyValuePair<int, object>>();
foreach (object o in lstMyListView.SelectedItems)
ItemsToMove.Add(new KeyValuePair<int, object>(lstMyListView.Items.IndexOf(o), o));
foreach (KeyValuePair<int, object> kvp in ItemsToMove)
{
if (kvp.Key > 0) // check if its the first item before moving
{
lstMyListView.Items.Remove(kvp.Value);
lstMyListView.Items.Insert(kvp.Key - 1, kvp.Value);
}
}
}
private void btnMoveDown_Click(object sender, EventArgs e)
{
HashSet<KeyValuePair<int, object>> ItemsToMove = new HashSet<KeyValuePair<int, object>>();
foreach (object o in lstMyListView.SelectedItems)
ItemsToMove.Add(new KeyValuePair<int, object>(lstMyListView.Items.IndexOf(o), o));
foreach (KeyValuePair<int, object> kvp in ItemsToMove)
{
if (kvp.Key < lstMyListView.Items.Count - 1) // check if its the last item before moving
{
lstMyListView.Items.Remove(kvp.Value);
lstMyListView.Items.Insert(kvp.Key + 1, kvp.Value);
}
}
}
I wrote this function to move my selected items:
using System.Collections;
using System.Collections.Generic;
private void MoveListboxItems(int step, ListBox lb) {
/* 'step' should be:
* -1 for moving selected items up
* 1 for moving selected items down
* 'lb' is your ListBox
* see examples how to call below this function
*/
try {
// do only something when really an item is selected
if (lb.SelectedIndex > -1) {
// get some needed values - they change while we manipulate the listbox
// but we need them as they was original
IList SelectedItems = lb.SelectedItems;
IList SelectedIndices = lb.SelectedIndices;
// set some default values
int selIndex = -1;
int newIndex = -1;
int selCount = SelectedItems.Count;
int lc = 0;
int mc = 0;
string moveOldValue = string.Empty;
string selectedItemValue = string.Empty;
if (step == 1) {
mc = selCount - 1;
} else {
mc = lc;
}
// enter the loop through the selected items
while (lc < selCount) {
selectedItemValue = string.Empty;
moveOldValue = string.Empty;
try {
// get the item that should get moved
selectedItemValue = SelectedItems[mc].ToString();
selIndex = Convert.ToInt32(SelectedIndices[mc]);
} catch {
selIndex = -1;
}
// gen index for new place
newIndex = selIndex + step;
try {
// get the old value from the place where the item get moved
moveOldValue = lb.Items[newIndex].ToString();
} catch { /* do nothing */ }
try {
if (!String.IsNullOrEmpty(selectedItemValue) && !String.IsNullOrEmpty(moveOldValue) && selIndex != -1 && newIndex != -1 && !lb.SelectedIndices.Contains(newIndex)) {
// move selected item
lb.Items.RemoveAt(newIndex);
lb.Items.Insert(newIndex, selectedItemValue);
// write old value back to the old place from selected item
lb.Items.RemoveAt(selIndex);
lb.Items.Insert(selIndex, moveOldValue);
// hold the moved item selected
lb.SetSelected(newIndex, true);
}
} catch { /* do nothing */ }
lc++;
if (step == 1) {
mc -= step;
} else {
mc = lc;
}
}
}
} catch { /* do nothing */ };
}
// examples how i call the function above
void BtnLbUp_Click(object sender, EventArgs e) {
MoveListboxItems(-1, this.lbMyList);
}
void BtnLbDown_Click(object sender, EventArgs e) {
MoveListboxItems(1, this.lbMyList);
}
For Up Button:
private void UpBottom_Click(object sender, EventArgs e)
{
//this.Options is ListBox
if (this.Options.SelectedIndex == -1 ||
this.Options.SelectedIndex == 0)
return;
string item, aboveItem;
int itemIndex, aboveItemIndex;
itemIndex = this.Options.SelectedIndex;
aboveItemIndex = this.Options.SelectedIndex - 1;
item = (string)this.Options.Items[itemIndex];
aboveItem = (string)this.Options.Items[aboveItemIndex];
this.Options.Items.RemoveAt(aboveItemIndex);
this.Options.Items.Insert(itemIndex, aboveItem);
}
For Down Button:
private void DownButton_Click(object sender, EventArgs e)
{
//this.Options is ListBox
if (this.Options.SelectedIndex == -1 ||
this.Options.SelectedIndex >= this.Options.Items.Count)
return;
string item, belowItem;
int itemIndex, belowItemIndex;
itemIndex = this.Options.SelectedIndex;
belowItemIndex = this.Options.SelectedIndex + 1;
if (belowItemIndex >= this.Options.Items.Count)
return;
item = (string)this.Options.Items[itemIndex];
belowItem = (string)this.Options.Items[belowItemIndex];
this.Options.Items.RemoveAt(itemIndex);
this.Options.Items.Insert(belowItemIndex, item);
this.Options.SelectedIndex = belowItemIndex;
}
I Use this with multiple selections.
It also works with interleaved selections.
private void Order_buttons_Click(object sender, EventArgs e)
{
//If noselection return
if (Layouts_listBox.SelectedItems.Count == 0) return;
//Determines wether up or down
int movement = (sender as Button) == Order_Upbutton? - 1 : 1;
//creates a dictionary associating the original Index (ListBox) to the text
Dictionary<int, string> Items = new Dictionary<int, string>();
//Also creates a list with the Index for sorting
List<int> DesiredOrder = new List<int>();
//Cycle through the selection and fill both the list and dictionary
ListBox.SelectedObjectCollection NN = Layouts_listBox.SelectedItems;
foreach (object n in NN)
{
DesiredOrder.Add(Layouts_listBox.Items.IndexOf(n));
Items.Add(Layouts_listBox.Items.IndexOf(n), (string)n);
}
//Sort the List according to the desired button (Up or Down)
DesiredOrder.Sort();
if ((sender as Button) != Order_Upbutton) DesiredOrder.Reverse();
//I'm using this ErrorHandling but thats up to you
try
{
//Call the MoveItem (Credits to Save) according to the sorted order
foreach (int n in DesiredOrder) MoveItem(movement, Items[n]);
}
catch (Exception)
{
SystemSounds.Asterisk.Play();
}
}
public void MoveItem(int direction, string Selected)
{
// Checking selected item
if (!Layouts_listBox.Items.Contains(Selected) || Layouts_listBox.Items.IndexOf(Selected) < 0)
throw new System.Exception(); // No selected item - Cancel entire Func
// Calculate new index using move direction
int newIndex = Layouts_listBox.Items.IndexOf(Selected) + direction;
// Checking bounds of the range
if (newIndex < 0 || newIndex >= Layouts_listBox.Items.Count)
throw new System.Exception(); // Index out of range - Cancel entire Func
object selected = Layouts_listBox.Items[Layouts_listBox.Items.IndexOf(Selected)];
// Removing removable element
Layouts_listBox.Items.Remove(selected);
// Insert it in new position
Layouts_listBox.Items.Insert(newIndex, selected);
// Restore selection
Layouts_listBox.SetSelected(newIndex, true);
}
Related
I have a map made of hexagons. When a hexagon is clicked on which a troop resides, other hexagons around the clicked one should be highlighted based on the troop's range variable.
I have tried quite a few methods on my own, but failed. I can't seem to get a list with the correct ones highlighted. In this example I will be using a troop of a range of 2 tiles.
The method I have tried to implement is as following:
There are 3 lists.
Open list contains nodes to be expanded by 1
Close list contains nodes that will be highlighted
remove list contains nodes that have already been expanded
I followed the following algorithm:
Add the start node to all lists.
Expand the nodes in open list by 1 tile.
Remove the nodes within open list using remove list.
add open list nodes to closed list.
clear remove list.
copy open list contents to remove list.
Repeat for whatever the range of the troop is.
My goal is to highlight a hex tiles of radius r, around the clicked tile; r = troop range.
Im quite lost, and I was probably doing some obvious mistakes. Any help will be appreciated!
(Below is the code that I made)
private List<string> expand(List<string> open, bool odd)
{
List<string> newopen = new List<string>();
int oddEven = 1;
if (odd == false)
{
oddEven = -1;
}
foreach (string s in open)
{
string[] rc = s.Split('.'); //row + col of the current hex
int Row = int.Parse(rc[0]);
int Col = int.Parse(rc[1]);
for (int r = 1; r >= -1; r--)
{
for (int c = -1; c <= 1; c++)
{
if (!((r == oddEven && c != 0) || (r== 0 && c == 0)))
{
if (((Row + r) > -1 && (Col + c) > -1) && ((Row + r) < 9 && (Col + c) < 18)) //mapcheck
{
if (Hexmap[Row + r, Col + c] == '-') //mapcheck
{
newopen.Add((Row + r).ToString() + "." + (Col + c).ToString());
}
}
}
}
}
}
return newopen;
}
private void Remove(ref List<string> rem, ref List<string> open)
{
foreach (string s in rem)
{
open.Remove(s);
}
}
private void Add(ref List<string> closed, ref List<string> open)
{
foreach (string s in open)
{
closed.Add(s);
}
}
private void TroopSelect(int row, int col, int range)
{
bool odd = true;
if ((row % 2) == 0)
{
odd = false;
}
List<string> open = new List<string>();
List<string> close = new List<string>();
List<string> remove = new List<string>();
open.Add(row.ToString() + "." + col.ToString());
close.Add(row.ToString() + "." + col.ToString());
remove.Add(row.ToString() + "." + col.ToString());
for (int i = 0; i < range; i++)
{
open = expand(open, odd); //row and col of the current point being checked --- REMOVE ROW COL, find it out from the list within the function
Remove(ref remove, ref open); //remove from open the remove hex values
Add(ref close, ref open); //add to closed what's in open
remove.Clear(); //clear remove
remove = open; //set remove the same as open
}
foreach (string s in close)
{
string[] rc = s.Split('.'); //row + col of the current hex
int Row = int.Parse(rc[0]);
int Col = int.Parse(rc[1]);
Hexagons.Add(new PointF(Row, Col));
}
}
private void CheckTroop(int row, int col)
{
if (Hexmap[row, col] == 'o') //it's a friendly troop
{
foreach (Troops t in playertroops)
{
if (t.row == row && t.column == col)
{
TroopSelect(row, col, t.range);
this.battlegrid.Invalidate();
}
}
}
}
private void battlegrid_MouseClick(object sender, MouseEventArgs e)
{
int row, col;
PointToHex(e.X, e.Y, HexHeight, out row, out col); //gets the hex in the x/y position
CheckTroop(row, col);
//this.Invalidate();
}
Below are also two images: (Ignore the rock in the background!)
First of troop range = 1, works ok
Second image of troop range = 2, doesn't work ok
Thanks once again for any help.
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.
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();
}
}
}
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.
Whats the best way to swap two ListView items in C#? I see that the standard ListView doesn't implement such functionality.
--
Best Regards,
Murat
Building upon the KnowDotNet article ref'd by Murat, here's my extension method that is a bit more flexible (it operates on any item, not just the cursel), and bugfixed (BeginUpdate/Endupdate for less flicker, EnsureVisible, and bounds checking).
Doesn't need to be an extension method, but I like them :)
namespace YourApp
{
public static class MyExtensions
{
// Based upon http://www.knowdotnet.com/articles/listviewmoveitem.html
public static void MoveSelectedItem(this System.Windows.Forms.ListView lv, int idx, bool moveUp)
{
// Gotta have >1 item in order to move
if(lv.Items.Count > 1)
{
int offset = 0;
if (idx >= 0)
{
if (moveUp)
{
// ignore moveup of row(0)
offset = -1;
}
else
{
// ignore movedown of last item
if (idx < (lv.Items.Count - 1))
offset = 1;
}
}
if (offset != 0)
{
lv.BeginUpdate();
int selitem = idx + offset;
for (int i = 0; i < lv.Items[idx].SubItems.Count; i++)
{
string cache = lv.Items[selitem].SubItems[i].Text;
lv.Items[selitem].SubItems[i].Text = lv.Items[idx].SubItems[i].Text;
lv.Items[idx].SubItems[i].Text = cache;
}
lv.Focus();
lv.Items[selitem].Selected = true;
lv.EnsureVisible(selitem);
lv.EndUpdate();
}
}
}
}
}
If you use custom ListViewItem, or object you cannot clone object, or stock in string:
enum Direction { UP = -1, DOWN = +1};
void ListViewMove(ListView lv, Direction direction)
{
if (lv.SelectedItems.Count > 0)
{
int selIdx = lv.SelectedItems[0].Index;
ListViewItem tmp = lv.Items[selIdx] ;
if ( ( (selIdx != 0) && direction == Direction.UP ) ||
((selIdx!=lv.Items.Count-1) && (direction == Direction.DOWN)) )
{
lv.Items.RemoveAt(selIdx);
tmp = lv.Items.Insert(selIdx + (int)direction, tmp);
tmp.Selected = true;
}
}
lv.Focus();
}
Both ASP.NET and Winforms ListView have Items property which allows to add or removed items.
I´ve wrote a little sample that should work.
ListViewItem[] copyOfItemsInListView1 = new ListViewItem[listView1.Items.Count];
ListViewItem[] copyOfItemsInListView2 = new ListViewItem[listView2.Items.Count];
listView1.Items.CopyTo(copyOfItemsInListView1, 0);
listView2.Items.CopyTo(copyOfItemsInListView2, 0);
listView1.Items.Clear();
listView2.Items.Clear();
for (int i = 0; i < copyOfItemsInListView2.Length; i++)
{
listView1.Items.Add(copyOfItemsInListView2[i]);
}
for (int i = 0; i < copyOfItemsInListView1.Length; i++)
{
listView2.Items.Add(copyOfItemsInListView1[i]);
}
Clone them:
// move selected item up
int selectedIndex = mListView.SelectedIndices[0];
if (selectedIndex > 0)
{
ListViewItem item1 = (ListViewItem)mListView.Items[selectedIndex - 1].Clone();
ListViewItem item2 = (ListViewItem)mListView.Items[selectedIndex].Clone();
mListView.Items[selectedIndex - 1] = item2;
mListView.Items[selectedIndex] = item1;
mListView.SelectedIndices.Remove(selectedIndex);
mListView.SelectedIndices.Add(selectedIndex - 1);
}