提示コードのMainWindow.cs コメント部内部のコードですがcomboBox_extension.SelectedIndex = -1;
としても未選択状況にならない原因が知りたいです。
環境
OS: windows10
IDE: visual studio 2022
.Net 6.0
コードの内容
URLを解析してダウンロード可能な拡張子とそのフォーマット形式をリストアップする処理です。
調べたこと
コンソールログを出してコードがそのif文に達しているかどうかを確認
参考サイトを調べてSelectedIndex= -1をすると未選択状況になることを確認
ソースコード全文
https://33.gigafile.nu/0529-c4ec7d1524b533a07b7f9f8a9adf85af0
参考サイト
参考サイト:https://ryoryoyohb.hatenablog.com/entry/2017/09/04/192912
リファレンス: https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.combobox.selectedindex?view=windowsdesktop-6.0&viewFallbackFrom=net-6.0
出力コンソール画面
スレッド 0x36b8 はコード 0 (0x0) で終了しました。 【Hans1126】【MAD】アルストロメリア(ALSTROEMERIA)-アルストロメリア(Alstroemeria)IDOLM@STER SHINY COLORS 偶像大師 閃耀色彩 m4a おおおおおおおお rrrrrrrrr あああああ OH MY GOD ー SHHis あああああ -1 あああああ 【Hans1126】【MAD】アルストロメリア(ALSTROEMERIA)-アルストロメリア(Alstroemeria)IDOLM@STER SHINY COLORS 偶像大師 閃耀色彩 m4a おおおおおおおお rrrrrrrrr あああああ OH MY GOD ー SHHis あああああ -1 あああああ プログラム '[8136] Downloader.exe' はコード 0 (0x0) で終了しました。
Data.cs
cs
1using System.Diagnostics; 2 3namespace Downloader 4{ 5 6 public enum CodecType 7 { 8 Audio, 9 Movie, 10 Audio_Movie 11 12 } 13 14 15 public struct Format 16 { 17 public string? id; 18 public string? extension; 19 public string? resolution; 20 public string? special_Resolution; //両方あるときは音声 21 22 public float? size; 23 public CodecType codecType; 24 25 } 26 27 public class Data 28 { 29 private Process process = new Process(); 30 31 32 33 private string url; 34 private string title; 35 private List<Format> format; 36 private string? format_audio = null; 37 private string? format_movie = null; 38 private string? format_extension = null; 39 40 /* ########################### 初期化 ###########################*/ 41 public Data(string t,string u,List<Format> f) 42 { 43 //省略 44 45 title = t; 46 url = u; 47 format = f; 48 } 49 50 /* ########################### Audioフォーマット取得 ###########################*/ 51 public string? getAudio_Format() 52 { 53 return format_audio; 54 } 55 56 /* ########################### Movieフォーマット取得 ###########################*/ 57 public string? getMovie_Format() 58 { 59 return format_movie; 60 } 61 62 /* ########################### 拡張子フォーマット取得 ###########################*/ 63 public string? getExtension_Format() 64 { 65 return format_extension; 66 } 67 68 /* ########################### Audioフォーマット設定 ###########################*/ 69 public void setAudio_Format(string? a) 70 { 71 format_audio = GetFormatAudio_ID(a); 72 } 73 74 /* ########################### Movieフォーマット設定 ###########################*/ 75 public void setMovie_Format(string? m) 76 { 77 format_movie = GetFormatMovie_ID(m); 78 } 79 80 public void Run() 81 { 82 if(format_movie == null) 83 { 84 process.StartInfo.Arguments = " -x " + "ytsearch:" + format_audio + " " + url + " " + " -o \" %(title)s\" "; 85 } 86 87 88 89 process.Start(); 90 91 process.BeginOutputReadLine(); 92 process.BeginErrorReadLine(); 93 94 process.WaitForExit(); 95 96 process.CancelOutputRead(); 97 process.CancelErrorRead(); 98 } 99 100 /* ########################### 出力 ###########################*/ 101 public void Output(object sender, DataReceivedEventArgs e) 102 { 103 Debug.WriteLine(e.Data); 104 } 105 106 /* ########################### エラー出力 ###########################*/ 107 public void Output_Error(object sender, DataReceivedEventArgs e) 108 { 109 110 } 111 }; 112}
MainWindow.cs
cs
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Diagnostics; 7using System; 8using System.IO; 9 10namespace Downloader 11{ 12 public partial class MainWindow : Form 13 { 14 List<Data> downloadList = new List<Data>(); 15 Data data; 16 string? url = null; 17 18 /* ########################### コンストラクタ ###########################*/ 19 public MainWindow() 20 { 21 InitializeComponent(); //ウインドウ初期化 22 Converter.Init(); //初期化 23 } 24 25 /* ########################### URLを入力した時 ###########################*/ 26 private void textBox_url_TextChanged(object sender, EventArgs e) 27 { 28 url = textBox_url.Text; 29 } 30 31 /* ########################### URLでエンターを押したとき ###########################*/ 32 private void textBox_url_KeyDown(object sender, KeyEventArgs e) 33 { 34 if(e.KeyCode == Keys.Enter) 35 { 36 if((url != null) && (url != "")) 37 { 38 Data data = Converter.GetData(url); 39 if(data == null) 40 { 41 MessageBox.Show("URL ERROR", "", MessageBoxButtons.OK, MessageBoxIcon.Error); 42 } 43 else 44 { 45 downloadList.Add(data); //ダウンロードリストに追加 46 textBox_url.Text = null; //テキストボックスを空にする 47 48 checkedListBox_list.Items.Add(data.getTitle(),true); //チェックボックス挿入 49 } 50 } 51 else 52 { 53 MessageBox.Show("URL NO INPUT", "", MessageBoxButtons.OK, MessageBoxIcon.Warning); 54 } 55 } 56 } 57 58 /* ########################## 項目リスト再設定 ###########################*/ 59 private void ReSetList(List<Format> list) 60 { 61 //リストをリセット 62 comboBox_extension.Items.Clear(); 63 comboBox_audio.Items.Clear(); 64 comboBox_movie.Items.Clear(); 65 66 //リスト設定 67 foreach (Format f in list) 68 { 69 if (f.codecType == CodecType.Audio) 70 { 71 comboBox_audio.Items.Add(f.resolution); //オーディオ 72 } 73 else if (f.codecType == CodecType.Movie) 74 { 75 comboBox_movie.Items.Add(f.resolution); //映像 76 } 77 78 79 comboBox_extension.Items.Add(f.extension); //拡張子 80 } 81 82 } 83 84 85 /* ########################## ダウンロードリストを選択した時 ###########################*/ 86 private void checkedListBox_list_Click(object sender, EventArgs e) 87 { 88 if(0 <= checkedListBox_list.SelectedIndex) 89 { 90 data = downloadList[checkedListBox_list.SelectedIndex]; 91 ReSetList(data.getFormat()); //項目を再設定 92////////////////////////////////////////////////////////////////////////////////////////////////////////////// 93 Debug.WriteLine(data.getTitle()); 94 Debug.WriteLine(data.getExtension_Format()); 95 96 if(data.getExtension_Format() == null) 97 { 98 Debug.WriteLine("あああああ"); 99 comboBox_extension.SelectedValue = ""; 100 101 comboBox_extension.SelectedIndex = -1; 102 103 Debug.WriteLine(comboBox_extension.SelectedIndex); 104 105 106 } 107 else 108 { 109 Debug.WriteLine("おおおおおおおお"); 110 comboBox_extension.SelectedItem = data.getExtension_Format(); 111 } 112 113 comboBox_audio.SelectedItem = data.getAudio_Format(); 114 comboBox_movie.SelectedItem = data.getMovie_Format(); 115/////////////////////////////////////////////////////////////////////////////////////////////////////////////// 116 } 117 } 118 119 /* ########################## オーディオファイル時 項目設定 ###########################*/ 120 private void SetIsAudioFile(string ext) 121 { 122 if (Converter.GetIsAudioExtension(ext) == true) 123 { 124 comboBox_movie.Items.Clear(); 125 comboBox_movie.Items.Add("audio only"); 126 comboBox_movie.SelectedIndex = 0; 127 128 comboBox_movie.Enabled = false; 129 } 130 else 131 { 132 comboBox_movie.Enabled = true; 133 } 134 } 135 136 137 138 /* ########################## 拡張子リストを選択 ###########################*/ 139 private void comboBox_extension_SelectionChangeCommitted(object sender, EventArgs e) 140 { 141 Data data = downloadList[checkedListBox_list.SelectedIndex]; 142 comboBox_audio.SelectedItem = data.getAudio_Format(); //オーディオを設定 143 comboBox_movie.SelectedItem = data.getMovie_Format(); //映像を設定 144 145 SetIsAudioFile(comboBox_extension.SelectedItem.ToString()); //オーディオファイル時の選択項目設定 146 data.setExtension_Format(comboBox_extension.SelectedItem.ToString()); //データに設定 147 } 148 149 /* ########################## Audioリストを選択 ###########################*/ 150 private void comboBox_audio_SelectionChangeCommitted(object sender, EventArgs e) 151 { 152 data.setAudio_Format(comboBox_audio.SelectedItem.ToString()); //データに設定 153 } 154 155 /* ########################## Movieリストを選択 ###########################*/ 156 private void comboBox_movie_SelectionChangeCommitted(object sender, EventArgs e) 157 { 158 data.setMovie_Format(comboBox_movie.SelectedItem.ToString()); //データに設定 159 } 160 } 161} 162
