I have an application with 2 form,
1 form reading from .csv file to insert to SQL Server,
1 form will be show when trouble happen ( Can not reading .csv file or reading error)
My code as below:
public void Timer_tick ( object sender eventargs e)
{
PushData(); // with time interval =100
}
public void PushData()
{
Form2 fr = new Form2();
// function to reading .csv file
if ( checkData(name) == "OK")
{
//update data to SQL Server
}
else
{
timer1.Stop();
fr.Show(); // User need to click OK button to hide Form2 and come back Form1
this.Refresh();
}
}
It's only one time show Form2.
But after User click button at Form2 to comback Form1. But Form1 not working normal with timer.
Please help me to solve this application.
Thank you!
Show the form as dialog, and then restart the timer when finished:
timer1.Stop();
fr.ShowDialog(); // User need to click OK button to hide Form2 and come back Form1
timer1.Start();
You can use the message box rather than using form2 and display error message. Refer the similar and sample code below.
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
PushData();
}
public void PushData()
{
Form2 fr = new Form2();
// function to reading .csv file
if (checkBox1.Checked==true)
{
progressBar1.Value += 1;
//update data to SQL Server
}
else
{
timer1.Stop();
MessageBox.Show("Error message");
timer1.Start();
}
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
}
Try the below code and let me know is it working or not
public void Timer_tick ( object sender eventargs e)
{
PushData(); // with time interval =100
}
public void PushData()
{
Form2 fr = new Form2();
frm.Closed += fr_Closed;
// function to reading .csv file
if ( checkData(name) == "OK")
{
//update data to SQL Server
}
else
{
timer1.Stop();
fr.Show(); // User need to click OK button to hide Form2 and come back Form1
this.Refresh();
}
}
void fr_Closed(object sender, EventArgs e)
{
timer1.Start();
}
Related
I'm trying to display a website in a form and automatically reload the website to see if a certain text is present. I'm doing this in a C# form and try to call a while with an button. This is the code I made:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnStartLoop_Click(object sender, EventArgs e)
{
lblStatusLoop.Text = "Status loop: Started";
lblStatusLoop.ForeColor = System.Drawing.Color.Green;
int i = 0;
while (i < 10)
{
Browser.Navigate("www.google.com");
Browser.DocumentCompleted += Browser_DocumentCompleted;
Thread.Sleep(5000);
i++;
}
}
void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string ZoekText = txtSearchbox.Text.ToString();
if (Browser.DocumentText.Contains(ZoekText))
{
lblSearchResult.Text = "Text found";
}
else
{
lblSearchResult.Text = "Text not found";
}
}
}
As soon as I push the button the appliction freezes and I have to force quit it. I've done some research on google and suggestions were to wait for the document to complete loading but it has no effect.
I don't have much programming experience and do not have someone to fall back on so I hope someone here can point me into right direction.
I want to show data in DataGridView which resides in form1 and data reside in form2. Form2 has a button named “ADD” which adds all the data in form1’s grid. Following code is working properly for the same form, how do I edit this code in order to show data from another form.
private void btn_Add_Click(object sender, EventArgs e)
{
InputUserInfo frm1 = new InputUserInfo();
frm1.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
frm1.dataGridView1.RowTemplate.Height = 120;
frm1.dataGridView1.AllowUserToAddRows = false;
int numberOfRows = frm1.dataGridView1.Rows.Count;
if (numberOfRows < 5)
{
MemoryStream ms = new MemoryStream();
thumb_pic.Image.Save(ms, thumb_pic.Image.RawFormat);
byte[] img = ms.ToArray();
frm1.dataGridView1.Rows.Add(lbl_CP_UserID.Text, lbl_CP_Name.Text, img);
}
else
{
MessageBox.Show("Please insert Only 5 Images");
}
this.Hide();
frm1.Show();
}
Maybe you have found a solution already but this might help someone in future.
The form (see pic Form1) with grid
Code behind this Form:
public Form1()
{
InitializeComponent();
}
public DataGridView DataGridView1
{
get
{
return dataGridView1;
}
}
private void btnAddItems_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.TopMost = true;
frm.ShowDialog();
}
Form (See pic Form2) to add data into Form1 dataGridView
Code behind this Form:
public Form1 frm1;
public Form2(Form1 gridForm)
{
InitializeComponent();
frm1 = gridForm;
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (frm1.DataGridView1.Rows.Count < 5)
{
frm1.DataGridView1.Rows.Add(txtOrderNo.Text, txtDesc.Text);
frm1.DataGridView1.Refresh();
txtOrderNo.Text = txtDesc.Text = "";
if (frm1.DataGridView1.Rows.Count == 5) this.Close();
}
else
{
MessageBox.Show("You can only add up to 5 items.");
}
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
I am using Visual studio 2012. I created two forms, form1 with a button to open form2 and the form2 has a button "exit" which will take me back to form1.
this is my code from form 1:
private void btnRecords_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
this.Hide();
}
and from form 2:
private void btnExit_Click(object sender, EventArgs e)
{
this.Hide();
}
I know I can use frm2.Show(); this.Hide(); instead of frm2.showdialog();. But, I need my form 1's state to be unaltered. My form1 contains a login form, which only enables the buttons (such as the new form button) if the login is correct. So if I hide form1 and just show it again, the login resets;
On Form2 class add a property to store the reference to the parent form:
public Form ParentForm { get; set; }
Then on Form1, you can show Form2 this way, hiding Form1 at the same time:
private void btnRecords_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ParentForm = this;
this.Hide();
frm2.ShowDialog();
}
When closing Form2, you can show Form1 again:
private void btnExit_Click(object sender, EventArgs e)
{
this.ParentForm.Show();
this.Close();
}
Or even better, close Form2 this way:
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.ParentForm != null)
this.ParentForm.Show();
}
This will also show Form1 back if we the user closes Form2 using the cross button in the title bar.
Why don't you handle the login in the form_load event of the main form. The form_load does not run every time the form regains focus. Is it appropriate to close the form on them if they do not login on load? In my case, I send an email with the username of windows user to Domain administrators and close the program. They have to reopen the program to give it another go.
private void frmMain_Load(object sender, EventArgs e)
{
//Check login
Form frmLogin = new Form();
frmLogin.ShowDialog();
if (frmLogin.LoginSucessful == true)
{
btnRecords.Enabled = true;
lblWarning.Visible = false;
}
else
{
btnRecords.Enabled = false;
lblWarning.Visible = true;
lblWarning.Text = "You must first Login";
}
//other setup code here
}
I am trying to open a secondary form immediately after the main form displays, but instead, the secondary form displays first and then the main form displays after the secondary one closes (The second form is acting like a splash screen). Here is an example:
private void Form1_Load(object sender, EventArgs e)
{
doSomething1(sender, e);
doSomething2(sender, e);
// The new form I want to open after the main form.
Form2 f2 = new Form2();
doSomething3(sender, e);
}
private void doSomething1(object sender, EventArgs e)
{
// Do something here...
}
private void doSomething2(object sender, EventArgs e)
{
// Do something here...
}
private void doSomething3(object sender, EventArgs e)
{
// Do something here...
}
Use the Form1.Shown event instead of the Load event:
private void Form1_Shown(Object sender, EventArgs e) {
doSomething1(sender, e);
doSomething2(sender, e);
// The new form I want to open after the main form.
Form2 f2 = new Form2();
doSomething3(sender, e);
}
Alternatively, depending on whether doSomething1 and doSomething2 are doing background processing that the user doesn't need to see: you could retain those two in the load handler, and merely move the last two statements into the Shown handler.
In your constructor initialized the Shown event handler first:
public Form1()
{
InitializeComponent();
this.Shown += new EventHandler(Form1_Shown);
}
Then put the code there:
void Form1_Shown(object sender, EventArgs e)
{
doSomething1(sender, e);
doSomething2(sender, e);
// The new form I want to open after the main form.
Form2 f2 = new Form2();
doSomething3(sender, e);
}
public Form1()
{
InitializeComponent();
Form2 f2 = new Form2();
f2.Show();
I'm putting together a simple UI that interacts with a SQL database. My problem is a UI problem, ever time a menustrip item is selected, it opens a new active window. How do I set this up to close the previous active window? I've tried using Form.Close();, but that just closes everything.
private void addCampusToolStripMenuItem_Click(object sender, EventArgs e)
{
if_add_campus go = new if_add_campus();
go.Show();
}
private void addDepartmentToolStripMenuItem_Click(object sender, EventArgs e)
{
if_add_dept go = new if_add_dept();
go.Show();
}
private void addEmployeToolStripMenuItem_Click(object sender, EventArgs e)
{
if_add_employee go = new if_add_employee();
go.Show();
}
Just keep track of the last form you created in a variable:
private Form lastForm;
private void showForm(Form frm) {
frm.FormClosed += (sender, ea) => {
if (object.ReferenceEquals(lastForm, sender)) lastForm = null;
};
frm.Show();
if (lastForm != null) lastForm.Close();
lastForm = frm;
}
And use showForm() to display your forms:
private void addCampusToolStripMenuItem_Click(object sender, EventArgs e)
{
showForm(new if_add_campus());
}
Not tested, should be close.