how to get values of Form1.cs into another .cs file - c#

this is code in Form1.cs. I want to get values of f_name, L_name etc in next dbfun.cs.
private void submit_Click(object sender, EventArgs e)
{
string f_name = first_name.Text;
string L_name = last_name.Text;
string user_email = email.Text;
string pass = password.Text;
string depart = department.SelectedItem.ToString();
string gender = "";
if (male.Checked)
{
gender = "Male";
}
else if (female.Checked)
{
gender = "Female";
}
String agree = accept.Text;
}

There nothing to do. Just create an object instance on Form2 with required number of Parameters for passing the values to Form2
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(textBox1.Text);
f2.Show();
}
public Form2(string value)
{
InitializeComponent();
textBox2.Text = value;
}

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;
namespace ValuesPassingFromformtoform
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string f_name = first_name.Text;
string L_name = last_name.Text;
string user_email = email.Text;
string pass = password.Text;
string depart = department.SelectedItem.ToString();
string gender = "";
Form2 f2 = new Form2(f_name,L_name,user_email,pass,depart);
f2.Show();
}
}
}
and Form2.cs is
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;
namespace ValuesPassingFromformtoform
{
public partial class Form2 : Form
{
public Form2(string f_name, string L_name, string user_email, string pass, string depart)
{
InitializeComponent();
textBox2.Text = f_name;
}
}
}

Related

Call a Get Method from Another Form, C#

I am sorry if my question is silly , I am a beginner.I have two forms:
Form1: Displays a Table of Information
Form2: Displays a Form to Fill information
I need to get the information in Form2 to Form1 Using get methods (If there is a better way please suggest it).
My problem is that when I type those get methods in Form1 they are not recognized.
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
//---------------------------------Initial Stuff------------------------------------
Form2 form2 = null;
//----------------------------------Constructor-------------------------------------
public Form1()
{
InitializeComponent();
}
private void nouveau_Click(object sender, EventArgs e)
{
if (form2 == null)
{
form2 = new Form2();
form2.Show();
}
}
//---------------------------------ListView of Information------------------------------
ListViewItem lvi = new ListViewItem(getClient());
lvi.SubItems.Add(societe.Text);
lvi.SubItems.Add(datedebut.Text);
lvi.SubItems.Add(type.Text);
lvi.SubItems.Add(etat.Text);
}
}
Form2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 :
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
client.Text="";
societe.Text = "";
datedebut.Text = "";
type.Text = "";
etat.Text = "";
}
//----------------------------Return Functions for table----------------------
public String getClient()
{
return client.Text;
}
public String getSociete()
{
return societe.Text;
}
public String DateDebut()
{
return datedebut.Text;
}
public String getType()
{
return type.Text;
}
public String getEtat()
{
return etat.Text;
}
}
}
So I update my code and tried another way to do things
Now I have 4 .cs files: Principal, FillInfo, Folder, Program
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Principal());
}
}
}
Folder:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
class Folder
{
//-----------------------------------------CONSTRUCTOR--------------------------
public Folder()
{
this.Customer = "";
this.Company = "";
this.StartDate = "";
this.TechUsed = "";
this.Status = "";
}
//-----------------------------------------GETTERS AND SETTERS-------------------
public string Customer { get; set; }
public string Company { get; set; }
public string StartDate { get; set; }
public string TechUsed { get; set; }
public string Status { get; set; }
}
}
Principal:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Principal : Form
{
//-----------------------------------INITIAL VARIABLES--------------------------------------------------
FillInfo fillinfo = null;
public Folder f;
//-----------------------------------INITIAL METHODS----------------------------------------------------
public Principal()
{
InitializeComponent();
}
//-----------------------------------ELEMENTS METHODS--------------------------------------------------
// NEW BUTTON
private void pNew_Click(object sender, EventArgs e)
{
f= new Folder();
if (fillinfo == null)
{
fillinfo = new FillInfo();
fillinfo.Show();
}
}
//---------------------------------------PROCESSING-----------------------------------------------------
ListViewItem fillInfoListView = new ListViewItem(f.getCustomer());
}
}
FillInfo:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class FillInfo : Form
{
//-----------------------------------INITIAL VARIABLES--------------------------------------------------
//-----------------------------------INITIAL METHODS----------------------------------------------------
public FillInfo()
{
InitializeComponent();
}
//-----------------------------------ELEMENTS METHODS--------------------------------------------------
private void fOkButton_Click(object sender, EventArgs e)
{
f.setCustomer = fCustomerTextField.Text;
f.setCompany = fCompanyTextField.Text;
f.setStartDate = FStartDateDatePicker.Text;
f.setTechUsed = fTechUsedDropList.Text;
f.setStatus = fStatusDropList.Text;
fCustomerTextField.Text = "";
fCompanyTextField.Text = "";
FStartDateDatePicker.Text = "";
fTechUsedDropList.Text = "";
fStatusDropList.Text = "";
}
}
}
Assuming Form2 is something that appears and asks the user for info, then disappears when the user is done typing into it, it would probably look like:
private void nouveau_Click(object sender, EventArgs e)
{
var form2 = new Form2();
form2.ShowDialog(); //SHOW DIALOG
ListViewItem lvi = new ListViewItem(getClient());
lvi.SubItems.Add(form2.Societe); //the property you are busy writing
lvi.SubItems.Add(form2.DateDebut); //the property you are busy writing
lvi.SubItems.Add(form2.Type); //the property you are busy writing. Try and think of a more hepful name than Type
lvi.SubItems.Add(form2.Etat); //the property you are busy writing
//do you need to add that lvi to something?
}
Remove Form2 from being a class level variable

Form1 GridView is not accessible by Form 2 Button

I am trying to interact the DataGridView, located in Form1 (frPlanDeLucru), by a button in Form2. The reason for this is to create a separate search window. I keep getting Error CS0122, frPlanDeLucru.dataGridView1' is inaccessible due to its protection level.
Please help.
The Code in Form 1:
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 System.Data.OleDb;
namespace Plan_de_lucru
{
[Serializable()]
public partial class frPlanDeLucru : Form
{
public frPlanDeLucru()
{
InitializeComponent();
}
public void TextBox1_TextChanged(object sender, EventArgs e)
{
}
public void ctrlLoad_Click(object sender, EventArgs e)
{
string constr = "Provider = MicroSoft.Jet.OLEDB.4.0; Data Source=" + TextBox1.Text + "; Extended Properties =\"Excel 8.0; HDR=Yes;\";";
OleDbConnection con = new OleDbConnection(constr);
OleDbDataAdapter sda = new OleDbDataAdapter("Select * From [" + textBox2.Text + "$]", con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
new Form2().Show();
this.Show();
}
}
}
The code in Form2:
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;
namespace Plan_de_lucru
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void search_button_Click(object sender, EventArgs e)
{
String str = "select * from searchBox where ( Name like '%' + #search + '%')";
BindingSource bs = new BindingSource();
bs.DataSource = frPlanDeLucru.dataGridView1.DataSource; //<- Here is the problem, do not know the fix
}
}
}
In Form2:
public DataGridView Dgv { get; set; }
In Form1:
Form2 f = new Form2();
f.Dgv = dt; //Add this to the ctrlLoad_Click
In Form2 access its own Dgv propety.
You could add a reference to form a when creating form b and expose a public method / property in that "formA" that returns the datasource you wish. An example in which this is used:
public class FormA
{
public FormA()
{
}
public object GetDataSource()
{
return datagrid1.DataSource.ToObject();
}
}
public class B
{
private A _formA;
public B(A formA)
{
_formA = formA;
}
private void GetDataSourceFromA()
{
object dataSource = _formA.GetDataSource();
}
}
This might lead to another problem though; the second form being on a different thread. Calling objects on the first form from a different thread is not possible, more info here, though I think out of scope for your question.
Thank you for your help. I found that Form1 GridView was set to private... i entered in the code behind Form1 and modified it to private.
Cheers.

In which part of the program should I put a function that keeps the application running continuosly in c#

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Temporizador
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
/*if (escolherHoraTxt.InvokeRequired == true)
escolherHoraTxt.Invoke((MethodInvoker)delegate { escolherHoraTxt.Text = "Invoke was needed"; });*/
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void escolherFicheiroBtn_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.ShowDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
string fileName;
fileName = dlg.FileName;
link.Text = fileName;
//MessageBox.Show(fileName);
}
}
private void escolherHoraTxt_MouseClick(object sender, EventArgs e)
{
if (escolherHoraTxt.Text == "--:--:--")
escolherHoraTxt.Text = " ";
}
private void escolherHoraTxt_TextChanged(object sender, EventArgs e)
{
if (escolherHoraTxt.Text == "--:--:--")
escolherHoraTxt.Text = " ";
}
private void gravarBtn_Click(object sender, EventArgs e)
{
}
private void gravarBtn_Click_1(object sender, EventArgs e)
{
String s = escolherHoraTxt.Text;
Horas hora = new Horas();
hora.IsValidTime(s);
hora.compareTime(s);
}
}
}
Horas.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Temporizador
{
class Horas
{
private String m_timeNow;
public String timeNow
{
get
{
return m_timeNow;
}
set
{
m_timeNow = value;
}
}
public Horas()
{
}
public string getCurrentTime()
{
m_timeNow=DateTime.Now.ToString("HH:mm:ss");
return m_timeNow;
}
public void IsValidTime(String theTime)
{
string[] timeArray = theTime.Split(new[] { ":" }, StringSplitOptions.None);
try{
Convert.ToDateTime(theTime);
}
catch (FormatException e)
{
MessageBox.Show(e.Message);
}
}
public void compareTime(String theTime)
{
string currentTime=getCurrentTime();
if (currentTime == theTime)
MessageBox.Show("SIM");
else
MessageBox.Show("NAO");
}
}
}
/I´m trying to make an application that it is always running and that will close and then open a file in a certain hour choosen by the user. I intend to use something like while(1){...} but I don´t know in what part of the program should I put this. Could someone help me please?/

C# get set error

This Form 1
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 System.Diagnostics;
using System.Threading;
using Managed.Adb;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
AndroidDebugBridge mADB;
String mAdbPath;
List<Device> devices = AdbHelper.Instance.GetDevices(AndroidDebugBridge.SocketAddress);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e )
{
//mAdbPath = Environment.GetEnvironmentVariable("PATH");
mAdbPath = "C:\\Users\\Nadun\\AppData\\Local\\Android\\android-sdk\\platform-tools";
mADB = AndroidDebugBridge.CreateBridge(mAdbPath + "\\adb.exe", true);
mADB.Start();
var list = mADB.Devices;
textBox1.Text = "" + list.Count;
foreach (Device item in list)
{
Console.WriteLine("");
listBox1.Items.Add("" + item.Properties["ro.build.product"].ToString() + "-" + item.SerialNumber.ToString() );
}
//Console.WriteLine("" + list.Count);
}
private void button2_Click(object sender, EventArgs e)
{
string text = listBox1.GetItemText(listBox1.SelectedItem);
Form2 f2 = new Form2(text);
// f2.Phone = "scs";
SetPhone sp = new SetPhone();
sp.PhoneModel = "Test";
this.Visible = false;
f2.ShowDialog();
}
}
}
This is Form 2
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;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
private string phone;
public string Phone
{
get { return this.phone; }
set { this.phone = value; }
}
public Form2(string a)
{
InitializeComponent();
textBox1.Text = a;
}
private void Form2_Load(object sender, EventArgs e)
{
//Form2 f2 = new Form2();
//f2.phone = "s";
//textBox1.Text = f2.Phone;
SetPhone sp = new SetPhone();
textBox1.Text = sp.PhoneModel;
Console.WriteLine("sefsef-"+sp.PhoneModel);
}
}
}
This is my Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication1
{
class SetPhone
{
private string phoneModel;
public string PhoneModel {
get { return this.phoneModel; }
set { this.phoneModel = value; }
}
}
}
Get always returning empty.i don't know why.
I am trying to set values from "form1".
i wrote class for that as well.but when i getting values from "form2" it returning empty.i don't know why
Your SetPhone class object which is calling the setter in the button2_click is a local variable, so when you try access the same in Form2_Load using another local variable, it is a completely new object and Get returns an empty string (default value). You should be able to share the SetPhone variable across forms, may be using constructor, then it will retain the values set using the setter

How to pass a string to child form?

Basically what I'm trying to do is I have a string on the main form that pulls its value from a textbox.
I then generate a modal version of a second form and want to have that string (or the main forms textbox1.text value) usable in the second form for processes.
Main Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace Tool{
public partial class MainForm : Form
{
public string hostname;
public MainForm()
{
InitializeComponent();
textBox1.Text = hostname;
}
public void btn_test_Click(object sender, EventArgs e)
{
string hostname = textBox1.Text;
SiteForm frmsite = new SiteForm();
frmsite.ShowDialog();
}
}
}
'
Child Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace Tool
{
public partial class SiteForm : Form
{
public string hostname {get; set; }
public SiteForm()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
label1.Text = this.hostname;
}
}
}
Any suggestions on how I can do this? I know there has to be a simpler way, sorry I'm still a bit of a noob and am trying to teach myself C# as I go.
The result is when I click the label on the child form it is blank, because of this I am able to deduce that the string isn't passing between the two forms correctly.
The simplest way is to pass it in the constructor of the Child form, for example:
private string _hostname = "";
...
public SiteForm(string hostname)
{
_hostname = hostname;
InitializeComponent();
}
Try hooking into your child form's Load event and set the value of its hostname property in an event handler on your main form.
public void btn_test_Click(object sender, EventArgs e)
{
string hostname = textBox1.Text;
SiteForm frmsite = new SiteForm();
frmsite.Load += new EventHandler(frmsite_Load);
frmsite.ShowDialog();
}
public void frmsite_Load(object sender, EventArgs e)
{
SiteForm frmsite = sender as SiteForm;
frmsite.hostname = this.hostname;
}

Categories

Resources