SendKeys.Send("hello"); continuously sending string how can i send only once - c#

This code is continuously sending a string. I need a solution which can send the supplied string only once by clicking a button in the window which has the focus.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SendKeys.Send("This is a test...");
}
}

The issue seems to be the fact that the button1 control has the focus, and when you send certain keys to it (like the space in your example), it triggers the Click event, and you end up in an infinite loop.
Try adding a TextBox to your form and then set focus to that first (or if you already know the thing that should receive the text, ensure that it has the focus before you call SendKeys):
private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
SendKeys.Send("This is a test...");
}

Related

Invoke or BeginInvoke cannot be called on a control... even using CreateControl()

Im not able to modify from my Form1 an element that belongs to Form 2.
public partial class Project : Form
{
public Form2 form = new Form2();
public Project()
{
InitializeComponent();
}
private void Project_Load(object sender, EventArgs e)
{
form.CreateControl();
}
private void buttonOpenForm2_Click(object sender, EventArgs e)
{
form.Show();
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
var indata = *whatever serial input data here*
bool result = Int32.TryParse(indata, out int data);
if (result) {
form.chart1.Invoke(new Action(() => { form.chart1.Series[0].Points.AddY(data); }));
}
}
Any time I press my button in order to show Form2 and its chart, an exception is raised in form.chart1.Invoke: Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
Why is this happening if i'm forcing form to do a CreateControl() ?
The error message is telling you what is wrong.
Your serial port is firing before the form2 (that holds your chart) is fully created
I guess you could check the visible flag (there are probably many others)
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
...
if(!form?.Visible)
return;
form.chart1.Invoke(...
The assumption is if its not visible, you don't want to display the data
Further reading
Order of Events in Windows Forms
Application Startup and Shutdown Events
The Form and Control classes expose a set of events related to
application startup and shutdown. When a Windows Forms application
starts, the startup events of the main form are raised in the
following order:
Control.HandleCreated
Control.BindingContextChanged
Form.Load
Control.VisibleChanged
Form.Activated
Form.Shown

Keeping track of how many instantiation of a Form is opened or closed

I have an application with three forms. MainForm, QuestionForm, and ViewItemsForm. The MainForm is always displayed. The MainForm contains two buttons and two read-only textboxes. One of the buttons allows the user to open up multiple instance of the QuestionForm which should then display the count of how many are open in one of the textboxes in the MainForm. If one of the QuestionForms is closed, the count inside the textbox should go down to.
I've tried to implement the trigger inside the button that opens the QuestionFrom, and another when the form is closed, but it doesn't seem to work.
public partial class Form1 : Form
{
private void questionFormButton_Click(object sender, EventArgs e)
{
QuestionForm questionFormOpen = new QuestionForm();
questionFormOpen.Show();
}
private void countOfQuestionForm_TextChanged(object sender, EventArgs e)
{
countOfQuestionForm.Text = //Assign the count here
}
}
You can use the Application.OpenForms Property like the following code:
private void questionFormButton_Click(object sender, EventArgs e)
{
QuestionForm questionFormOpen = new QuestionForm();
questionFormOpen.Show();
questionFormOpen.Shown += Fr_Closed;
questionFormOpen.Disposed += Fr_Closed;
}
private void Fr_Closed(object sender, EventArgs e)
{
countOfQuestionForm.Text = Application.OpenForms.OfType<QuestionForm>().Count().ToString();
}

Returning to the last form the user was on

I have multiple forms and i can make them all go to a certain form through the click of one button, but how do I give one button the ability to go back to the last page the user was on, and not just the form before the current one? This is for my 'help' form, as users can visit the form through many other forms. My coding was as follows. I am using Winforms
namespace Spanish_Quiz
{
public partial class Help : Form
{
public Help()
{
InitializeComponent();
CenterToScreen();
}
private void btnBackToStart_Click(object sender, EventArgs e)
{
Homescreen Homescreen = new Homescreen();
Homescreen.Show();
Homescreen.Activate();
this.Hide();
}
private void btnBack_Click(object sender, EventArgs e)
{
/*This is where my problem is,
what code do I write here that could be similar to
my 'back to start' button's code?*/
}
}
}

how to pass datagridview value to another form

i am working with my c# windows aplication, in this application form named "patients" and other named "patientsDuplicatedName" which contain datagridview and load all Duplicated Patients Name (this and works fine,,)
but i want when slsected Row get all values into form "patients" at run time (already open) without creating new form "Patients"..
Below is the code I am referring to:
public partial class frmPatientsNameDuplicated : Form
{
PatientFiles frmPatientsFiles =new PatientFiles() ;
public frmPatientsNameDuplicated()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnOk_Click(object sender, EventArgs e)
{
frmPatientsFiles.txtFileNum.Text = this.dgvPatientsName.CurrentRow.Cells[0].Value.ToString();
frmPatientsFiles.txtArbName.Text = this.dgvPatientsName.CurrentRow.Cells[1].Value.ToString();
frmPatientsFiles.txtEngName.Text = this.dgvPatientsName.CurrentRow.Cells[2].Value.ToString();
//frmPatientsFiles.show();//this line is creating new form and run
this.Close();
}
}
sorry about my bad english & thanks in advance
The commented out line frmPatientsFiles.show() has a comment that says that the line is creating a new form. It is not. It is simply displaying a form that has been previously created on line PatientFiles frmPatientsFiles = new PatientFiles(); This appears to be creating the new form that you don't want. If you already have an existing form that you want to update, reference that form from your btnOk_Click event handler. To do this, you probably want to pass in a reference to the (existing) form to your class, either via the constructor or some other method/property. I hope I have understood your question correctly.
i found same issue here :
Pass data to a existing form
so my code become
public partial class frmPatientsNameDuplicated : Form
{
PatientFiles frmPatientsFiles = Application.OpenForms["PatientFiles"] as PatientFiles;
public frmPatientsNameDuplicated()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnOk_Click(object sender, EventArgs e)
{
frmPatientsFiles.txtFileNum.Text = this.dgvPatientsName.CurrentRow.Cells[0].Value.ToString();
frmPatientsFiles.txtArbName.Text = this.dgvPatientsName.CurrentRow.Cells[1].Value.ToString();
frmPatientsFiles.txtEngName.Text = this.dgvPatientsName.CurrentRow.Cells[2].Value.ToString();
this.Close();
}
}

how to set a custom control button's job after adding it to some form?

Im making a userControl named [File_Manager] and i was wondering if i can add a button to this custom control that i can set its job later after adding this custom control to another form .. something like
File_Manager fManager = new File_Manager();
fManager.SetFreeButtonJob( MessageBox.Show("Hello") ); // something like this.
then whenever user press that button .. the messageBox shows up.
So.. Is it possible to do that?
thanks in advance.
Sure you can. Just attach the buttons click handler to the action you pass in.
fManager.SetFreeButtonJob(() => MessageBox.Show("Hello"));
private void SetFreeButtonJob(Action action)
{
button1.Click += (s,e) => action();
}
Just note that passing in the Action breaks the encapsulation of user control though. You should probably do something like SetFreeButtonJob(Jobs.SayHello); and put the knowledge of what to do inside the control.
Create a custom event for your UserControl and fire it when your Button is clicked. You can then attach an event handler to the custom event in your Form. Or you can just raise the UserControl's Click Event when your Button is clicked.
public delegate void CustomClickEventHandler(object sender, EventArgs e);
public partial class buttonTest : UserControl
{
public event CustomClickEventHandler CustomClick;
public buttonTest()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CustomClick(sender, e);
}
}
and in your Form
public Form1()
{
InitializeComponent();
buttonTest1.CustomClick +=new CustomClickEventHandler(userControl1_ButtonClick);
}
private void userControl1_ButtonClick(object sender, EventArgs e)
{
MessageBox.Show("Hello");
}
Or as my second option try.
private void button2_Click(object sender, EventArgs e)
{
OnClick(e);
}
and in your Form subscribe to the UserControl's Click event.
buttonTest1.Click +=new EventHandler(buttonTest1_Click);
private void buttonTest1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello Again");
}

Categories