C# Windows窗体-将值从窗体传递到用户控件

C# Windows窗体-将值从窗体传递到用户控件,c#,winforms,user-controls,C#,Winforms,User Controls,我的用户控件包括OpenFileDialog和PictureBox。我在可编辑表单中使用它,允许用户选择和保存图像以及其他信息。我遇到的问题是我需要设置openFileDialog.Filter,而这个过滤器实际上是我的TextBox中一个名为txtCode的值 现在我在表单加载事件上传递txtCode.Text,但这还不够好,当用户试图打开文件对话框时,我需要从文本框中获取值。因为我使用它作为用户控件: 因此,我无法捕捉按钮单击事件。事实上,我只能处理用户控件单击事件,当我单击文件浏览按钮和

我的用户控件包括
OpenFileDialog
PictureBox
。我在可编辑表单中使用它,允许用户选择和保存图像以及其他信息。我遇到的问题是我需要设置
openFileDialog.Filter
,而这个过滤器实际上是我的
TextBox
中一个名为
txtCode
的值

现在我在
表单加载
事件上传递
txtCode.Text
,但这还不够好,当用户试图打开
文件对话框时,我需要从
文本框
中获取值。因为我使用它作为
用户控件

因此,我无法捕捉按钮单击事件。事实上,我只能处理用户控件单击事件,当我单击
文件浏览
按钮和
图片框
之外的任何位置时会触发该事件,这会破坏我处理事件并检查发送者是否为
按钮
的解决方案


当打开
OpenFileDialog
或至少在非常接近的时刻,我如何传递
txtCode.Text
值,以便使用最新的值?

您可以在表单中的任何位置使用
txtCode.Text
。不仅在您的
表单中加载
处理程序。只要试着在需要的地方使用它。

在打开
OpenFIleDialog
之前启动
后台线程。在后台线程中,搜索可用的打开文件对话框并设置文件名的值。您还可以自动单击“打开”按钮。有一些windows API可以在.net中使用

FindWindow的示例:

**Calling Code:**

Thread thread2 = new Thread(new ThreadStart(MyClass.SelectFile));
thread2.IsBackground = true;
thread = thread2;
thread.Start();


**Class Code:**

[DllImport("user32.dll", SetLastError=true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", EntryPoint="FindWindow", SetLastError=true)]
private static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);

public void SelectFile(string filename)
{
    Thread.Sleep(0x3e8);
    IntPtr zero = IntPtr.Zero;
    IntPtr parentHandle = IntPtr.Zero;
    IntPtr child = new WinAPI(Process.GetCurrentProcess().MainWindowHandle, "#32770").GetChild();
    while (child == IntPtr.Zero)
    {
        Application.DoEvents();
    }
    if (child != IntPtr.Zero)
    {
        zero = child;
        parentHandle = FindWindowEx(zero, IntPtr.Zero, "ComboBoxEx32", "");
        if (parentHandle != IntPtr.Zero)
        {
            parentHandle = FindWindowEx(parentHandle, IntPtr.Zero, "ComboBox", "");
            if (parentHandle != IntPtr.Zero)
            {
                parentHandle = FindWindowEx(parentHandle, IntPtr.Zero, "Edit", "");
                if (parentHandle != IntPtr.Zero)
                {
                    SendMessage(parentHandle, 12, IntPtr.Zero, fileName);
                    parentHandle = FindWindowEx(zero, IntPtr.Zero, "Button", "&Open");
                    if (!(parentHandle == IntPtr.Zero))
                    {
                        SendMessage(parentHandle, 0xf5, IntPtr.Zero, "");
                    }
                }
            }
        }
    }
    Thread.Sleep(0x7d0);
}

你能在上下文中显示代码吗?如果有人能从下面的描述中找出你的问题所在,我会感到惊讶:)@Nik如果你不能找出它,并不意味着没有人能:)