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

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

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

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

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

UI

UIはUser Interfaceの略であり、人間がコンピュータとやりとりをするためのシステムです。

Q&A

解決済

1回答

3808閲覧

visualstudio・C#でのUIの作成について

退会済みユーザー

退会済みユーザー

総合スコア0

C#

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

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

UI

UIはUser Interfaceの略であり、人間がコンピュータとやりとりをするためのシステムです。

0グッド

1クリップ

投稿2018/01/09 12:34

編集2018/01/10 01:50

###前提・実現したいこと
現在、音声認識を利用したデスクトップアプリケーションを作成しています。
作成している内容については声でiPhoneを操作できる「Siri」のようにデスクトップ上でも声を使い、アプリを立ち上げる(終了させる)などの操作が出来るというものです。
基礎となるプログラムは、あらかた完成したのですがこのアプリを操作するためのUIの作成に苦戦しています。

完成させたい操作手順は以下の通りです。
1.アプリケーションのアイコンを指定のボックスにD&Dしてパスを取得する。
2.テキストボックスにアプリケーションを起動する際に発する言葉を入力する。
3.OKボタンを押すとアプリケーションのパスと入力された言葉を辞書ファイルに登録する。
4.マイクに向かって登録した言葉を発すると指定のアプリケーションを起動する。

1項目のD&Dして指定のアプリケーションのパスを取得することは、できましたがその取得したパスと起動するときに発する言葉を「Form1.cs」の「アプリの追加」項目への追加。「grammar.xml」のファイルには、起動するときに発する言葉のみを追加したいのですがその追加方法がわかりません。
目指したい完成形としては、取得したパスと起動ワードをテキストボックスに入力し、「登録」ボタンを押すと追加される。追加されたパスとワードが下のリストに追加され、そのリストからパスや登録ワードの変更が出来るというものです。
このやり方をお教えてほしいです。よろしくお願いします。

下の画像は現在作成途中のUIになります。
また、何か追加した方が良いと思う機能がありましたら意見としてコメントいただけると幸いです。

イメージ説明

###Form1.cs

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Speech.Recognition; using System.Xml; using System.Diagnostics; using System.Speech; namespace xxx { public partial class Form1 : Form { public Form1() { InitializeComponent(); { SpeechRecognitionEngine engine = new SpeechRecognitionEngine(); engine.SpeechRecognized += delegate(object sender, SpeechRecognizedEventArgs e) { int hour = DateTime.Now.Hour; //現在時刻(hour)を答える string min = DateTime.Now.Minute.ToString("d"); //現在時刻(min)を答える { if (e.Result.Confidence > 0.4f) //音声の聞き取り精度(0.1~1.0f) switch (e.Result.Text) { //時間 case "今何時"://ここに命令 if (hour < 13) { //am System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\hour\am.wav"); System.Threading.Thread.Sleep(1000); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\hour\" + hour + ".wav"); System.Threading.Thread.Sleep(500); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\hour\時.wav"); System.Threading.Thread.Sleep(1000); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\min\" + min + ".wav"); System.Threading.Thread.Sleep(1200); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\min\分.wav"); } else { //pm System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\hour\" + hour + ".wav"); System.Threading.Thread.Sleep(1000); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\hour\" + hour + ".wav"); System.Threading.Thread.Sleep(500); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\hour\時.wav"); System.Threading.Thread.Sleep(1000); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\min\" + min + ".wav"); System.Threading.Thread.Sleep(1200); System.Diagnostics.Process.Start(@"C:\Users\xxx\Desktop\sozai\toki\min\分.wav"); } break;           //アプリの追加 case "ワード"://ここに命令 System.Diagnostics.Process.Start(@"C:\xxx...");   break; case "エクセル"://ここに命令  System.Diagnostics.Process.Start(@"C:\xxx...");  break; case "パワポ"://ここに命令 case "パワーポイント": System.Diagnostics.Process.Start(@"C:\xxx..."); break;             case "〇〇終了"://ここに命令 case "〇〇閉じる": System.Diagnostics.Process.Start(@"C:\xxx..."); Application.Exit(); //このアプリを終了する break; } } }; Grammar g = new Grammar(@"C:\Users\xxx\Documents\Visual Studio 2015\Projects\xxx\grammar.xml", "command"); engine.LoadGrammar(g); engine.LoadGrammar(new DictationGrammar()); engine.SetInputToDefaultAudioDevice(); engine.RecognizeAsync(RecognizeMode.Multiple); } } } } private void Form1_Load(object sender, EventArgs e) { } //ユーザーインターフェース private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.listBox1 = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // textBox1 // this.textBox1.AllowDrop = true; this.textBox1.Location = new System.Drawing.Point(12, 12); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(456, 19); this.textBox1.TabIndex = 0; this.textBox1.Text = "登録したいアプリをD&Dしてください"; this.textBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop); this.textBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter); // // textBox2 // this.textBox2.Location = new System.Drawing.Point(12, 37); this.textBox2.Multiline = true; this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(456, 19); this.textBox2.TabIndex = 1; this.textBox2.Text = "登録ワードを入力してください"; // // button1 // this.button1.Location = new System.Drawing.Point(393, 62); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 2; this.button1.Text = "クリア"; this.button1.UseVisualStyleBackColor = true; // // button2 // this.button2.Location = new System.Drawing.Point(312, 62); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 3; this.button2.Text = "登録"; this.button2.UseVisualStyleBackColor = true; // // listBox1 // this.listBox1.FormattingEnabled = true; this.listBox1.ItemHeight = 12; this.listBox1.Location = new System.Drawing.Point(12, 95); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(456, 88); this.listBox1.TabIndex = 4; // // Form1 // this.ClientSize = new System.Drawing.Size(618, 195); this.Controls.Add(this.listBox1); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Load += new System.EventHandler(this.Form1_Load_1); this.ResumeLayout(false); this.PerformLayout(); } //TextBoxのD&D private void TextBox_DragEnter(object sender, DragEventArgs e) { //ファイルがドラッグされている場合、カーソルを変更する if (e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.Copy; } } private void TextBox_DragDrop(object sender, DragEventArgs e) { //ドロップされたファイルの一覧を取得 string[] fileName = (string[])e.Data.GetData(DataFormats.FileDrop, false); if (fileName.Length <= 0) { return; } // ドロップ先がTextBoxであるかチェック TextBox txtTarget = sender as TextBox; if (txtTarget == null) { return; } //TextBoxの内容をファイル名に変更 txtTarget.Text = fileName[0]; } private void Form1_Load_1(object sender, EventArgs e) { } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { } } }

###grammar.xml

<?xml version="1.0" encoding="UTF-8"?> <grammar xml:lang="ja-JP" root="command" tag-format="semantics/1.0" xmlns="http://www.w3.org/2001/06/grammar" version="1.0"> <rule scope="public" id="command"> <one-of>     <item>今何時</item>    <item>ワード</item>       <item>エクセル</item>       <item>パワポ</item>       <item>パワーポイント</item> </one-of> </rule> </grammar>

###補足情報(言語/FW/ツール等のバージョンなど)
開発環境:visualstudio2015
開発言語:C#
OS:Windows8.1

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

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

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

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

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

alg

2018/01/09 12:57

提示された「grammar.xml」には、「登録ワード」に該当する情報は含まれているようですが、対応するアプリのパスは含まれていないように見えます。パスはどのように格納する仕様でしょうか?
退会済みユーザー

退会済みユーザー

2018/01/09 13:15

すみません私の書き方が悪かったです。「grammar.xml」は、「登録ワード」用の辞書ファイルになりますのでパスの格納は、必要ないです。
episteme

2018/01/10 01:46

下世話ながら: × ディスクトップ(disktop) ○ デスクトップ(desktop)
退会済みユーザー

退会済みユーザー

2018/01/10 01:51

ご指摘ありがとうございます!修正いたしました。
guest

回答1

0

ベストアンサー

現状ですと、UIにてデータを追加するという処理を作成するより先に
現在固定で行っているものをデータで格納し、処理を行えるようにした
ほうがよいかと思います。

例えばですが、

C#

1struct CommandData 2{ 3 public string Command; 4 public string Path; 5 6 public CommandData(string command,string path) 7 { 8 this.Command = command; 9 this.Path = path; 10 } 11} 12 13List<CommandData> commandlist = new List<CommandData>{ 14 new CommandData("ワード",@"C:\xxx..."), 15 new CommandData("エクセル",@"C:\xxx..."), 16 new CommandData("パワポ",@"C:\xxx...") 17};

みたいな感じ(適当に書いたので文法上おかしい部分があるかもしれません)で、
データを格納するものをコードで定義してやり、現在switchで固定文字列で行って
いる部分を、これで処理を行うようにしてみましょう。

これが出来たら、UIからこのデータを追加する処理を追加していけばいいと思います。

また、辞書ですが、現在のように単語みたいな感じで認識させるのであれば
xmlを使わずに、

C#

1Choices choices = new Choices("ワード","エクセル","パワポ"); 2engine.LoadGrammar(new Grammar(new GrammarBuilder(choices)));

みたいな感じで動的に辞書を作成したほうがいいかもしれません。

投稿2018/01/09 13:46

YAmaGNZ

総合スコア10242

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問