I 'am making a WindowsForms application. I have 2 Forms :
On the first form (Form1), There are many fields (Textboxs) that should be filled by the user, then click to a button (Transfer).
This button should show all the input datas of Form1 in a new Form (Form2).
I'm not sure how to begin to move data from one form to another. Can somebody guide me how to do it?
If Form1 creates Form2 specifically to show the data, then you can use a non-default constructor to pass the information as you create the form.
First, let's consider an example of the information you want to transfer, and name it Form2Info:
// This class is an example of the information you want to transfer
public class Form2Info
{
public string text1;
public int number1;
}
Then, you modify Form2's constructor to take the information:
public partial class Form2 : Form
{
private Form2Info info;
public Form2(Form2Info information)
{
InitializeComponent();
info = information;
// Do something with this information, such as populate a TextBox or Label on the form.
}
}
Finally, you want to create a Form2 instance from your Form1:
// Create the information you want to pass; we fill it with some placeholder data here.
Form2Info info = new Form2Info();
info.text1 = "Hello"
info.number1 = 5;
// Now create the form and pass the data
Form2 form2 = new Form2(info);
form2.ShowDialog(); // Show modal dialog.
Each Textbox has a value (Textbox.Text property). You will have to transfer the content of these different properties to the new controls in your second form.
The easiest way will be through a custom Form constructor for the second form.
public Form2(string textBox1Value, string textBox2Value)
// etc... add as many as you like or use an object that holds all values in properties
{
InitializeComponent(); // Required by WinForms
this.TextBox1.Text = textBox1Value;
this.TextBox2.Text = textBox2Value;
}
Make sure you match the text boxes you want using the names. Finally when creating the form on the Transfer button code, call this constructor instead.
Related
I have one main form with controls like datagridview, combobox , textboxes and so on.I have created another which i want to use it as a wizard for selection as am trying to make it look user friendly. Now i want to be able to inherit controls inside the main form. i don't have a class nor create it as i don't wanna use it. i know how to create class and call my class within the form i want to access the method, properties and function.
How can i access the controls from one form to another.
e.g datagridview and textbox are from form1. on form two,i want to be able to access those two controls(datagridview and textbox). Please note here am giving as an example but from my main form i have more that two kinds of control.
My code from the wizard form
private void btnBrosweInput_Click(object sender, EventArgs e)
{
BrowseFile();
ReadFile(ref Gridview_Input,
ref txtInputfile,
ref cboLanguage,
bref btnNewfile);
//Errors that m getting from this method
1.Gridview_Input' does not exist in the current context
2.The best overloaded method match for .FrmExisitngFile.ReadFile(ref System.Windows.Forms.DataGridView, ref System.Windows.Forms.TextBox, ref System.Windows.Forms.ComboBox, ref System.Windows.Forms.Button)' has some invalid arguments
3.Error 5 Argument 1: cannot convert from 'ref Gridview_Input' to 'ref System.Windows.Forms.DataGridView'
}
I have place this method for readfile on the form
private void ReadFile(ref DataGridView _Grid, ref TextBox _InputTexBox, ref ComboBox _cboLanguage, ref Button _btnNew)
{
_Grid.Rows.Clear();
string PathSelection = "";
if (_InputTexBox.Text.Length > 0)
{
PathSelection = _InputTexBox.Text;
}
oDataSet = new DataSet();
XmlReadMode omode = oDataSet.ReadXml(PathSelection);
for (int i = 0; i < oDataSet.Tables[2].Rows.Count; i++)
{
string comment = oDataSet.Tables["data"].Rows[i][2].ToString();
string font = Between(comment, "[Font]", "[/Font]");
string datestamp = Between(comment, "[DateStamp]", "[/DateStamp]");
string commentVal = Between(comment, "[Comment]", "[/Comment]");
string[] row = new string[] { oDataSet.Tables["data"].Rows[i][0].ToString(), oDataSet.Tables["data"].Rows[i][1].ToString(), font, datestamp, commentVal };
_Grid.Rows.Add(row);
_btnNew.Enabled = true;
}
}
from the wizard form am trying to access the controls from the main form which contains the grid where everything will get loaded on
There is a lot of things going on in your code, but the main problem is that you try to manipulate data from your UI controls. This is a tremendously bad idea and it dramatically complicates your design (as you can already see from this fairly small feature).
As I stated in the OP's comments, you don't want to manipulate the controls to work with data. You want to manipulate data from POCO (Plain Old CLR Objects). And then update (or better: databind) all the controls that expose those data.
So you should start by doing something like this:
public class Form2 : Form
{
private List<Things> _listOfThings;
private string _inputText;
private LanguageEnum _lang;
...
// Receives data, NOT controls!
public Form2(List<Things> listOfThings, string inputText, LanguageEnum lang, ...)
{
_listOfThings = listOfThings;
...
}
...
}
Your Form1 reflects the state of listOfThings via a DataGridView, inputText via a TextBox and so on. You get the idea. Form1 pass those data (NOT the controls) to Form2 via constructor injection:
Form2 form2 = new Form2(listOfThings, inputTextBox.Text, ...);
form2.ShowDialog();
You read and update the data using the private fields in the Form2 class. When Form2 is closed, you update Form1's controls, either explicitly or using Databinding.
In my form1 i have my tblContacts in a datagridview, the user can click the button next/previous to navigate through the database. form1 also has an update button which leads to form 2 which is a details view of the same dataset from form1. Can someone tell me how i can make form2 automatically load the contact details of the specific contact that form1 datagridview has been selected on.
Thanks.
firstly: in your form2 class, create constructors which hold information from the datagrid, for example;
public string variable1 {get;set;}
public int variable2 {get;set;}
// as long as they're public, it doesnt matter what you call them, or there type
//(just make sure that you're using the right type, for datagrid's, string is
//usually fine but you can always convert)
secondly: when you create a new instance of your form, you can then assign the variables to "pass" to your new form, like so
Form2 form2 = new Form2();
form2.variable1 = dataSource[rowIndex]["columnName"]
form2.variable2 = dataSource[rowIndex2]["columnName2"]
to get the selected row index, do;
dataTable.SelectedRows[0].Index;
instead of [rowIndex]
From here, in your form2 you can use these values to do whatever you want with them
I am trying to write a program that opens an arbitrary number of forms ( each one containing a label) when the user clicks on a button, and i put them on a list:
List<Form> formlist = new List<Form>();
...
public void showFrame()
{
Form f = new Form();
// I add the components i need ...
formlist.Add(f)
}
What i need now is, given the i index of the form in formlist, to change the label.Text of that form.
Is possible to do it, withous using a different name for each lavel?
Give the control(s) you add to the form a Name. Then it is
formlist[i].Controls["somename"].Text = "mumble";
i'm using a main form and edit form, and i want to use the edit form text boxes in the main form, how can i do it?
edit
can't use user controls.
The easiest way would be create properties that expose the text fields. Call your edit form, then read the properties back.
public class MainForm
{
private void OnEditClick()
{
EditForm editForm = new EditForm();
DialogResult result = editForm.ShowDialog(this);
//check the result for ok/cancel etc if your using them.
whatever = editForm.TextBox1;
whatever2 = editForm.TextBox2;
}
public class EditForm
{
public string TextBox1 { get { return textBox1.Text;} }
public string TextBox2 { get { return textBox2.Text;} }
// etc
}
You could expose the whole control, but if all you care about is the contents of the text boxes, creating properties to expose just those is cleaner.
Does it have to be live? If not, add a property on the edit form and store the value (like an OpenFileDialog does when retrieving the .Filename). After it's closed, retrieve back the property and place it in the main form.
If it does need to be live, you probably need to use events (implement something close to INotifyPropertyChanged in Silverlight) then have the mainform attach to the edit form's events and update the controls as necessary (remember to check if InvokeRequired!)
I have a winforms application.
I have a textbox on one form (call F1) and when a button is clicked on this form (call F2), it launches another form.
On F2, I want to set a string via a textbox (and save it to a variable in the class), and then when I close this form, the string will appear in a label in F1.
So I am basically sharing variables between both forms. However, I can't get this to work correctly. How would this code look?
I would add a new property to form2. Say it's for a phone number. Then I'd add a friend property m_phone() as string to form 2. After showing an instance of form2 but before closing it, you can refer to the property m_phone in form1's code.
It's an additional level of indirection from Matthew Abbott's solution. It doesn't expose form2 UI controls to form1.
EDIT
e.g.:
public string StoredText
{
get;
private set;
}
inside the set you can refer to your UI control, like return textBox1.text. Use the get to set the textbox value from an earlier load.
And:
public string GetSomeValue()
{
var form = new F2();
form.ShowDialog();
return form.StoredText;
}
Just ensure that StoredText is populated (or not, if appropriate) before the form is closed.
Are you showing the second form as a dialog, this is probably the best way to do it. If you can avoid doing shared variables, you could do the following:
public string GetSomeValue()
{
var form = new F2();
form.ShowDialog();
return form.TextBox1.Text;
}
And called in code:
Label1.Text = GetSomeValue();
This might not be the most efficient way of approaching, but you could create a class called DB (database). Inside this class, create variables like
public static bool test or public static bool[] test = new bool[5];
In your other forms, you can just create an instance. DB db = new DB(); then grab the information using db.test = true/false. This is what I've been doing and it works great.
Sorry, I'm only like a year late.