8
Answers

How can i call enter key from my program when a savedialogbox opened in windows?

Bahar Jalali

Bahar Jalali

13y
2.2k
1
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
Answers (8)
0
Zoran Horvat
NA 5.7k 516.3k 13y
You must check return value of FindForm:

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
Accepted
0
Sam Hobbs
NA 28.7k 1.3m 13y
I would not do it that way. Experienced Windows programmers would know how to send a message to the dialog that notifies it that the okay button was clicked.

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.
0
Bahar Jalali
NA 153 259.8k 13y
yes that's right!

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
0
Bahar Jalali
NA 153 259.8k 13y
i use timer but it enters every program on windows that mouse go move on!

because the command <sendkey.send("{ENTER}")> is in the timer tick event!

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?!
0
Zoran Horvat
NA 5.7k 516.3k 13y
I've written you the code with timer in another thread you posted. Try with that, it should be sufficient for your needs.

Zoran
0
Bahar Jalali
NA 153 259.8k 13y
thanks
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
0
Zoran Horvat
NA 5.7k 516.3k 13y
You are executing this code from Form_Load method, i.e. from form's Load event handler. It means that code will be executed only once, and when form is shown for the first time. So you need to restart the application in order to execute this code again.

If you want the code to be executed every time the form is shown, then implement it in the Shown event handler.

Zoran
0
Bahar Jalali
NA 153 259.8k 13y
every body!
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