我正在尝试创建一个新的用户控件作为通用查找表单打开器。
lkpControl.FormToOpen="FrmProductList";
lkpControl.ReturnValueVariableName="ProductCode";
lkpControl.ShowThatForm();
if dialog result is OK:
lkpControl.txtValue=lkpControl.GetSelectedValue();
此控件将从其名称打开预配置(或在设计时指定)表单,并将打开表单中的选定项目返回到其文本框。就像从文件夹浏览器对话框中获取路径一样。
只是一个通用的表单打开器和打开表单中选定项目的值获取器。
如果您能指导我找到任何解决方案或遵循的路径,我将不胜感激。
提前致谢
更新:我已经解决了这个问题:
public partial class LookupButton : UserControl
{
    [Description("Type of the Form To Open: typeof(LookupButtonTest.Form2)")]
    [Category("Custom")]
    public Type FormToOpen { get; set; }
    [Description("Name Of the public property to get return value from opened form.")]
    [Category("Custom")]
    public string PropertyToGet { get; set; }
    public LookupButton()
    {
        InitializeComponent();
    }
    private void btnOpenForm_Click(object sender, EventArgs e)
    {
        if (FormToOpen == null)
        {
            throw new ArgumentNullException("FormToOpen");
        }
        if (PropertyToGet.Length <= 0)
        {
            throw new ArgumentNullException("PropertyToGet");
        }
        Form objForm = (Form)Activator.CreateInstance(FormToOpen);
        if (objForm.ShowDialog() == DialogResult.OK)
        {
            bool propertyFound = false;
            PropertyInfo[] props = objForm.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo p in props)
            {
                if (p.Name == PropertyToGet)
                {
                    txtReturnVal.Text = p.GetValue(objForm, null).ToString();
                    propertyFound = true;
                }
            }
            if (!propertyFound)
            {
                throw new ArgumentNullException("PropertyToGet");
            }
        }   
    }
}
