Reputation: 9866
I have user control that includes OpenFileDialog
and PictureBox
. I use it in editable forms where the user is allowed to select and save image along with the other info. The problem that I met is that I need to set openFileDialog.Filter
and this Filter is actually the value of one my TextBox
which is named txtCode
.
Now I pass the txtCode.Text
on the Form_load
event but this is not good enough I need to get the value form the TextBox
when the user tries to open the File Dialog
. Because I use it as an User Control
:
And thus I can't catch the button click event. In fact I can only handle the user control click event, which is fired when I click anywhere outside the File Browse
button and the PictureBox
which ruins my workaround plan to handle the event and check if the sender is a Button
.
How can I pass the txtCode.Text
value when the OpenFileDialog
is opened or at least at a very close moment so I can work with the most current value?
Upvotes: 0
Views: 1391
Reputation: 20745
Start a background thread
just before opening the OpenFIleDialog
. In the background thread, search for available open file dialog box and set the value of filename. you can also click the open button automatically also. There are windows API to do this that can be used in .net.
Sample for 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);
}
Upvotes: 1
Reputation: 3821
You can use txtCode.Text
anywhere in your form. Not only in your Form_Load
handler. Just try using it where you need it.
Upvotes: 0