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
Related
I have a C# project (Win Forms), where a form reads an external text file, and then puts the text in a textbox on the form. What I would like to do is create a hyperlink from the text that the program reads in.
For example, if the text file reads "To go to Google, click HERE [www.google.com]", then I want the program to make "HERE" clickable, and go to www.google.com if HERE is clicked on.
Right now the program can read in the text file and recognize the web address just fine. I just don't know how to make "HERE" clickable.
Note: Due to external factors, I cannot make a button, or LinkLabel, or other object for the user to click on. The word itself has to be the hyperlink (if that is at all possible). Also, I have to read the string from an external file. I can't simply put textBox1.Text = "To go to Google, click HERE [www.google.com]";
Thanks in advance!
The easiest work-around for what you are looking for would be to add a handler for DoubleClick, and then just compare the selected text.
If it must be a single click, you want to use OnClick, and then get the test up to the last space to the left and to the right, and do the same compare.
VERY HACKISH btw.
private void textBox1_DoubleClick(object sender, EventArgs e)
{
if (string.Compare(textBox1.SelectedText.Trim(), "HERE") == 0)
System.Diagnostics.Process.Start("http://www.google.com");
}
Building on the previous response, you might want to add an eventhandler to a label instead. You should be able to format the label to be blue and underlined to look like a link, also.
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
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.
Suppose i have a textfile which continuous to be updated with text, how can i display the contents in a textbox in windows form (in real-time)?
for example. this is the contents of log.txt:
connected, bla bla bla
disconnected, bla bla bla
PS: i want it to be displayed in textBox of Form1 (real time also) so everytime the text file has new text, the textbox displays it. Any ideas, pls help. thanks. also can u provide a sample working code. thanks
Use a FileSystemWatcher
Use the example on MSDN (see the link above) you can add a event handler to Changed Event
and update your textbox from there.
Just call Application.DoEvents(); once you've updated your text. But note that updating too often might cause flicker as well as slow down your overall processing. Also note that this is error prone if any drawing/update code causes updates/draws again (infinite recursion).
Edit: Read half the question ...
Add a FileSystemWatcher to watch for changes to the log file. However this is a very ineffective approach and depending on the settings used to write the log file you might be missing access rights and/or the file might update only when the application closes. If both processes are your own code and you're able to modify them, you should think about other possibilities (e.g. a simple "server" you can connect to using some telnet (or custom) client).
Insert a RichTextBox and a FilesystemWatcher, and a textbox or something else to store the path and filename of the text file you want to show. Now, go to the FilesystemWatcher_Changed (or whatever it's called) event. There you put an if that checks if the changed file is the one you want to be shown, and then RichTextBox.LoadFile(file).
What this is going to do is monitor the fileystsem (hard disk or any folder you specify), and every time a file has cha nged, it's going to check if it's the file that we're looking at. If it is, it's going to reload the file into the RichTextBox.
Don't forget to set the RaiseEvent and Path properties of the FilesystemWatcher.
Example:
string blaFile = "C:\TextFile.txt"; // The text file that we want to read.
private void FilesystemWatcher_Changed(....).... // The FilesystemWatcher_Changed event notifies us when a file on the monitored path (FilesystemWatcher.Path) has changed. When a file has changed the code within this event gets executed.
{
if (e.File == blaFile) // We check if the file that has chenged, is the one we want.
{
RichTextBox.LoadFile(blaFile); // It is! We load it into the RichtextBox again so that it's "up-to-date"!
}
}
I'm not completely sure about the e.File as I haven't used C# in a while now, but it's something like that.
It is not suggested to use text file, since there are two processes to access the file and you require real-time update.
Try to use NamePipeServerStream and NamePipeClientStream for two processes memory sharing.
Simplifying
I have a text box and a button
The button just create an messagebox with the text from the textbox.
But i change the value of the textbox, the new value apears (Ex: Type 123) but the message box does not show the value.
If i try to use the value in the programming (get the value by textbox1.text) the variable has nothing ( textbox1.text = "") but i can still see what i typed in the form.
Anyone has any clue?
Your button's click event handler should look something like this
private void button_Click(object sender, EventArgs e)
{
MessageBox.Show(textBox.Text);
}
I suspect you already have code similar to this and that at some point the textbox is cleared or otherwise set to String.Emppty but without seeing actual code it is difficult to help you
When/where did you check the value of textBox1.Text? If you're checking it in the constructor, Form1_Load, or anything else that occurs before you'll have typed text, you will get an empty value.
To properly check the value of textBox1.Text, you should set what's called a breakpoint on the line that calls MessageBox.Show(textBox1.Text). To do this, click in the grey area of the source editor (it's on the far left) on the line containing MessageBox.Show(..). A red circle will appear and your code should be highlighted. When you run your application and click on your button, your application should pause and Visual Studio will highlight that line and from here you can hover over "textBox1.Text" in the MessageBox.Show() line and it should show you the current value.
If your application is as simple as a form, a textbox, and your button1_Clicked event handling code, this should work no problem. If it is not this simple, then you need to look for anything that sets the value of the textBox in your code and make sure it isn't passing any blank values by using breakpoints.
To solve this properly, though, we really need more information.
Thanks Eric and Crippledsmurf. As both of you said, its hard to help without the code.
The problem I found is that when calling the form, I send some objects by reference, so I can track them down and I found that when (don't ask me why it happens that way, I'm still working on it) the construtor is called he make an new component, so the component in the interface no longer represents the one pointed by the variable "textbox1" (Yes Crash893, I haven't mispelled the name).
I found that I was making some mess with the references, and probably that was causing the problem. I fixed the problem by changing the actions performed by references for delegates and events, but I couldn't track down the exactly source of the problem.
Thanks, again, everyone for the insights.