【问题标题】:How to call a class from a winform? [closed]如何从winform调用一个类? [关闭]
【发布时间】:2016-07-11 08:47:18
【问题描述】:

对不起,我故意使用这个标题。我对 c# 很陌生,而且我不能以通常的方式给我的班级打电话。

我有一个 winform,我想插入一个类(从 API 复制和粘贴),然后从我的 winform 调用它。

这个类是一个程序,它打开控制台,问我参数,然后午餐肌电图采集。

课程是这样的:

  class Program1     
        {
            static void Main(string[] args)
            { 
// some code here, with  a lot of "Console.Writeline"
            }
        }

效果很好。

我改成:

  public class Program1     
            {
               public void method1(string[] args)
                { 
//some code here, , with  a lot of "Console.Writeline"
                }
            }

我试图在我的表单上调用它

 private void button1_Click(object sender, EventArgs e)
        {
            Program1 program = new Program1();
            program.method1();
        }

它说“方法'method1'没有重载需要0个参数”。 我不明白的是,当我单独运行 API 的程序时,它不会问我任何它只是运行的东西。

我很确定错误来自Console.WriteLineConsole.Readline,但我不知道如何解决我的问题。 我想要我的 winform 上的一个按钮来运行我的课程,打开控制台并在控制台上询问我想要哪些参数。

谢谢!

【问题讨论】:

  • 不要忘记“string[] args”,您的输入不是无效的。 "program.method1(new string[] {"a","b","c"});"
  • 这是一个问题,因为即使您调用该方法,您也没有控制台,因为您正在运行一个 winforms 应用程序。
  • 好的,程序现在运行(谢谢)!但是什么也没发生,它只是冻结了!!
  • 你不能在winform上调用控制台?!为什么不呢?!
  • 你可以,检查thisthis

标签: c# winforms console


【解决方案1】:

方法重载是 OOP 中的一个特性。它允许您编写多个同名但具有不同参数的方法,在调用期间它会根据参数选择正确的方法。

你可以试试

string[] args 的指定参数作为例如传递。

public class Program1     
{
    public void method1(string[] args){ 
        //some code here, , with  a lot of "Console.Writeline"
    }
}

private void button1_Click(object sender, EventArgs e){
    Program1 program = new Program1();
    program.method1(new string[]{"one","two"});
}

或者尝试使用可选参数,例如。

public class Program1     
{
    public void method1(string[] args=null){ 
        //some code here, , with  a lot of "Console.Writeline"
    }
}

private void button1_Click(object sender, EventArgs e){
    Program1 program = new Program1();
    program.method1();
}

【讨论】:

  • 他也可以使用无参数方法public void method1()
  • 是的,实际上很好,谢谢!我不明白他们为什么放 (string[] args) 然后..
猜你喜欢
  • 1970-01-01
  • 2021-09-09
  • 1970-01-01
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
  • 2014-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多