简体   繁体   English

C#从形式输出到另一个类

[英]c# output from a form to another class

Hi I am creating a application of three types of dark chocolate. 嗨,我正在创建三种黑巧克力的应用程序。 I have a windows form (inherited from another form) and one other class. 我有一个Windows窗体(从另一个窗体继承)和另一个类。

For my windows form class: 对于我的Windows窗体类:

    darkchoco darkco = new darkchoco();
    string[] darkType = new string[3] { "Dark Chocolate Truffle", "Dark Chocolate Bar", "Dark Chocolate Almond" };
    public string returnType;

darkco is the another class. darkco是另一类。 for the box in the windows form, I have a mousedown action defined: 对于Windows窗体中的框,我定义了mousedown操作:

    private void optionBox1_MouseDown(object sender, MouseEventArgs e)
    {
        optionBox1.DoDragDrop(optionBox1.Text,
            DragDropEffects.Copy);
        returnType = darkType[0];
        darkco.darkType = returnType;

for my darkco class, I have: 对于我的darkco班,我有:

         public string darkType;
         public override string ToString()
    {
        return "For dark chocolate, you have selected: " + darkType;
    }

I don't know why the darkType doesn't show up, the only thing shows us is "for dark chocolate, you have selected: " then nothing. 我不知道为什么无法显示darkType,唯一向我们显示的是“对于黑巧克力,您已选择:”然后什么也没有。 My question is where is my code wrong that makes darkType not showing up. 我的问题是我的代码在哪里使darkType无法显示。

Looking forward to get it fixed, thanks! 期待得到修复,谢谢!

Your example is not complete. 您的示例不完整。 How do you transfer the chocolate type to your darktype class? 您如何将巧克力类型转换为深色类型? I'm not sure your darktype variable is part of the darktype class. 我不确定您的darktype变量是否属于darktype类。 I would expect something like this. 我希望这样的事情。 Now you can construct your darco class, using the chocolate type as construction parameter. 现在,您可以使用Chocolate类型作为构造参数来构造darco类。 You also can add logic to the property. 您还可以向该属性添加逻辑。

    public class darco
    {
    public string darktype { set; get; } = String.Empty;

    public darco( string mydarktype)
        {
        darktype = mydarktype;
        }

    public override string ToString()
        {
        return "For dark chocolate, you have selected: " + darkType;
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM