質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

3回答

2492閲覧

C# リスト検索の方法

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2017/05/26 03:01

編集2017/05/26 03:41

C#

1List<string> list = new List<string>; 2list.Add("A"); 3list.Add("ABC"); 4list.Add("RAND") 5list.Add("HELLO");

リストに上記をAddします。
そして、textBoxにaと入力しbuttonを押すとaが含まれている要素をすべてComboBoxに表示させるにはどうすればよいのでしょうか?(小文字、大文字は関係なし)

namespace WindowsFormsApplication3 { public partial class Form1 : Form { List<string> list = new List<string>(); public Form1() { InitializeComponent(); list.Add("A"); list.Add("ABC"); list.Add("RAND"); list.Add("HELLO"); } private void button1_Click(object sender, EventArgs e) { if (list.Contains(textBox1.Text)) { comboBox1.Text = } } } }

=の後がわからないです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2017/05/26 03:34

自分で実装してみて、どこでつまづいているのか、コードをアップして説明できないですか?
guest

回答3

0

ベストアンサー

概ねこんな感じかなと思います。

csharp

1 2using System; 3using System.Collections.Generic; 4using System.Linq; 5using System.Windows.Forms; 6 7namespace WindowsFormsApp1 8{ 9 public partial class Form1 : Form 10 { 11 private readonly List<string> _list = new List<string> 12 { 13 "A", 14 "ABC", 15 "RAND", 16 "HELLO" 17 }; 18 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 24 private void button1_Click(object sender, EventArgs e) 25 { 26 comboBox1.Items.Clear(); 27 comboBox1.Text = ""; 28 29 30 foreach (var str in _list.Where(x => x.ToLower().Contains(textBox1.Text.ToLower()))) 31 comboBox1.Items.Add(str); 32 33 if (comboBox1.Items.Count != 0) comboBox1.Text = (string) comboBox1.Items[0]; 34 } 35 36 37 } 38} 39

_list内の文字列が半角英数で有ればうまく行きますが、全角英数が入ったら破綻します。(半角・全角のCase-sensitivityは存在するので)

また、TextBoxが空の場合は、すべてが引っかかりますのでその点もご注意の程。


ココよりコメント受けての追記

foreach以降は

LINQを使って要素の抽出をしています。
その子細に関しては、ココではオフトピックとなりますので、リンク先をご参照ください。

前方一致させる場合は、string.IndexOfメソッドを使い、結果が0で有れば、前方一致していることになるので、下記のようになります。

以下のサンプルコードは

  1. button1は最初のまま、含まれていればマッチ
  2. button2はLINQを使って前方一致を実装
  3. button3はLINQを使わずに実装

となります。

csharp

1 2using System; 3using System.Collections.Generic; 4using System.Linq; 5using System.Windows.Forms; 6 7namespace WindowsFormsApp3 8{ 9 public partial class Form1 : Form 10 { 11 private readonly List<string> _list = new List<string> 12 { 13 "A", 14 "ABC", 15 "RAND", 16 "HELLO" 17 }; 18 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 24 /// <summary> 25 /// 含まれていればOK 26 /// </summary> 27 /// <param name="sender"></param> 28 /// <param name="e"></param> 29 private void button1_Click(object sender, EventArgs e) 30 { 31 comboBox1.Items.Clear(); 32 comboBox1.Text = ""; 33 34 35 foreach (var str in _list.Where(x => x.ToLower().Contains(textBox1.Text.ToLower()))) 36 comboBox1.Items.Add(str); 37 38 if (comboBox1.Items.Count != 0) comboBox1.Text = (string) comboBox1.Items[0]; 39 } 40 41 /// <summary> 42 /// 前方一致 43 /// </summary> 44 /// <param name="sender"></param> 45 /// <param name="e"></param> 46 private void button2_Click(object sender, EventArgs e) 47 { 48 //ComboBoxを初期化 49 comboBox2.Items.Clear(); 50 comboBox2.Text = ""; 51 52 //検索対象の文字列を取得 53 var matchStr = textBox1.Text.ToLower(); 54 55 //LINQを利用したフィルタリング 56 57 var resultSeq = _list.Where(x => x.ToLower().IndexOf(matchStr) == 0); 58 59 //ComboBox.Itemsに結果を追加 60 foreach (var elem in resultSeq) 61 comboBox2.Items.Add(elem); 62 63 //抽出結果が0出なければ最初の要素をテキストボックスに設定 64 if (comboBox2.Items.Count != 0) comboBox2.Text = (string) comboBox2.Items[0]; 65 } 66 67 /// <summary> 68 /// 前方一致をLINQつか言わないで書いてみた。 69 /// </summary> 70 /// <param name="sender"></param> 71 /// <param name="e"></param> 72 private void button3_Click(object sender, EventArgs e) 73 { 74 //ComboBoxを初期化 75 comboBox2.Items.Clear(); 76 comboBox2.Text = ""; 77 78 //検索対象の文字列を取得 79 var matchStr = textBox1.Text.ToLower(); 80 81 //LINQを利用したフィルタリング 82 //ComboBox.Itemsに結果を追加 83 foreach (var elem in _list) 84 { 85 //小文字に正規化 86 var regularize = elem.ToLower(); 87 88 //前方一致するか判定.マッチしたらコンボボックスに追加 89 if (regularize.IndexOf(matchStr) == 0) comboBox2.Items.Add(elem); 90 } 91 92 //抽出結果が0出なければ最初の要素をテキストボックスに設定 93 if (comboBox2.Items.Count != 0) comboBox2.Text = (string) comboBox2.Items[0]; 94 } 95 } 96} 97 98

補足ですが、ComboBoxのListBoxに要素を追加したいなら、ComboBox.Items.Addメソッドを利用して追加し、TextBox部分に、表示したいのなら、ComboBox.Textプロパティに文字列を指定する必要があります。

また蛇足ながら、初期の質問に対する回答に対する貴方のコメントは下の質問からずいぶん離れたイメージを個人的に持ちました。(現に元の質問からずいぶん質問そのものが変化していますよね?)
なので、別に質問をすべきではなかったのかなと思います。

投稿2017/05/26 03:46

編集2017/05/26 06:21
Tokeiya3

総合スコア260

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2017/05/26 05:50

ありがとうございます。 この方法で前方一致をするにはどうすればよいのでしょうか?? forreach以降が全くわかないので簡単に教えていただけると幸いです。 よろしくお願いいたします。
退会済みユーザー

退会済みユーザー

2017/05/26 07:21

大変失礼いたしました。 本当にありがとうございました。
guest

0

<Window x:Class="listtest2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:listtest2" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox Name="txtbox" HorizontalAlignment="Center" Height="23" Margin="0,86,0,0" TextWrapping="NoWrap" VerticalAlignment="Top" Width="120"/> <Button Content="ボタン" HorizontalAlignment="Center" Margin="0,133,0,0" VerticalAlignment="Top" Width="75" Click="List_Add"/> <ComboBox Name="combox" HorizontalAlignment="Center" Margin="0,173,0,0" VerticalAlignment="Top" Width="120"/> </Grid> </Window>

C#

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Windows; 5 6namespace listtest2 7{ 8 /// <summary> 9 /// MainWindow.xaml の相互作用ロジック 10 /// </summary> 11 public partial class MainWindow : Window 12 { 13 public MainWindow() 14 { 15 InitializeComponent(); 16 } 17 private void List_Add(object sender, RoutedEventArgs e) 18 { 19 List<string> list = new List<string>(); 20 list.Add("A"); 21 list.Add("ABC"); 22 list.Add("RAND"); 23 list.Add("HELLO"); 24 25 List<string> data(List<string> input, string search) 26 { 27 return input.Where(x => x.ToUpper().Contains(search.ToUpper())).ToList(); 28 } 29 30 combox.ItemsSource = data(list, txtbox.Text); 31 } 32 } 33}

考えてみました。

投稿2017/05/26 03:41

編集2017/05/26 03:49
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

大文字小文字の区別をしないのなら比較する双方をToLower()等で統一して比較すればどうでしょうか。
また文字列の中に文字列が含まれるか判断するのは str.Contains(txt) のようにして真ならstrの中にtxtが含まれます。
Listの中から条件に合致する物を全部抽出するなら FindAll でいいと思います。

投稿2017/05/26 03:40

HiroshiWatanabe

総合スコア2160

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問