我拥有包含OpenFileDialog
和PictureBox
的用户控件。我在可编辑的表单中使用它,允许用户选择并保存图像以及其他信息。我遇到的问题是我需要设置openFileDialog.Filter
,而此过滤器实际上是我的TextBox
的一个名为txtCode
的值。
现在我在txtCode.Text
事件上传递Form_load
,但这还不够好当用户尝试打开{{1}时,我需要从TextBox
获取值}。因为我将其用作File Dialog
:
因此我无法抓住按钮点击事件。事实上,我只能处理用户控制点击事件,当我点击User Control
按钮外的任何地方时会触发,而File Browse
会破坏我的解决方案以处理事件并检查发件人是否是PictureBox
。
如何在Button
打开时或至少在非常接近的时刻传递txtCode.Text
值,以便我可以使用最新值?
答案 0 :(得分:1)
在打开background thread
之前启动OpenFIleDialog
。在后台线程中,搜索可用的打开文件对话框并设置filename的值。您也可以自动点击打开按钮。有一些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);
}
答案 1 :(得分:0)
您可以在表单中的任何位置使用txtCode.Text
。不仅在您的Form_Load
处理程序中。只需尝试在您需要的地方使用它。