C#中控制台代码如何转换为windows窗体程序代码

zhuhuanlai 2016-12-21 02:52:14
请教一下大侠,有一段控制台代码运行结果正确,我想做个程序界面,把X.i = 3; X.rr = 6.0;从界面输入,同时把Y.rr从界面输出。我的思路是先把这段控制台代码转换为windows窗体程序代码(以便设计窗体和控件),该如何实现啊?
控制台代码如下:
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public struct inx
{
public int i;
public double rr;
}
public struct outx
{
public int a;
public double rr;
}
[DllImport("Dll1")]
extern static int myfortran(ref inx A, ref outx B);
static void Main(string[] args)
{
inx X;
outx Y;
X.i = 3;
X.rr = 6.0;
Y.a = 1;
Y.rr = 3.0;
int c = myfortran(ref X, ref Y);
Console.WriteLine("c# return:");
Console.WriteLine(Y.rr);
Console.Read();
}
}
}
windows窗体程序代码(新建窗体程序时自带模板,我不知道在这个模板上如何修改):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
...全文
1298 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
HarveyHarveyHarvey 2017-01-06
  • 打赏
  • 举报
回复
在你的项目下面双击Properties,在弹出的窗体中将输出类型选为Windows应用程序即可。
zhuhuanlai 2016-12-26
  • 打赏
  • 举报
回复
感谢大神的指点,问题已经解决了,再次谢谢!!!
zhuhuanlai 2016-12-24
  • 打赏
  • 举报
回复
u011981242:您好!

感谢您的热心帮助,我试了试您给的代码,不知道是不是我哪里没有考虑好,出现以下问题,还请您帮忙指点:


主程序代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

Form1的代码如下(在您代码基础上修改了命名空间,其余完全一致,Form1里添加3个textbox控件和1个button控件):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
[DllImport("Dll1")]
extern static int myfortran(ref inx A, ref outx B);

public Form1()
{
InitializeComponent();
}

inx X;
outx Y;
private void button1_Click(object sender, EventArgs e)
{
X.i = Convert.ToInt32(textBox1.Text.Trim());
X.rr = Convert.ToInt32(textBox2.Text.Trim());
Y.a = 1;
Y.rr = 3.0;
textBox3.Text = Y.rr.ToString();
}

private TextBox textBox1;
private TextBox textBox2;
private TextBox textBox3;
private Button button1;
}
public struct inx
{
public int i;
public double rr;
}
public struct outx
{
public int a;
public double rr;
}
}
linrachel 2016-12-21
  • 打赏
  • 举报
回复
将具体业务和输入输出分开,到时候想用什么展现就用什么
zhuhuanlai 2016-12-21
  • 打赏
  • 举报
回复
补充说明:我的思路是新建一个解决方案,在新的解决方案中新建一个windows窗体程序,然后把这段控制台代码转换为windows窗体程序代码(以便设计窗体和控件)
  • 打赏
  • 举报
回复
Form1里添加3个textbox控件和1个button控件 双击button,添加button1_Click事件,所有代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Test
{
    public partial class Form1 : Form
    {
        [DllImport("Dll1")]
        extern static int myfortran(ref inx A, ref outx B);

        public Form1()
        {
            InitializeComponent();
        }

        inx X;
        outx Y;
        private void button1_Click(object sender, EventArgs e)
        {
            X.i = Convert.ToInt32(textBox1.Text.Trim());  
            X.rr = Convert.ToInt32(textBox2.Text.Trim());
            Y.a = 1;
            Y.rr = 3.0;
            textBox3.Text = Y.rr.ToString();
        }
    }
    public struct inx
    {
        public int i;
        public double rr;
    }
    public struct outx
    {
        public int a;
        public double rr;
    }
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧