I have a string value in a variable. I want it to be thrown in keypress event.
When user clicks on "Start Writing Button". The text contained in variable gets written to the area whereever cursor has focus.
eg.
string str = "Example"
I have a web page with a textbox and a button. When user clicks on Start Reading button Example gets written on to textbox.
Basically the characters being written should be trapped on
javascript-onKeyPress event
Winform- KeyPress event
etc.
EDIT
I want to use some devices that will be throwing data constantly to my variable using window service. I need to write this data to the active window whereever the cursor has focus currently irrespective of window or web.
I copied data to clipboard and pasted on active window but this is problematic since different tabs are considered to same active window and doesn't writes.
Looking for a proper way rather then workaround I have taken.
Now that you've clarified the question, I suspect you want SendKeys.SendWait.
You'll need to be somewhat careful with it, but it may do what you want.
Related
In windows forms I have a simple TextBox:
TextBox textBox = new TextBox() { Text = "text" };
textBox.Enabled = false;
textBox.MouseEnter += (object sender, EventArgs e) =>
{
MessageBox.Show("MOUSE ENTERED"); // this never fires if the control is disabled.
};
I want to disable the users ability to interact with the control and I want the control to be styled as a disabled control. But I also want to receive MouseEnter,MouseLeave, and Click events from the control so that I can change the background cover of the control on hover and respond to clicks on the control.
But as I have just discovered if you disable a windows forms control it disabled the events as well. I know with some effort I can accomplish the same thing by checking mouse coordinates globally but it would be a lot nicer if I could just have it disabled but still receive events for it. Is that possible?
Enabled doesn't really do anything in Windows Forms itself. It is a property of windows controls in general that a disabled window doesn't receive input messages (such as mouse events and keyboard events). So no, there is no way for you to disable a control and still receive those messages. Windows just don't work that way on Windows. It's not the TextBox control filtering those messages away - they don't come in the first place.
TextBox is a great wrapper around a windows common control. When you do something like tbx.Text = "Hello";, the TextBox just sends a message to that common control, saying "change the text to Hello". If you want to change that, you need to make the control essentially from scratch. You can make some hack that reverts whatever the common control does as response to a mouse event, but these usually don't work very well and tend to break down in unexpected ways.
In practice, what you really want is probably to tweak either the way ReadOnly behaves (e.g. disabling focus as well as making the control read only, but that's again just a dirty hack), or replace the TextBox with a control that can either be a control or a label - allowing you to switch between the two. If you want the text box to stop behaving as a text box, stop it from being a text box. Problem solved :)
I'd still reconsider using ReadOnly, though. Are you sure the user would not want to select text in the text box and copy it somewhere else? Or change the reading order?
I'm currently using the .Text of the TrayNotifyIcon to display a statusdisplay when the user has the mouse over it (for a percentual completion of a process)
Thus I just set: TrayNotifyIcon.Text to the appropriate % of completion.
Example (the following code is part of a code I use where I create a new thread which sets in the subprocess variable if it is completed or not and also how many % completion are. The code below shall display as the TrayNotifyIcon.Text how many % of the subprocess are completed with updates every second):
while (subprocess.NotCompleted)
{
TrayNotifyIcon.Text = "TextToUpdateTo....." + subprocess.percent.ToString() + "% completion";
Thread.Sleep(1000);
}
Now I've seen that it only updates the display whenever I move the mouse and does not update it as soon as I set .Text appropriately.
Thus my quesiton is is there any way to make it so that I can tell the system to update the text that is being displayed?
The system displays the hint text when the mouse is hovered over the notification icon. This text is not expected to be dynamic. The display of the text is handled by the system. When it wants to display the hint, it queries the icon for the text, and then displays it. It will not go back and check if the text has been changed, and there is no mechanism to inform the system that the text has been changed and should be updated. This is a feature of the shell (Shell_NotifyIcon) rather than anything in the .net libraries.
So, using TrayNotifyIcon in its vanilla form, there is nothing you can do to change this behaviour. If you really want dynamic update of hint text you will have to suppress the system drawn hint (by setting Text to an empty string) and drawing your own hint window. You'd need to detect the mouse hovering over the icon, and also detect when it leaves the icon.
Frankly, I don't think that this is really a very good piece of UX design. If I were you I would find a different way to let the user receive this feedback.
Just out of being bored, I decided to start building my own text editor. I have been having trouble with my coding, so my teacher had suggested building smaller programs that I wanted to write to help get me more familiar with the language, and since I couldn't think of anything, I ended up making this text editor.
I've been trying to root through the code on my own as much as possible, but I was wondering how to make the text that appears at the top of the form (beside the icon) reflect the current filename (or "new" or something if there is no file loaded) as well as having the * if the file has been edited.
I would also like to know how to code my exit button to check if the text has been edited before closing, and ask the user to save if it has, as well as having this show up if the user uses the "X" button in the corner, which currently flat out exits the program no matter what.
To change the title (text besides the icon):
Form1.Text = "This is a new title";
where Form1 is the name of you form object
To check if text is saved:
Hold a boolean variable that indicates whether the user saved the text or not.
Use the Form_Closing method to check if this variable is set to true, and do as you wish
More on Form_Closing here
Many questions :)
Let me answer a few of them:
In your own code, you should probably set a "dirty bit". In other words, declare a boolean variable that says whether or not the text changed. "Changed" is something you, the programmer, needs to define. It can mean many different things - you get to decide.
Each Winform "control" has a set of "properties", most of which you can change programmatically (on-the-fly). Your "form" has a "text" property that changes the title. Label, Button and other controls also have their own "text" property you can change at will.
Each Winform control also has a set of "events" you can override. The "Close" event is the easiest way to manage program shutdown - including if somebody pressed the "X" button. This is also a good place to check your "dirty bit", and save the file accordingly.
Please guide me as how to save/discard values of controls of a form, upon pressing Ok/Cancel button accordingly in Visual Studio C#?
Controls in a form include TablelayoutPanel(TextBoxes), NumericUpDown.
Need your expert guidance
Regards
Asad
With both of your buttons, inside the "onclick" event, call a function that will save the content of the form. You also need this call in the "onclose" event of the form, in case the user presses the top-right X button (or not, if you dont want data to be saved at that moment)
Inside that function, you will need some code that will save data to the registry.
Writing in the registry is easy. This webpage also explain how to get the data back. The values you will write will be the textbox.Value and such
The question isn't clear, but in a WinForm you can call
this.Close()
on the Click event of your Close button.
Every object or variable used by the form will be destroyed. Be careful! running background threads will still be alive until they terminate.
About saving the status of your variables it completely depends on what you need to with them after; you can either keep them in memory and pass them around like parameters or write on a disk (maybe with serialization?).
We need to know more.
edit
You may want to take a look at Application Configuration ( http://msdn.microsoft.com/en-us/library/ms184658(VS.80).aspx ).
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