![](http://oshiete.xgoo.jp/images/v2/pc/qa/question_title.png?e8efa67)
ArrayListのソートの件とListViewのおかしな挙動は解決できたのですが
最初に質問して解決したはずの削除の部分で再び分からなくなって
しまったので質問させて頂きます。
今回は本体に今までのコードを正式に組み込んで、動作の確認を
行っていたのですが、複数件(連続や個別選択)のデータ削除を
行おうとしたら意図しないデータ(未整列状態の並びのデータ)が
削除されてしまうという事態が発生してしまいました。
これはどうすれば解決できるのでしょうか?
また複数件の未読既読を制御(選択された項目に対してフラグを
入れ替える)するのも同様の方法で出来るのでしょうか?
環境はVS2005(C#)、.NET Framework2.0です。
以下に問題の削除時のコードを載せます。
private void menuDelete_Click(object sender, EventArgs e)
{
// 選択アイテムが0のときは反応にしない
if(listView1.SelectedItems.Count == 0){
return;
}
if(listView1.Columns[0].Text == "差出人"){
// 受信メールのとき
for (int sel_index = listView1.Items.Count - 1; sel_index > -1; sel_index--) {
if (listView1.Items[sel_index].Selected) {
collectionMail[DELETE].Add(collectionMail[RECEIVE][sel_index]);
collectionMail[RECEIVE].RemoveAt(sel_index);
}
}
this.textBody.Text = "";
}
else if(listView1.Columns[0].Text == "宛先"){
// 送信メールのとき
for (int sel_index = listView1.Items.Count - 1; sel_index > -1; sel_index--) {
if (listView1.Items[sel_index].Selected) {
collectionMail[DELETE].Add(collectionMail[SEND][sel_index]);
collectionMail[SEND].RemoveAt(sel_index);
}
}
this.textBody.Text = "";
}
else if(listView1.Columns[0].Text == "差出人または宛先"){
// 削除メールのとき
if(MessageBox.Show("選択されたメールは完全に削除されます。\nよろしいですか?", "確認", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK){
for (int sel_index = listView1.Items.Count - 1; sel_index > -1; sel_index--) {
if (listView1.Items[sel_index].Selected) {
collectionMail[DELETE].RemoveAt(sel_index);
}
}
this.textBody.Text = "";
}
}
// ツリービューとリストビューの表示を更新する
UpdateTreeView();
UpdateListView();
// 選択している位置がリストの件数よりも少ないとき
if(currentRow < listView1.Items.Count){
// 選択していた位置-1の行にフォーカスを当て直す
listView1.Items[currentRow - 1].Selected = true;
listView1.Items[currentRow - 1].Focused = true;
listView1.SelectedItems[0].EnsureVisible();
listView1.Select();
listView1.Focus();
}
else{
// リストの件数が1以上の時
if(listView1.Items.Count > 0){
// ListViewの行数位置-1の行にフォーカスを当て直す
listView1.Items[listView1.Items.Count - 1].Selected = true;
listView1.Items[listView1.Items.Count - 1].Focused = true;
listView1.SelectedItems[0].EnsureVisible();
listView1.Select();
listView1.Focus();
}
}
}
No.1ベストアンサー
- 回答日時:
ListViewのItemsのIndexと collectionMailリストのインデックスが同じ物を指しているかが問題なのでしょう
ListからListViewItemを登録する際にListViewItemのTagプロパティにListのインデックスを登録しておきます
削除する際には
for (int sel_index = listView1.Items.Count - 1; sel_index > -1; sel_index--) {
if (listView1.Items[sel_index].Selected) {
int nIndex = (int)listView1.Items[sel_index].Tag
collectionMail[DELETE].Add(collectionMail[RECEIVE][nIndex]);
//collectionMail[RECEIVE].RemoveAt(sel_index);
// 削除済みとして -にしてマークしておく
listView1.Items[sel_index].Tag = -nIndex;
collectionMail[RECEIVE][nIndex].DeleteFlag = true;
}
}
としてみてはいかがでしょう
ArrayListから登録する際にも
foreach( Mail mail in list ) {
// フラグをみてリストを追加
if ( !mail.DeleteFlag ) {
ListViewItem Item = new ListViewItem( mail.subject );
Item.SubItems.Add( mail.Address );
// ... その他のデータをついか
}
}
データをファイルに落とす際に DalateFlagがついてない物だけ書き込むようにする
等の対策でどうでしょう ...
この回答への補足
うーん、個人的にはRECEIVEリストから削除して、削除分を
DELETEリストに移動するという処理だけで良いと思うのですが・・・。
→元々のプログラムに出来るだけ手を加えないようにしています。
これだと間違った項目を消してしまう可能性があるんですよね。
→何度か試してみて何回か削除するものが異なっていた。
private void menuDelete_Click(object sender, EventArgs e)
{
// 選択アイテムが0のときは反応にしない
if(listView1.SelectedItems.Count == 0){
return;
}
if(listView1.Columns[0].Text == "差出人"){
// 受信メールのとき
for (int sel_index = listView1.Items.Count - 1; sel_index > -1; sel_index--) {
if (listView1.Items[sel_index].Selected) {
// collectionMail[DELETE].Add(collectionMail[RECEIVE][sel_index]);
// collectionMail[RECEIVE].RemoveAt(sel_index);
int nIndex = (int)listView1.Items[sel_index].Tag;
collectionMail[DELETE].Add(collectionMail[RECEIVE][nIndex]);
collectionMail[RECEIVE].RemoveAt(nIndex);
}
}
this.textBody.Text = "";
}
else if(listView1.Columns[0].Text == "宛先"){
// 送信メールのとき
for (int sel_index = listView1.Items.Count - 1; sel_index > -1; sel_index--) {
if (listView1.Items[sel_index].Selected) {
// collectionMail[DELETE].Add(collectionMail[SEND][sel_index]);
// collectionMail[SEND].RemoveAt(sel_index);
int nIndex = (int)listView1.Items[sel_index].Tag;
collectionMail[DELETE].Add(collectionMail[SEND][nIndex]);
collectionMail[SEND].RemoveAt(nIndex);
}
}
this.textBody.Text = "";
}
else if(listView1.Columns[0].Text == "差出人または宛先"){
// 削除メールのとき
if(MessageBox.Show("選択されたメールは完全に削除されます。\nよろしいですか?", "確認", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK){
for (int sel_index = listView1.Items.Count - 1; sel_index > -1; sel_index--) {
if (listView1.Items[sel_index].Selected) {
// collectionMail[DELETE].RemoveAt(sel_index);
int nIndex = (int)listView1.Items[sel_index].Tag;
collectionMail[DELETE].RemoveAt(nIndex);
}
}
this.textBody.Text = "";
}
}
// ツリービューとリストビューの表示を更新する
UpdateTreeView();
UpdateListView();
// 選択している位置がリストの件数よりも少ないとき
if(currentRow < listView1.Items.Count){
// 選択していた位置-1の行にフォーカスを当て直す
listView1.Items[currentRow - 1].Selected = true;
listView1.Items[currentRow - 1].Focused = true;
listView1.SelectedItems[0].EnsureVisible();
listView1.Select();
listView1.Focus();
}
else{
// リストの件数が1以上の時
if(listView1.Items.Count > 0){
// ListViewの行数位置-1の行にフォーカスを当て直す
listView1.Items[listView1.Items.Count - 1].Selected = true;
listView1.Items[listView1.Items.Count - 1].Focused = true;
listView1.SelectedItems[0].EnsureVisible();
listView1.Select();
listView1.Focus();
}
}
}
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- Visual Basic(VBA) ListView重複データ削除 2 2022/08/05 18:12
- JavaScript 画像の表示位置 3 2022/12/23 08:25
- C言語・C++・C# プログラミング c言語 4 2023/03/07 01:05
- Ruby pandasでsqlite3にテーブル作成・追加・読み出しでindexの取り扱い方教えてください 5 2023/03/08 09:57
- Visual Basic(VBA) RemoveDuplicatesメソッドについて教えてください。 シート1にシート2から値をもってく 1 2023/02/28 22:43
- Visual Basic(VBA) サブフォルダ(データ)にある複数の.xlsxファイルのSheet3のA2セルの値で01から左側をB2 2 2022/08/14 15:46
- Visual Basic(VBA) 集めたシートのシート名を変更したい。 下記のコードでサブフォルダにあるファイルのSheet3を集めて 6 2022/08/23 10:38
- PHP PHPでCookieを使った訪問回数について 1 2023/05/28 14:10
- C言語・C++・C# C言語のエラーについて 2 2022/07/11 13:56
- PHP imageフォルダに、画像をリサイズして保存する時のファイル名を変更したい 1 2023/05/30 11:39
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
.htaccessで特定URLからのア...
-
イントラネットURLの登録方法
-
XREA使用で作成したHTMLファイ...
-
1つ下の階層のフォルダに相対...
-
htaccessでPCと携帯の振り分け(...
-
FTPでのエラーメッセージ
-
URLに「.html」の拡張子がなく...
-
フォルダ内の最初のファイル名...
-
URLの書き方
-
HTML5
-
index.htmlファイルはどこに?
-
リンク先に日本語フォルダがあ...
-
ホームを index.html から inde...
-
アドレスの「index.html」の表...
-
複数のindex.htmlを作ってもい...
-
FTPしたのに、ページが真っ白です
-
.htaccessを使用した携帯端末の...
-
HTMLからフォルダを開きたい
-
Excelで、社外秘(閲覧のみ)と...
-
googleドライブで、PDFファイル...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
1つ下の階層のフォルダに相対...
-
リンク先に日本語フォルダがあ...
-
URLに「.html」の拡張子がなく...
-
複数のindex.htmlを作ってもい...
-
フォルダ内の最初のファイル名...
-
XREA使用で作成したHTMLファイ...
-
.htmと.htmlの違い
-
URLの書き方
-
サーバー内のフォルダにある写...
-
1つのドメインHP内に2つ<inde...
-
index.htmlファイルはどこに?
-
トップページ「/index.html」の...
-
webサイトの「index」を非表示...
-
URLの.html省略について
-
htmlでリンクに勝手にindex.htm...
-
フォルダにはダミーでもindex.h...
-
旧URLからのサーバー移転でhttp...
-
Application.OnTimeで引数付き...
-
Apacheで、indexの拡張子別に優...
-
ホームページを作成したら、フ...
おすすめ情報