I have a program with a timer which sends the message "Test" each second via sendkeys, and I have it setup to where it will sendkeys 5 times, then disable the timer!
But I'm wondering if there is a way to only make it send the message if a valid input for it is selected, because it keeps trying to sendkeys when I don't have an input selected(EG: Notepad), and that is not really the desired result!
Not sure I fully understand your situation, but you can check to see if the active control is what you expect it to be by calling
If Screen.ActiveControl.Name <> "MyControl> Then ...
End If
before the SendKeys is executed. You can also set the focus to a control you need it to be focused to with:
Me!YourControlName.SetFocus
Related
I'm very new to C# and I'm trying to make a simple GUI hangman game to help me learn. I am using a textbox to both output errors (The letter was already entered. Try again) etc. and input the user's guess. However my problem comes in that whenever the user is given an error, they have to manually clear the textbox. What I'm looking for is a feature in most search boxes, google for example, that clears (in google's case, highlights) the text currently in the box.
I know it can be simply done using
textboxname.Clear();
but I'm not sure where it should go, I can place it under the code for a button without a problem but my instinct is to put it outside of the button's {} , however when I try this the text box isn't recognized and if statements can't be used.
I think I'm looking for:
if (TextBoxName.Focus)
{
TextBoxName.Clear();
}
But I'm just not sure where to put it
I want to get the input value of an <input type="text"> element, on the fly (while it is being typed) and implement a search method with it as parameter.
I have this piece of code:
_window.Frame(WatiN.Core.Find.ById("a_frame"))
.TextField(WatiN.Core.Find.ById("an_element"))
.FireEventNoWait("event_string", other params);
What event would you think is the best suited for this? I have some thoughts on KeyPressed or KeyUp, but I'd like some other opinions for this matter? I have searched for TextChanged and some similar Event, but I haven't found anything.
Are we to assume the above code isn't working, or? If nothing else works you could always do a do loop right after the text box gets focus, do loop would contain these:
Sleep 100
Doevents
And of course after every 100 ms break and a Doevents, you can check to see if .value has changed, and if so, query your search. When the text box looses focus, you stop the loop.
You are using the webbrowser control, right? And you want to d this using the webbrowser control as opposed to JavaScript? Because, you can get keyup keydown events through the webbrowser controls eventing system, and that would be a better way to do it, but I'm not clear on the who what when where why of what your doing :)
I have a C# 2008 Winform application and I'm in the middle of a loop. I'm displaying a date to the user and I want them to tell me the day of the week that this date falls on. For example 6/22/2010 is displayed and the user needs to press t.
What I'm stuck on is how do I pause my application and wait for keyboard input? I want to respond to Esc, m, t, w, h, f, s, u and nothing else. All other key presses will be ignored.
In a console application it would be Console.ReadLine(). But how would I do this within a Winform application?
Thanks
You could perhaps use a modal dialog...
Trying to think of a better solution in terms of presentation...
Spawn a modal dialog box requesting the input.
To read a key you need to respond to the KeyDown event.
Then in the handler have something like:
if (e.KeyCode == Keys.M)
{
}
Though you'd probably want a switch statement rather than a series of ifs.
You'd have to think about how you presented the date to the user as well, if it's on the main form or in a model dialog (as others have suggested).
You've got the concept wrong, a Windows program is always in a loop, waiting.
So you'll have to think about what input to accept and what to block. You can Cancel a FormClose event for example (but please, leave the user something to get out).
To implement your scheme, use the concept of 'state', after the right input you advance to the next state.
When you need some information during a calcultion (the loop in your case), you can use a callback method to get this information.
int Calculate( Func<DateTime, string> callback )
{
var result = callback( dateTime );
}
The caller of this method has to provide a callback that returns the requested value. When the calculation is started in a form, this form could pop up a dialog to ask the user for input. And this could happen in the callback.
EDIT:
Do you know of the DateTime.DayOfWeek property? Maybe you can skip the user dialog at all.
I have a created a custom keyboard shortcut for my application,
when the user press combination keys of CTRL + ALT + Q, i show
a messagebox "Are you sure you want to log out ?" Then if clicked
YES, i log out of the application.
Problem :
I want to make sure that, only once instance of message box shows.
No matter how many times the user presses the shortcut.
currently it shows multiple message box, on pressing multiple
shortcuts.
How to overcome this ?
From MSDN
A message box is a modal dialog box,
which means no input (keyboard or
mouse click) can occur except to
objects on the modal form. The program
must hide or close a modal form
(typically in response to some user
action) before input to another form
can occur.
File a bug on connect.microsoft.com !
Taking ck's comment into consideration...If you are showing a custom dialog (form) then you need to invoke the form using Form.ShowDialog() and not Show().
A quick and dirty way would be to have a class level boolean variable that tracks when the user is trying to exit. If they are, it's set to true, and your routine to display the dialog box can check this flag, then return without doing anything.
Seems like Singleton Pattern is your option.
I think you can create your own form and use the mymessageboxform.show() method, and check its dialogue result.
You'll want to make your application single instance so it can only be started once.
Single Instance App
I'm currently working in an application that has to navigate a webpage and recollect data about products, prices, ... with webbrowser object in .net 3.5.
The problem is that the page has some fields with a suggest script in ajax, and I can't simply modify the innerText property because the script also saves codes in a hidden input field.
I need a way to simulate the typing in that field and then send the "Enter" key, or launch the ajax script and then send the "Enter" key, or any better ways to do this.
Use Watin
Then you can use this solution.
To submit a form or run a script you can do this:
If you know the script name you can use InvoekScript of Document object:
myWebBrowser.Document.InvokeScript("script-name",null);
the second argument is an array of objects to pass parameters values.
if you know the name of an element that it's click event fires the script you can do this:
HtmlElement element=myWebBrower.Document.GetElementById("element-name")[0];
element.InvokeMember("click");
There are a few ways to do this with the standard WebBrowser control.
For HTML: If you want to fillout a textbox and then click submit then don't even bother with a keypress. Do this:
webbrowser1.Navigate("javascript:function%20E(){f0=document.forms[0];f0['login'].value='foo';}E()")
webbrowser1.Navigate("javascript:document.forms[0].submit()")
This will be much more reliable then trying to send keypresses for HTML.
**For Flash:**If there's a flash element on the webpage that needs clicking then it won't work. In that case SendKeys is reliable but only sends to the active application so it won't work in the background. You can send windows messages like this (example will press the letter "f"):
Dim c as char = "f"c
Dim classname As New System.Text.StringBuilder(100)
Dim ExplorerHandle As IntPtr = webbrowser1.Handle
GetClassNameA(ExplorerHandle, classname, classname.Capacity)
Do While classname.ToString <> "Internet Explorer_Server"
ExplorerHandle = GetWindow(GetExplorerHandle, GetWindow_Cmd.GW_CHILD)
GetClassNameA(ExplorerHandle, classname, classname.Capacity)
Loop
PostMessageA(ExplorerHandle, WM_Constants.WM_KEYDOWN, IntPtr.Zero, IntPtr.Zero)
PostMessageA(ExplorerHandle, WM_Constants.WM_KEYUP, CType(VkKeyScanA(CType(Asc(c), Byte)), IntPtr), IntPtr.Zero)
You can find the definitions for PostMessage, GetClassName, GetWindow, etc, online at pinvoke.net. Notice that the WM_KEYUP uses c but the WM_KEYDOWN sends a dummy key (0). KEYDOWN and KEYUP have to go in pairs or else the key won't be registered, but if you hold down Control while sending, for example, KEYDOWN "p", it will activate IE's print function. For all the letters and digits you can send 0 for KEYDOWN and the correct letter for KEYUP. Backspace seems to need a real KEYDOWN, not 0, but Control-Backspace doesn't seem to do much in IE so if c = vbBack, KEYDOWN needs to be different.
The keypresses aren't very accurate, either, and 1 time in about 500 it misses. But you can do it with the window minimized, no problem, and a standard WebBrowser control.
Another option is to use AxWebBrowser. The ActiveX control seems to avoid the Control-P problem but it's not as easy to manipluate because it isn't a nice .Net control.