hi
i need to call enter key automatically from my program
i want , when a savedialogbox is opened in windows(all savedialogbox!), then it save automatically without need to click or press by user.
how can i do it?
thanks
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Zoran HorvatRank #792 · 2.9K reputationPosted Aug 3, 2011, 1:19 AM EDT
string windowHandle = "Save As";
IntPtr hwnd = (IntPtr)FindWindow(null, windowHandle);
if (hwnd != IntPtr.Zero)
{
SetForegroundWindow(hwnd);
SendKeys.Send("{ENTER}");
}
Without this check you're sending ENTER key to currently active window, which is wrong.
Zoran
Bahar Jalali165 reputationPosted Aug 2, 2011, 11:31 PM EDT
i find it and it works
it's the code
string windowHandle = "Save As";
IntPtr hwnd = (IntPtr)FindWindow(null, windowHandle);
SetForegroundWindow(hwnd);
SendKeys.Send("{ENTER}");
i put this code in form_load()
like this:
///////////////////////////////////////////////////////////
// Get a handle to an application window.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string windowHandle = "Save As";
IntPtr hwnd = (IntPtr)FindWindow(null, windowHandle);
SetForegroundWindow(hwnd);
SendKeys.Send("{ENTER}");
}
}
}
//////////////////////////////////////////////////////////
now it work but it enter the first save dialog box on windows
and when i open a savedialog box again, it dosn't work
i try use if the form is already open but it dosn't work again.
please help me .
i want while my program is running , it enters all save dialog boxes.
thanks
Zoran HorvatRank #792 · 2.9K reputationPosted Aug 3, 2011, 12:03 AM EDT
If you want the code to be executed every time the form is shown, then implement it in the Shown event handler.
Zoran
Bahar Jalali165 reputationPosted Aug 3, 2011, 12:22 AM EDT
i put the code in form1_shown and form1_changevisible but dosn't work again
i put a timer and it fall in a EXTREME loop
could you test it ?
thanks
Zoran HorvatRank #792 · 2.9K reputationPosted Aug 3, 2011, 12:28 AM EDT
Zoran
Bahar Jalali165 reputationPosted Aug 3, 2011, 12:48 AM EDT
because the command
and it execute this command on all programs.
i thinks when i put the code in a timer thick event, it can't detect "Save Az" window ! but i want when it detect a save as window then enters it.
///////////////////////////////////////
private void timer1_Tick(object sender, EventArgs e)
{
string windowHandle = "Save As";
IntPtr hwnd = (IntPtr)FindWindow(null, windowHandle);
label1.Text = hwnd.ToString();
SetForegroundWindow(hwnd);
SendKeys.Send("{ENTER}");
}
//////////////////////////////////////
and it seems i need to a if command for check it
but i don't know how can i check it?!
and what about "static" in DLLs on the top of my code? is it possible my problem is maked for it?
any idea?!
Bahar Jalali165 reputationPosted Aug 3, 2011, 2:24 AM EDT
i found that myself and wort like this:
///////////
private void timer1_Tick(object sender, EventArgs e)
{
string windowHandle = "Save As";
// Process[] word = Process.GetProcessesByName(programname);
IntPtr hwnd = (IntPtr)FindWindow("#32770", windowHandle);
IntPtr val = IntPtr.Zero;
if (hwnd != val)
{
SetForegroundWindow(hwnd);
SendKeys.Send("{ENTER}");
timer1.Stop();
timer1.Start();
}
}
///////////////////////////////////////
ant it's like your code
Sam HobbsRank #107 · 20.5K reputationPosted Aug 3, 2011, 11:14 AM EDT
It is not clear to me if this is for the same process or a different process. If it is the same process and you have the code for program, then I don't understand why you are showing a save file dialog when you don't need it.