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.
Related
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.
Just so you are all aware, I have almost solved the issue BUT there is a key item in my way.
Alright. I have a "DataModule" class where there are several DataAdaptors, my DataSet, and the Connection.
My database is working fine, and so is saving and retrieving (including updating and deleting) data. If I put a TextBox on the DataModule form, and use an OleDbReader, and the following statement: txtBoxTest.Text = reader["firstName"].ToString();
Then the selected record's (which I have chosen by ID number) first name appears in the box.
However, the ACTUAL place for this data to appear is on another form, and as we know, components on a Windows Forms Application are set to "private", by default. Thus, my dilemma.
Question is below this line
How can I make the data appear in the correct form's TextBoxes (which is PatientRecord.cs) using the DataModule class' reader, and the statement of code I provided?
Many thanks!
There are Some way for Data transfer between forms.I explain one of common ways:
Sender form:
FrmChild child = new FrmChild(number,line,this.Width);
child.Owner = this;
child.Show();
Receiver form:
public partial class FrmChild : Form
{
int frmw;//A public varaible
public FrmChild()//normal constrauctor
{
InitializeComponent();
}
public FrmChild(String cNumber,byte LINE,int w)//custom constrauctor
{
frmw = w;
InitializeComponent();
}
}
I have a windows form application with a ComboBox on it and I have some strings in the box. I need to know how when I select one of the strings and press my create button, how can i make that name show up on another windows form application in the panel I created.
Here is the code for adding a customer
public partial class AddOrderForm : Form
{
private SalesForm parent;
public AddOrderForm(SalesForm s)
{
InitializeComponent();
parent = s;
Customer[] allCusts = parent.data.getAllCustomers();
for (int i = 0; i < allCusts.Length; i++)
{
Text = allCusts[i].getName();
newCustomerDropDown.Items.Add(Text);
newCustomerDropDown.Text = Text;
newCustomerDropDown.SelectedIndex = 0;
}
now when i click the create order button I want the information above to be labeled on my other windows form application.
private void newOrderButton_Click(object sender, EventArgs e)
{
//get the info from the text boxes
int Index = newCustomerDropDown.SelectedIndex;
Customer newCustomer = parent.data.getCustomerAtIndex(Index);
//make a new order that holds that info
Order brandSpankingNewOrder = new Order(newCustomer);
//add the order to the data manager
parent.data.addOrder(brandSpankingNewOrder);
//tell daddy to reload his orders
parent.loadOrders();
//close myself
this.Dispose();
}
The context is not very clear to me, but if I got it right, you open an instance of AddOrderForm from an instance of SalesForm, and when you click newOrderButton you want to update something on SalesForm with data from AddOrderForm.
If this is the case, there are many ways to obtain it, but maybe the one that requires the fewer changes to your code is this one (even if I don't like it too much).
Make the controls you need to modify in SalesForm public or at least internal (look at the Modifiers property in the Design section of the properties for the controls). This will allow you to write something like this (supposing customerTxt is a TextBox in SalesForm):
parent.customerTxt.Text = newCustomerDropDown.SelectedItem.Text;
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.