我的代码有一个小问题,我想在静态void函数中使用两个变量字符串numberTextBox1和numberTextBox2替换变量chiffre_1和chiffre_2
我看过将字符串转换为var ==>失败 我试图将变量numberTextBox 1和2 ==>设置为函数参数 我试图将函数变量设置为等于TextBox1.Text和TextBox2.Text ==>失败
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IronPython;
using IronPython.Hosting;
using System.IO;
namespace CSharp_with_Python_Script
{
public partial class Form1 : Form
{
string nombreTextbox1;
string nombreTextbox2;
string chiffre_1;
string chiffre_2;
public Form1()
{
InitializeComponent();
}
static void Execute(nombreTextBox1,
nombreTextBox2)
{
var psi = new ProcessStartInfo();
psi.FileName =
@"C:\Users\adm\AppData\Local\Programs\Python\Python37-32\python.exe";
var script = @"C:\Users\adm\Documents\Visual Studio
2017\Projects\CSharp_with_Python_Script\Class_Plot\Class_Plot.py";
var chiffre_1 = nombreTextBox1;
var chiffre_2 = nombreTextBox2;
psi.Arguments = $"\"{script}\" \"{chiffre_1}\" \"{chiffre_2}\"";
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
var errors = "";
var result = "";
using (var process = Process.Start(psi))
{
errors = process.StandardError.ReadToEnd();
result = process.StandardOutput.ReadToEnd();
}
}
private void Btn_ChangePicture_Click(object sender, EventArgs e)
{
pictureBox1.ImageLocation = (@"C:\Users\adm\Documents\Visual
Studio
2017\Projects\CSharp_with_Python_Script\Class_Plot\PLOT_MATPLOTLIB.png");
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void Btn_Script_Click(object sender, EventArgs e)
{
Execute();
}
private void button1_Click(object sender, EventArgs e)
{
nombreTextbox1 = textBox1.Text;
nombreTextbox2 = textBox2.Text;
}
}
}
我希望函数的变量chiffre_1和chiffre_2等于在函数Execute中输入的变量
Ps:我是C#的初学者,并且是函数的新手
答案 0 :(得分:0)
尝试
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IronPython;
using IronPython.Hosting;
using System.IO;
namespace CSharp_with_Python_Script
{
public partial class Form1 : Form
{
string nombreTextbox1;
string nombreTextbox2;
string chiffre_1;
string chiffre_2;
public Form1()
{
InitializeComponent();
}
static void Execute()
{
chiffre_1 = nombreTextBox1;
chiffre_2 = nombreTextBox2;
var psi = new ProcessStartInfo();
psi.FileName =
@"C:\Users\adm\AppData\Local\Programs\Python\Python37-32\python.exe";
var script = @"C:\Users\adm\Documents\Visual Studio
2017\Projects\CSharp_with_Python_Script\Class_Plot\Class_Plot.py";
var chiffre_1 = nombreTextBox1;
var chiffre_2 = nombreTextBox2;
psi.Arguments = $"\"{script}\" \"{chiffre_1}\" \"{chiffre_2}\"";
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
var errors = "";
var result = "";
using (var process = Process.Start(psi))
{
errors = process.StandardError.ReadToEnd();
result = process.StandardOutput.ReadToEnd();
}
}
private void Btn_ChangePicture_Click(object sender, EventArgs e)
{
pictureBox1.ImageLocation = (@"C:\Users\adm\Documents\Visual
Studio
2017\Projects\CSharp_with_Python_Script\Class_Plot\PLOT_MATPLOTLIB.png");
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void Btn_Script_Click(object sender, EventArgs e)
{
Execute();
}
private void button1_Click(object sender, EventArgs e)
{
nombreTextbox1 = textBox1.Text;
nombreTextbox2 = textBox2.Text;
}
}
}
在抛出事件button1_Click
时,将其设置为nombreTextbox1
和nombreTextbox2
,然后Btn_Script_Click
启动Execute()
方法并使用私有属性来设置chiffre_1
和chiffre_2
使用新值。