|
hi guys,
how don't textbox inputs get deleted once going 1 form to another?
I created 2 form. Once I click next button to go 2nd form and click previous button to come back , I see all inputs at textboxes are gone. How can we save inputs at switching forms?
thanks.
modified 13-Feb-19 21:02pm.
|
|
|
|
|
This type of problem usually occurs because the first Form is re-created, and the previous instance of the Form is thus, "lost."
I'll be happy to help you, but, first, you need to make an effort: post your code. Describe what is the "Main form," and where and how the second Form is created. State clearly if this is a Windows Forms project.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
hello.first of all thank you. as a beginner of c#, I try to make a windows forms application which contain 2 forms and some labels,textboxes,comboboxes. program will make all entries a pdf file at the end. that's pretty much what I want to build.
below is code of form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
namespace lastcheck
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label4_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
}
private void label11_Click(object sender, EventArgs e)
{
}
private void label13_Click(object sender, EventArgs e)
{
}
private void label26_Click(object sender, EventArgs e)
{
}
private void label12_Click(object sender, EventArgs e)
{
}
private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2sec = new Form2();
form2sec.Show();
Hide();
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
}
and form2 codes:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
namespace lastcheck
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void label17_Click(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
Form1 form1sec = new Form1();
form1sec.Show();
Hide();
}
private void button3_Click(object sender, EventArgs e)
{
Close();
}
}
}
modified 13-Feb-19 21:02pm.
|
|
|
|
|
And there is your problem:
Form 1 does this:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2sec = new Form2();
form2sec.Show();
Hide();
}
And Form2 does this:
private void button5_Click(object sender, EventArgs e)
{
Form1 form1sec = new Form1();
form1sec.Show();
Hide();
}
Which means that each time you click a button, you create a new instance of the form. That's not the same as previous one!
Think of it like this:
You get into your car, and put your phone in the glove box. Then you buy a new car. Would you expect your phone to be in the new car's glove box? Of course not! You know the difference between "this car" and "that car", "old car" and "new car" - and you know that whatever is in the glove box of one car is not in the glove box of any other.
Computer classes work the same way: Each time you use the new keyword you are "buying a new car" - only it's a form instead of a car!
What you need to do probably most easily done like this:
Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2sec = new Form2();
form2sec.ShowDialog();
string valueFromForm2 = form2sec.getTheValueProperty;
} Form2:
private void button5_Click(object sender, EventArgs e)
{
Close();
}
public string getTheValueProperty
{
get { return myTextBox.Text; }
}
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: You get into your car, and put your phone in the glove box. Then you buy a new car. Would you expect your phone to be in the new car's glove box? That is wonderful
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
If you can afford a new car, why not buy a new phone as well? with a hands-free kit, and matching colors...
|
|
|
|
|
Because my old one works? Or at least it did until I sold the old car.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: You get into your car, and put your phone in the glove box. Then you buy a new car. Would you expect your phone to be in the new car's glove box? Of course I would. If it wasn't, I would expect Jeeves to deliver a damn good thrashing to the salesman for being so remiss. It's all about the service old chap.
This space for rent
|
|
|
|
|
Why would you put your phone in the glove box? Surely that is your chauffeur's part of the automobile?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Good grief, you don't let the chauffeur drive the Jag old bean.
This space for rent
|
|
|
|
|
Hi:
How to define a recursive lambda function using C#?
By example, I was using the following sentence to get the even numbers in a Real group:
double[] doubles = Odd(a, (double x) => x + 2.0);
Any ideas?
Ieshua Carroll
Systems Engineer
|
|
|
|
|
|
It is an useful link. Thanks !
|
|
|
|
|
yw
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
However one should read the following and understand it.
StackOverflowException Class (System)[^]
Especially understand that it will terminate the AppDomain absolutely.
And read the following...
"if your app depends on recursion, use a counter or a state condition to terminate the recursive loop. The following example uses a counter to ensure that the number of recursive calls to the Execute method do not exceed a maximum defined by the MAX_RECURSIVE_CALLS constant. "
|
|
|
|
|
If you're using a recent version of the C# compiler, you might want to consider local functions:
Local functions (C# Programming Guide) | Microsoft Docs[^]
Using the function from the blog post that Eddy linked to, it works almost without change:
int Fib(int n) => n > 1 ? Fib(n - 1) + Fib(n - 2) : n;
Console.WriteLine(Fib(6));
(Of course, the best solution for calculating Fibonacci numbers is to avoid recursion. )
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,savers.
i can move my row but after binding it will back to the previous state. how can i update it to my database e.g the second row become third row in both datagridview and database and also the ID column become update (resort assending from 1 to the end and 2become3 and 3become2) in my database(i can update that in datagridview by a foreach loop).
please help me.....
please guide me by your codes .
thank you in advance.
|
|
|
|
|
Add a column to the table and specify a value to determine the order, or create an index. Don't try to "order" rows in a database-table. A database is not meant for direct viewing, and the order in which it is stored is not related to the way it is shown.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
thank you so much for your answer.
i have to sort them in my database because some calculation will perform in the DBS that data sorting and ID Numbers are very important.
thank you any way to care about my question.
i'll be grateful if you add your comments if anything else ocured to your mind.
|
|
|
|
|
Member 13325846 wrote: i have to sort them in my database Rows in a database have no particular order. You can specify the order when fetching the rows, or if you want to save a user-defined "order", you add that column. Don't muck with the order that the rows are saved in.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
thank you for you guide my friend.
best wishes.
|
|
|
|
|
You're welcome
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
i am loading my data with a dataReader but every time i am select another table, the new selected Rows will add below the previous table Rows. how can i empty it and load Only data of the new selected table.
thank you in advance.
here is my code:
OleDBConnectio con=new OleDBConnection(conectionString);
con.Open();
OleDBDataCommand com=new OleDBCammand(“Select *From[“+txtBox1.Text+”]”,con);
OleDBDataReader dr;
dr=com.ExecuteReader();
While(dr.read())
{
Dgv1.Rows.Add(dr[“ID”],dr.[“name”].ToString());
}
dr.Close();
con.Close();
i should mention that i don't want use from data source to bind my database to DGV, because i want to add row in my DGV programmatically.
thanks a lot.
|
|
|
|
|
Dgv1.Rows.Clear();
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi, thank you legitimate programmer.
thanks a million for your help
|
|
|
|