I am trying to click a button in a Windows Form Application and use the SendKeys function in C# to write button1.Text to a separate window. Kind of like a "paste" function. However, when I use SendKeys.SendWait, it writes the text incorrectly. For instance, the button1.Text = "Hello World!"
It will paste "HHHelloo Worlddd!!"
Is there a way I can get the exact string to send over?
Here's a sample of what's going on:
private void button1_Click(object sender, EventArgs e)
{
this.Visible = false;
SendKeys.SendWait(button1.Text);
this.Visible = true;
}
I have found the solution to this problem. Instead of sending the text one key at a time and using the extra xml in the config file, you can simply call SendKeys.SendWait("^(v)"); to simulate Paste.
Related
I'm writing a program in c# (visual studio, windows forms) which involves the user selecting a name from a combo box, and then clicking a button which brings them to a quiz on another form. I want a text file to be created at the end of the quiz showing the name selected and the quiz results, followed by a "~" symbol.
I honestly don't know where to start. I'm using stream reader.
This is the code I used for the combo box and the button that sends you to the next form
private void quizSelectPupilCB_SelectedIndexChanged(object sender, EventArgs e)
{
selectedClass = quizSelectPupilCB.SelectedItem.ToString();
}
private void quizStartBTN_Click(object sender, EventArgs e)
{
Quiz2 formQuiz2 = new Quiz2();
formQuiz2.Show();
this.Hide();
}
How do I use this data on another form?
I don't even know where to start. I tried looking up things like "how to access data from one form and put it in another c#" and similar things but everything I found was either not what I was looking for, or worded in a really confusing way.
As a variant you can save selectedItem to String/StringCollection, then to app settings(Settings.Default). And on another form get this value back.
The form I am using requires a copy pasted URL. I am trying to have a textChanged event that will check the url as soon as it is pasted, telling the user whether it is valid or invalid. I also want to be able to lock out the textbox when this happens, with a message saying something like "Processing...".
The problem is with the code below, the textbox is never disabled, the program will do the checkUrl() method and the textbox is never disabled even though it is first to execute (I assume it is but the fact there is a function call right underneath it is messing around with something or getting higher priority).
How do I go about making the control visually disabled while the method runs?
private void urlTxtBx_TextChanged(object sender, EventArgs e)
{
urlTxtBx.Enabled = false;
checkUrl();
urlTxtBx.Enabled = true;
}
I think this is happening because the Application needs to complete all the active threads before disabling the TextBox. Please try the following code:
private void urlTxtBx_TextChanged(object sender, EventArgs e)
{
urlTxtBx.Enabled = false;
Application.DoEvents();
checkUrl();
urlTxtBx.Enabled = true;
}
This will let the UI to be updated. For more details check here.
I'm creating a native C# application and I need to do a simple thing:
Once the user clicks some certain button, another .cs file is opened (with its own design, code and stuff). If it is possible, I would like to know how to close the current form at the same time.
EDIT: what I exactly need:
namespace Mokesciai
{
public partial class Mokesciai : Form
{
public Mokesciai()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//write code here to open another page called "NewPage.cs" with its own subfiles "NewPage.Designer.cs" and
//"NewPage.resx", as shown in the solution explorer
}
}
}
The application is C# Windows application
EDIT2: what I want in the graphical way: http://sdrv.ms/JXKVEL
By clicking "Click me" I want to open the new form
private void button1_Click(object sender, EventArgs e)
{
YourSecondForm objForm=new YourSecondForm();
objForm.Show();
this.Close();
}
Assuming YourSecondForm is the name of your another form which you want to display on the button click event.
That is a form.
You can create a new instance of the form class, then call Show().
As far as i understand from your question, may be you want to do this. You can use Process.Start() to start any other application from your native app.
using System.Diagnostics;
string path=#"path to the app"
Process.Start(path);
OR
Create a new form place a multi line text box, then read the file using StreamReader & fill its result on the text box. For more information on how to use Stream Reader Check out this or this
I have a RichTextBox as output and TextBox as input on the main WinForms form. I would like to be able to keep focus on the TextBox while highlighting text in the output with the mouse. That would allow me, as an example, to type something in the input and simultaneously select something in the output with the mouse.
I saw this done in one application which isn't necessarily WinForms based, but it does run on a Windows machine.
How can I do this with WinForms?
You can try something along the lines of
bool selecting;
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
selecting = true;
}
private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
{
if (selecting)
textBox1.Focus();
selecting = false;
}
This resets the focus on the TextBox as soon as you finish selecting from the RichTextBox. The problem however is that, as soon as the focus is restored, the selection is cleared.
I can't seem to find a way to catch the input of a magnetic card reader. When it swipes, the input gets into active text editor, like say a notepad.
Unfortunately, the focus on textbox field won't do the trick, because I'm required to make it a label instead of a textbox. Thus, I need a way to catch the input from the USB device to a variable or label instead.
Does anyone knows of a .NET class I could use to do this or any better ideas?
If it's a winforms app you could do
private void Form1_Load(object sender, EventArgs e)
{
KeyPreview = true;
KeyPress += Form1_KeyPress;
}
private bool inputToLabel = true;
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (inputToLabel)
{
label1.Text = label1.Text + e.KeyChar;
e.Handled = true;
}
else
{
e.Handled = false;
}
}
and as long as the window has focus, the keypress characters will go to the label's text.
I don't think there will be anyway for you to prevent the user from manual input. I suspect the card reader that you have emulates a keyboard. So, to be able to read from the reader, you must receive keyboard input, and keyboard input means the user can type anything they like.
A possible solution is to change your card reader to one that uses an API to read from cards.
If getting a better card reader isn't an option, I think the best method to do this is to have a button. When the button is clicked, open a new form that contains the code #Bala R provided. But in addition, close the form within 1 second from the first key input. This will prevent users from tampering the input manually, but will provide sufficient time for the reader to complete.