C# WinForm text change/ exit prompt - c#

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.

Related

C# Windows app, clear textbox on focus

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

Updating The .Text of the TrayNotifyIcon

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.

Put the selected text in a variable of my application

In my application, I have a textbox. I want to be able to copy text from a webpage or document open elsewhere in Windows by simply highlighting the text and pressing a global hotkey - then have that text appear in the textbox.
What I am stuck on is the selection part: I'm looking for a way to use the selected text as a variable within my WPF application, but I can't seem to get access to it in a way that makes sense.
Right now, I'm working with the clipboard and Clipboard.GetText() to get my variable :
private void OnHotKeyHandler(HotKey hotKey) {
if (Clipboard.ContainsData(DataFormats.Text))
tb_number.Text = Clipboard.GetText();
}
But i'm trying to avoid a tedious CTRL-C each time i want to get this value and work with selected text, rather than copied text. Any ideas how I can do this on-select rather than by accessing the clipboard?
I beleive the question is much clearer now. Thanks for the editing.
I dont think you can modify the clipboard functionality thru a .NET application in an easy way. But you may get what you need with a third party application for windows called autohotkey.
http://www.autohotkey.com/docs/Tutorial.htm#Send
https://superuser.com/questions/166270/change-ctrl-x-c-and-v-hotkeys-in-windows-to-different-keys

Converting string obtained from database into keypress event

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.

How to save/discard values of controls of a form, upon pressing Ok/Cancel button?

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 ).

Categories

Resources