The empty string passes from one form to another in Winforms
source link: https://www.codesd.com/item/the-empty-string-passes-from-one-form-to-another-in-winforms.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
The empty string passes from one form to another in Winforms
I have following problem: I have a Form1
where I open a second Form2
from it. Now I have a save Button
in Form2
where entries from Textboxes are saved to a csv file. But I want to save some entries from Form1
too. The Textbox
entries from Form2
are getting saved but entries from Form1
are empty strings. Following code:
In Form1:
public void showInputToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show();
}
to open the second Form via the first one and functions to grab the entries from the Form1 Textboxes:
public String getLocation()
{
return LocationBox.Text;
}
public String getFilesLoc()
{
return FilesLocation.Text;
}
In Form2 I have the following:
private Form1 m_form = null;
public Form2(Form1 f)
{
InitializeComponent();
m_form = f;
}
and then the function to grab the entries and save them:
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
proc.setParams(form1.getLocation(),getFilesLoc());
proc.saveCurrentSettings();
}
I left the other parameters out. So entries from Form2
are read correctly but the ones from Form1
are just an empty string (""). What can I do?
In the click handler, you're creating a new Form1
here:
Form1 form1 = new Form1();
That will have empty values - but you want the value from the existing form which you kept a reference to in your constructor - so use it!
private void button1_Click(object sender, EventArgs e)
{
proc.setParams(m_form.getLocation(), m_form.getFilesLoc());
proc.saveCurrentSettings();
}
(I'd strongly advise you to start following .NET naming conventions, quite possibly turning those get
methods into properties, and also considering passing the values into your Form2
constructor instead of the Form1
reference itself. It depends on whether you need to "see" any changes made to Form1
after the construction of Form2
.)
Recommend
-
7
How do I use an empty string in Regex Literal? advertisements I thought I'd space out after every letter in a string, for example foo
-
4
Find li whose children an element has an empty string advertisements Example HTML <li class= "View"> &...
-
4
ng table does not filter string empty in strict comparison advertisements I am using Ng-Table to show a list of items with 3 col...
-
2
Conversion of an empty string to a date, then return advertisements I'm trying to do some validation testing in...
-
3
[Golang] Remove Leading and Trailing Empty Strings in String Array J...
-
5
How to Check for an Empty String in JavaScript 1062 views 2 years ago Javascript Use the ===
-
4
The empty Windows Runtime string is not just a pretty face R...
-
3
Check if a string can be made empty by repeatedly removing given subsequenceLast Updated : 03 Aug, 2021Given a
-
4
Loading...This forum...
-
1
Minimum steps to empty String of 'a's and 'b'sSkip to content
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK