【问题标题】:Load Form in Class Library with pre-defined values C#使用预定义值 C# 在类库中加载表单
【发布时间】:2017-04-21 22:23:38
【问题描述】:

我将尽我所能解释这一点。我有一个设计成类库的表单,我让它在代码中间可见。当我这样做时,我想用预先确定的值填充文本框。假设我希望变量 color1 和 color2 填充文本框。如何在表单加载时调用这些变量?当我键入它们时,没有一个文本框出现在 Visual Studio 中......

字符串 color1 = '蓝色'; 字符串颜色2 = '红色';

textbox1.text = color1 textbox2.text = color2

InspectionForm myForm = new InspectionForm();
                        myForm.Visible = true;

...

private void InspectionForm_Load(object sender, System.EventArgs e)
        {
        }

【问题讨论】:

  • AFAIK 表单成员不公开可见。
  • 你能改变类库还是外部资源?
  • 这是我的类库。有没有更好的方法来解决这个问题?我对编码还是很陌生。

标签: c# class-library


【解决方案1】:

你可以在你的表单上创建一个构造函数

public InspectionForm(string color1, string color2)
{
    InitializeComponent(); //This is may or may not be needed
    textBox1.Text = color1;
    textBox2.Text = color2;
}

或者创建一个公共方法来设置你的值。

public void SetColors(string color1, string color2)
{
    textBox1.Text = color1;
    textBox2.Text = color2;
}

然后像这样使用它们:

var form = new InspectionForm("blue", "red");
//or
var form = new InspectionForm();
form.SetColors("blue", "red");

但是,如果您无法更改类库,那么除了一些反射黑客之外,您将没有任何选择。

【讨论】:

  • 很抱歉没有回复,但这个答案有帮助!
猜你喜欢
  • 1970-01-01
  • 2019-02-18
  • 2010-12-16
  • 1970-01-01
  • 2011-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-11
相关资源
最近更新 更多