top of page

Visual Basicをやめて、Visual C#に移行した

執筆者の写真: snackvirtualsnackvirtual

更新日:2022年6月13日


まあそうだろうなあ

仕事では使いやすいVisual Basicを使っていたが、

ちょうどいい機会なので、Visual C#に挑戦しよう

ついでにVisual Studio 2022にしてしまえ~


ついでにEnterキーで動作するようにして、ビープ音もならないようにした

namespace Python_Execute_Test
{
    using System.Diagnostics;
    public partial class Form1 : Form
    {
        int picturebox1_state = 0;
        public Form1()
        {
            InitializeComponent();
            pictureBox1.ImageLocation = "C:/Users/Hideo/Downloads/hiyori_free_jp/hiyori_free_jp/Scene1.gif";

        }


        private void call_python()
        { 

            picturebox1_state = (picturebox1_state + 1) % 3;

            if (picturebox1_state == 0) { pictureBox1.ImageLocation = "C:/Users/Hideo/Downloads/hiyori_free_jp/hiyori_free_jp/Scene1.gif"; }
            if (picturebox1_state == 1) { pictureBox1.ImageLocation = "C:/Users/Hideo/Downloads/hiyori_free_jp/hiyori_free_jp/Scene2.gif"; }
            if (picturebox1_state == 2) { pictureBox1.ImageLocation = "C:/Users/Hideo/Downloads/hiyori_free_jp/hiyori_free_jp/Scene3.gif"; }

            label1.Text = picturebox1_state.ToString();

            string a = textBox1.Text;

            // プロセス開始情報クラスを設定する。
            // 引数配列よりコマンド配列(空白繋ぎ)に変換し設定する

            ProcessStartInfo psInfo = new ProcessStartInfo();

            psInfo.FileName = "C:/ProgramData/Anaconda3/python.exe";
            psInfo.Arguments = "C:/Users/Hideo/Documents/test1.py" + " " + a;
            psInfo.RedirectStandardOutput = true;
            psInfo.UseShellExecute = false;
            psInfo.CreateNoWindow = true;

            // プロセスを開始
            Process ps = Process.Start(psInfo);

            // 結果を取得する。
            string calcResult = ps.StandardOutput.ReadToEnd();

            // プロセス終了を待機する
            ps.WaitForExit();
            ps.Close();

            textBox2.AppendText(calcResult);

        }


        private void button1_Click(object sender, EventArgs e)
        {
            call_python();
        }

        private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // デザインでTextboxのプロパティのイベントで、キー KeyPress でイベント名を設定する必要あり!!!!!!!
            // Enterキー以外なら抜ける
            if (e.KeyChar == 13)
                // EnterキーでBeep音がしないようにする
                e.Handled = true;

                call_python();
        }
    }
}


閲覧数:0回0件のコメント

最新記事

すべて表示

Comentarios


bottom of page