Shift selection c# optimization - c#

Basically I have done a code which works as a shift selection, I have already programmed the file selection working with Ctrl-Key selection, but it is required to optimize it. Nevertheless I have not found any way to make it easier has anyone any idea of how could I achieve that? The highlightedIndex is a list of int.
private int lastClickedPointIdx = -1;
int firstvaluepoint = 0;
private int movingPointIdx = -1; //-1: no point moving
private void KeySelection()
{
bool shiftclick = false;
int firstselectedpoint = 0;
if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) //Ctrl
{
if (highlightedIndex.Contains(movingPointIdx)) //remove point if already is in the highlightedIndex list
{
highlightedIndex.Remove(movingPointIdx);
firstselectedpoint = movingPointIdx;
shiftclick = false;
return;
}
else //otherwise add it to the highlightedIndex list
{
highlightedIndex.Add(movingPointIdx);
highlightedIndex.Sort();
firstselectedpoint = movingPointIdx;
shiftclick = false;
}
}
else if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)) //Shift
{
if (!shiftclick)
{
highlightedIndex.Clear();
shiftclick = true;
}
if (lastClickedPointIdx > -1) //highlights all points
{
if (firstvaluepoint > firstselectedpoint)
{
for (int i = firstselectedpoint; i <= firstvaluepoint; i++)
{
highlightedIndex.Add(i);
}
}
else
{
for (int i = firstvaluepoint; i <= firstselectedpoint; i++)
{
highlightedIndex.Add(i);
}
}
}
}
lastClickedPointIdx = movingPointIdx;
if (!shiftclick)
firstvaluepoint = firstselectedpoint;
}

Related

Unique correspondence for characters in C#

I'm new to programming and I got really stuck on an exercise.
I need to determine whether each character in the first string can be uniquely replaced by a character in the second string. Both strings are equal in length.
For example, "aabc ea" and "ddtf hd" , the result needs to be:
True
a => d
b => t
c => f
=>
e => h
If I have for example "abac ea" and "ddtf hd" , the result needs to be:
False
Since "abac ea" and "ddt hd" don't have an unique replacement.
This is my Code:
using System;
namespace UniqueStrings
{
class Program
{
static void Main(string[] args)
{
string firstPhrase = Console.ReadLine();
string secondPhrase = Console.ReadLine();
bool result = false;
int charsCount = 0;
char[] firstPhraseChars = new char[firstPhrase.Length];
char[] secondPhraseChars = new char[secondPhrase.Length];
if (firstPhrase.Length != secondPhrase.Length)
{
result = false;
}
for (int i = 0; i < firstPhrase.Length; i++)
{
if (firstPhrase[i] == firstPhraseChars[i])
{
firstPhraseChars[i] = firstPhrase[i];
secondPhraseChars[i] = secondPhrase[i];
}
for (int j = 0; j < secondPhrase.Length; j++)
{
if (secondPhrase[j] == secondPhraseChars[j])
{
firstPhraseChars[j] = firstPhrase[j];
secondPhraseChars[j] = secondPhrase[j];
result = false;
}
else
{
result = true;
}
}
}
for (int i = 0; i < firstPhrase.Length; i++)
{
if (result == false)
{
firstPhraseChars[charsCount] = firstPhrase[i];
secondPhraseChars[charsCount] = secondPhrase[i];
charsCount++;
}
}
if (result == false)
Console.WriteLine(result);
else
{
Console.WriteLine(result);
for (int i = 0; i < firstPhrase.Length; i++)
{
Console.WriteLine(firstPhrase[i] + " => " + secondPhrase[i]);
}
}
Console.Read();
}
}
}
Can someone please help me understand what I'm doing wrong? I have no idea anymore and I feel like this code will never work. There needs to be some solution to this, which I'm not understanding.
I'm not supposed to use LINQ, list or dictionary, only System.
If anyone has other questions, feel free to ask.
exemple of non-optimized solution
using System;
namespace UniqueStrings
{
class Program
{
static bool CheckStringSimilarity(string firstPhrase, string secondPhrase)
{
if (firstPhrase.Length != secondPhrase.Length)
{
return false;
}
var length = firstPhrase.Length;
for (var i =0; i<length; i++)
{
for(var j=0; j<length; j++ )
{
if((firstPhrase[i] == firstPhrase[j]) && (secondPhrase[i] != secondPhrase[j]))
{
return false;
}
if((firstPhrase[i] != firstPhrase[j]) && (secondPhrase[i] == secondPhrase[j]))
{
return false;
}
}
}
return true;
}
static void Main(string[] args)
{
Console.WriteLine($"CheckStringSimilarity('aaa','bbb') = {CheckStringSimilarity("aaa", "bbb")}");
Console.WriteLine($"CheckStringSimilarity('aaab','bbbc') = {CheckStringSimilarity("aaab", "bbbc")}");
Console.WriteLine($"CheckStringSimilarity('rrt','aze') = {CheckStringSimilarity("rrt", "aze")}");
Console.WriteLine($"CheckStringSimilarity('rrt dd','aad aa') = {CheckStringSimilarity("rrt dd", "aad aa")}");
}
}
}
DEMO
In your first for loop the result will always be true since both if statements will always return false. When the if statements compare their values "firstPhraseChars[i]" and "secondPhraseChars[j]" are always empty since you never put anything into these arrays before the comparison.
Hope this helps without giving away too much ;)
You can look for a counter example: if you've found it, correspondent doesn't exist:
private static bool HasCorrespondence(string left, string right) {
if (left == null)
return right == null;
if (right == null)
return false;
if (left.Length != right.Length)
return false;
// known correspondence
Dictionary<char, char> correspondence = new Dictionary<char, char>();
for (int i = 0; i < left.Length; ++i)
if (correspondence.TryGetValue(left[i], out char expected)) {
// counter example: we want expected, but have right[i]
if (expected != right[i])
return false;
}
else
// we have nothing for left[i], so we can add (left[i], right[i]) pair
correspondence.Add(left[i], right[i]);
// no counter example exists, return true
return true;
}
If Dictionary is prohibited, you can emulate it with help of array:
private static bool HasCorrespondence(string left, string right) {
if (left == null)
return right == null;
if (right == null)
return false;
if (left.Length != right.Length)
return false;
int[] correspondence = new int[char.MaxValue];
for (int i = 0; i < left.Length; ++i)
if (correspondence[left[i]] != 0) {
if (correspondence[left[i]] != right[i])
return false;
}
else
correspondence[left[i]] = right[i];
return true;
}
If you are looking for one to one correspondence, you can just check twice"
private static bool HasOneToOne(string left, string right) =>
HasCorrespondence(left, right) &&
HasCorrespondence(right, left);

How can I select and Deselect all items by checkbox in dynamically added check _list_box in c#?

I wanted to do select/ deselect with one checkbox. I have tried to get the index of the selected item. I haven't been able to get this, so I tried to change the name of the box. Still no success.
foreach (var item in DATAsetname_INIlist)
{
checkedListBox2.Items.Add(item);
}
if (checkedListBox2.Items.Count != 0)
{
checkedListBox2.Items.Add("Select all");
}
private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (checkedListBox2.Items.Count != 0)
{
if (checkedListBox2.SelectedItem.ToString() == "Select all")
{
for (int i = 0; i < checkedListBox2.Items.Count; i++)
{
checkedListBox2.SetItemChecked(i, true);
}
string changed = "Deselect all";
checkedListBox2.SelectedItem =changed;
}
if (checkedListBox2.SelectedItem.ToString() == "Deselect all")
{
for (int i = 0; i < checkedListBox2.Items.Count; i++)
{
checkedListBox2.SetItemChecked(i, false);
}
string changed = "Select all";
checkedListBox2.SelectedItem = changed;
}
}
}
can you please help on this ? thank you
Have done quick exercise. Please modify your code accordingly.
/// <summary>
/// Check all check boxes and vice versa
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ChkSelectAll_CheckedChanged(object sender, EventArgs e)
{
//Declare your checkedListBox2 count
iCount = checkedListBox2.Items.Count;
if (sender != null)
{
for (int i = 1; i <= iCount; i++)
{
CheckBox ck = null;
Control[] chkTest = this.Controls.Find("chkDrive" + i, true);
if (chkTest.Length > 0)
{
if (chkSelectAll.Checked)
{
for (int j = 0; j < chkTest.Length; j++)
{
ck = (CheckBox)chkTest[j];
ck.Checked = true;
}
}
else
{
for (int j = 0; j < chkTest.Length; j++)
{
ck = (CheckBox)chkTest[j];
ck.Checked = false;
}
}
}
}
}
}
can you try this.
try
{
checkedListBox1.SelectedIndexChanged -= checkedListBox1_SelectedIndexChanged;
if (checkedListBox1.Items.Count != 0)
{
if (checkedListBox1.SelectedItem.ToString() == "Select all")
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}
string changed = "Deselect all";
checkedListBox1.Items[checkedListBox1.SelectedIndex] = changed;
}
else if (checkedListBox1.SelectedItem.ToString() == "Deselect all")
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, false);
}
string changed = "Select all";
checkedListBox1.Items[checkedListBox1.SelectedIndex] = changed;
}
}
}
catch (Exception ex)
{
}
finally
{
checkedListBox1.SelectedIndexChanged += checkedListBox1_SelectedIndexChanged;
}
If this is C# based, windows application.
Search form collection and find all check box on form, using for each.
Code
foreach(CheckBox item in Form.Control)
{
Item.Checked=true;
}
For check list box
for(int i;I<checklistbox.items.count;I++)
{
checklistbox.SetItemChecked(I,true);
}
When i work with c# i had litle simillar problem in my case was enought was to replace if(string==string) by string.equals(string)
Btw few questions:
checkedListBox2.SelectedItem.ToString() what is value in here where comparing?
Does your listener work at all?
Not better way to check which checkBox is taken comparing sender in switch?
How u declare checkedListBox2
By checkedListBox2.Items.Add(item); i suspect u create checkBoxes by hand can u show it? meybe something is there wrong
This part added after Lian comment
foreach(var item in DATAsetname_INIlist)
{
checkedListBox2.Items.Add(item);
}
if (checkedListBox2.Items.Count != 0) {
checkedListBox2.Items.Add("Select all");
}
private void checkedListBox2_SelectedIndexChanged (object sender, EventArgs e)
{
if (checkedListBox2.Items.Count != 0 && checkedListBox2.SelectedItem.ToString().equals("Select all")) {
changeStateOfSelectedItem("Deselect all", true);
} else if (checkedListBox2.SelectedItem.ToString().equals("Deselect all")) {
changeStateOfSelectedItem("Select all", false);
}
}
private void changeStateOfSelectedItem (String state, bolean stateToReplace){
for (int i = 0; i < checkedListBox2.Items.Count; i++) {
checkedListBox2.SetItemChecked(i, stateToReplace);
}
string changed = state;
checkedListBox2.SelectedItem = changed;

Optimizing grid based inventory

I am trying to create a grid based inventory and so far everything is going well except for one thing.
The inventory consists of Grids that are built of Cells, Grids can have custom size (like 5x3, 3x3). Every Cell is always 1x1 and can hold Items which can be of different sizes (like Grids).
I am handling the interactions in inventory using HandleCursor function which I run every frame to check if user is highlighting items/cells or moving stuff around.
As you can see in the code below I'm using a lot of fors to do that and I'am wondering if I could optimize it somehow so it isn't this hard on the CPU (right now highlighting a grid/item makes my inventory script create an overhead of 0.27-0.31ms.
Is there any better alternative than checking if Rect of each Cell contains my cursor?
Sorry if this is a wrong place to ask this kind of questions.
My code:
void HandleHighlighting()
{
HandleHighlightingCells();
HandleHighlightingItems();
HandleHighlightingPickedItem();
}
Highlighting all cells in all grids:
void HandleHighlightingCells()
{
for (int i = 0; i < Grids.Count; i++)
{
InventoryGrid grid = Grids[i];
if (grid.GetRect().Contains(Input.mousePosition))
{
for (int x = 0; x < grid.width; x++)
{
for (int y = 0; y < grid.height; y++)
{
InventoryCell cell = grid.cells[x, y];
if (cell.GetRect().Contains(Input.mousePosition))
{
if (highlightedCell && highlightedCell != cell)
{
highlightedCell.Highlight(EItemHighlightMode.NONE);
}
highlightedCell = cell;
highlightedCell.Highlight(EItemHighlightMode.BASE);
}
else
{
if (cell == highlightedCell)
{
highlightedCell = null;
}
cell.Highlight(EItemHighlightMode.NONE);
}
}
}
}
else
{
for (int x = 0; x < grid.width; x++)
{
for (int y = 0; y < grid.height; y++)
{
InventoryCell cell = grid.cells[x, y];
if (highlightedCell && highlightedCell != cell)
{
if (highlightedCell.grid == grid)
{
highlightedCell = null;
}
}
cell.Highlight(EItemHighlightMode.NONE);
}
}
}
}
}
Highlighting items in inventory:
void HandleHighlightingItems()
{
if (highlightedCell && highlightedCell.heldItem)
{
InventoryItem item = highlightedCell.heldItem;
if (highlightedItem && highlightedItem != item)
{
for (int i = 0; i < highlightedItem.occupiedCells.Length; i++)
{
highlightedItem.occupiedCells[i].Highlight(EItemHighlightMode.NONE);
}
}
highlightedItem = item;
for (int i = 0; i < item.occupiedCells.Length; i++)
{
item.occupiedCells[i].Highlight(EItemHighlightMode.BASE);
}
}
else
{
if (highlightedItem)
{
for (int i = 0; i < highlightedItem.occupiedCells.Length; i++)
{
highlightedItem.occupiedCells[i].Highlight(EItemHighlightMode.NONE);
}
highlightedItem = null;
}
}
}
Highlighting picked items:
void HandleHighlightingPickedItem()
{
if (pickedItem)
{
if (highlightedCell)
{
InventoryGrid grid = highlightedCell.grid;
InventoryCell[] cellsToHighlight = new InventoryCell[pickedItem.width * pickedItem.height];
InventoryItem firstItem = null;
bool valid = true;
int index = 0;
for (int x = 0; x < pickedItem.width; x++)
{
for (int y = 0; y < pickedItem.height; y++)
{
if (highlightedCell.x + x < grid.width && highlightedCell.y + y < grid.height)
{
InventoryCell cell = grid.cells[highlightedCell.x + x, highlightedCell.y + y];
cellsToHighlight[index] = cell;
if (highlightedItem)
{
if (cell.heldItem != highlightedItem)
{
if (cell.heldItem)
{
valid = false;
}
}
}
else
{
if (cell.heldItem)
{
if (!firstItem)
{
firstItem = cell.heldItem;
}
else
{
if (cell.heldItem != firstItem)
{
valid = false;
}
}
}
}
}
else
{
valid = false;
}
index++;
}
}
for (int i = 0; i < cellsToHighlight.Length; i++)
{
if (cellsToHighlight[i])
{
if (valid)
{
cellsToHighlight[i].Highlight(EItemHighlightMode.VALID);
}
else
{
cellsToHighlight[i].Highlight(EItemHighlightMode.INVALID);
}
}
}
}
}
}
With help of t3chb0t and juvian from codereview.stackexchange.com I managed to reduce the overhead from ~0.31ms to <0.08ms.
I actually ended up only changing the function that highlights all cells.
I'm posting this as an answer so it's visible in case someone else has similar problem.
void HandleHighlightingCells()
{
Vector2 mousePos = Input.mousePosition;
bool anyHighlighted = false;
for (int i = 0; i < Grids.Count; i++)
{
InventoryGrid grid = Grids[i];
if (grid.GetRect().Contains(mousePos))
{
Vector2 uiPos = Vector2.zero;
RectTransformUtility.ScreenPointToLocalPointInRectangle(grid.cellsRoot.GetComponent<RectTransform>(), Input.mousePosition, GameManager.singleton.inventoryCanvas.worldCamera, out uiPos);
//64 is the constant size of a cell
int cellX = Mathf.FloorToInt(uiPos.x / 64);
int cellY = Mathf.FloorToInt(-uiPos.y / 64);
if(cellX < grid.width && cellY < grid.height)
{
cellX = Mathf.Clamp(cellX, 0, grid.width - 1);
cellY = Mathf.Clamp(cellY, 0, grid.height - 1);
InventoryCell cell = grid.cells[cellX, cellY];
if (highlightedCell && highlightedCell != cell)
{
highlightedCell.Highlight(EItemHighlightMode.NONE);
}
highlightedCell = cell;
highlightedCell.Highlight(EItemHighlightMode.BASE);
anyHighlighted = true;
}
}
}
if(!anyHighlighted)
{
if(highlightedCell)
{
highlightedCell.Highlight(EItemHighlightMode.NONE);
highlightedCell = null;
}
}
}

C# Tic-Tac-Toe Minimax

I am currently trying my hand at making a minimax AI for tictactoe. My goal was that it should suffice for larger boards as well. However, I am having quite a hard time wrapping my head around how to implement the algorithm. I have read countless different descriptions of the algorithm but I still don't seem to be able to make it work. My result as of yet is an incredibly stupid AI. Here is my code for it.
Edit: The main point I am wondering about is how I make the AI value me not winning over forwarding itself towards the win. As of now it doesn't really care that I will win the next turn.
namespace TicTacToe_AI
{
public class Move //A class for moves
{
public int x, y, value, MoveNumber;
void SetMove(int a, int b)
{
x = a;
y = b;
}
public Move(int a, int b)
{
SetMove(a, b);
}
public Move()
{ }
}
class AI //AIClass
{
//The minimax algorithm
public Move CalculateMoves(int[,] Board, int BoardSize, int Depth, Move BestMoveAI, Move BestMovePlayer, int OriginalDepth, int CurrentTurn)
{
Depth--; //Decrease the depth for each iteration
bool Alpha = false; //Alpha-beta pruning - needs improvement
bool Beta = false;
bool WinningMove = false;
if (CurrentTurn == 1) CurrentTurn = 2;
if (CurrentTurn == 2) CurrentTurn = 1;
List<Move> DifferentMoves = new List<Move>();
List<Move> PossibleMoves = new List<Move>();
for (int i = 0; i < BoardSize; i++) //Add all possible moves to a list
{
for (int j = 0; j < BoardSize; j++)
{
if (Board[i, j] == 0)
{
Move Possible = new Move(i, j);
PossibleMoves.Add(Possible);
}
}
}
if (CurrentTurn == 2 && Depth >= 0 && Depth < BestMoveAI.MoveNumber) Alpha = true; //Alpha-beta pruning
if (CurrentTurn == 1 && Depth >= 0 && Depth < BestMovePlayer.MoveNumber) Beta = true;
if(Alpha || Beta)
{
foreach (Move TryMove in PossibleMoves) //Try every possible move to see if they are a winning move
{
int[,] Trying = new int[BoardSize, BoardSize];
Trying = (int[,])Board.Clone();
Trying[TryMove.x, TryMove.y] = CurrentTurn;
TryMove.MoveNumber = OriginalDepth - Depth;
if (Form1.Win(Trying) == 2)
{
TryMove.value = -1;
DifferentMoves.Add(TryMove);
if (Depth + 1 == OriginalDepth)
{
if (TryMove.MoveNumber < BestMoveAI.MoveNumber) BestMoveAI = TryMove;
WinningMove = true;
break;
}
else
{
WinningMove = true;
if (TryMove.MoveNumber < BestMoveAI.MoveNumber) BestMoveAI = TryMove;
return TryMove;
}
}
else if (Form1.Win(Trying) == 1)
{
WinningMove = true;
TryMove.value = 1;
BestMovePlayer = TryMove;
DifferentMoves.Add(TryMove);
return TryMove;
}
}
if (!WinningMove) // If no winning move was found, try recursively searching for a winning move
{
if (Alpha || Beta)
{
foreach (Move TryMove2 in PossibleMoves)
{
int[,] TestMove = new int[BoardSize, BoardSize];
TestMove = (int[,])Board.Clone();
TestMove[TryMove2.x, TryMove2.y] = CurrentTurn;
TryMove2.value = CalculateMoves(TestMove, BoardSize, Depth, BestMoveAI, BestMovePlayer, OriginalDepth, CurrentTurn).value;
DifferentMoves.Add(TryMove2);
}
}
}
}
//Find the best possible move and return it
BestMoveAI.value = 0;
BestMoveAI.MoveNumber = OriginalDepth;
BestMovePlayer.value = 0;
BestMovePlayer.MoveNumber = OriginalDepth;
if (CurrentTurn == 2)
{
foreach (Move AllMoves in DifferentMoves)
{
if (AllMoves.value <= BestMoveAI.value && AllMoves.MoveNumber <= BestMoveAI.MoveNumber)
{
BestMoveAI = AllMoves;
}
}
return BestMoveAI;
}
else if(CurrentTurn == 1)
{
foreach (Move AllMoves in DifferentMoves)
{
if (AllMoves.value >= BestMovePlayer.value && AllMoves.MoveNumber <= BestMovePlayer.MoveNumber)
{
BestMovePlayer = AllMoves;
}
}
return BestMovePlayer;
}
Move BadMove = new Move();
BadMove.value = 0;
BadMove.MoveNumber = Depth;
return BadMove;
}
}
}

Whats the best way to swap two ListView items in C#?

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);
}

Categories