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
Related
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
I want to create a new user and save it in my list. Im new in C#, and I don´t know how I´m going to solve this. I´m using list.
using MetroFramework.Forms;
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 SGE
{
public partial class Registar_Utilizador : MetroForm
{
string username, password, tipo;
List<Pessoa> todos = new List<Pessoa>();
List<Pessoa> novaListaPessoa = new List<Pessoa>();
public Registar_Utilizador(List<Pessoa> todos)
{
InitializeComponent();
}
private void metroButtonAdicionar_Click(object sender, EventArgs e)
{
int i = 0;
Pessoa p = new Pessoa();
do
{
p.setusername(metroTextBoxUsername.Text);
}while(i < todos.Count && p.getusername() == todos[i++].getusername());
}
}
}
[Error] Error 1 Use of unassigned local variable 'todos'
In your Registar_Utilizador, you don't need to pass the list. You are doing nothing with it that I see.
public Registar_Utilizador(List<Pessoa> todos)
{
InitializeComponent();
}
Now for this method: you will change it to below - I have shown using foreach:
private void metroButtonAdicionar_Click(object sender, EventArgs e)
{
foreach(Pessoa p in todos)
{
if (someCondition) //p.getusername() == todos[i++].getusername()
{
p.setusername(metroTextBoxUsername.Text);
novaListaPessoa.Add(p)
}
}
}
if you want to create a new instance of objects in todos, then you can use information from here to copy one object to another
copy one object to another
This question already has answers here:
How to reset bindingsource filter to nothing
(4 answers)
Closed 6 years ago.
How can I automatically or by button clear the filter applied ?
I use the following code to filter a DataGridView from frPlanMain (Form 1) from 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 Plan_de_lucru_1._0
{
public partial class SearchWindow : Form
{
public frPlanMain refTofrPlanMain;
public SearchWindow(frPlanMain f) //<<Edit made here
{
refTofrPlanMain = f;
InitializeComponent();
}
private void SearchButtonW_Click(object sender, EventArgs e)
{
{
(refTofrPlanMain.dGVPlan.DataSource as DataTable).DefaultView.RowFilter = string.Format("Vodic = '{0}'", searchTBoxW.Text);
for (int i = refTofrPlanMain.dGVPlan.Rows.Count - 1; i >= 0; i--)
{
DataGridViewRow item = refTofrPlanMain.dGVPlan.Rows[i];
if (item.Visible)
{
refTofrPlanMain.dGVPlan.Rows.RemoveAt(i);
break;
}
//
}
}
}
}
}
To clear a filter on a DataView, set its RowFilter property to an empty string
yourDataView.DefaultView.RowFilter = ""
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_1._0
{
public partial class SearchWindow : Form
{
public frPlanMain refTofrPlanMain;
public SearchWindow(frPlanMain f) //<<Edit made here
{
refTofrPlanMain = f;
InitializeComponent();
}
private void SearchButtonW_Click(object sender, EventArgs e)
{
(refTofrPlanMain.dGVPlan.DataSource as DataTable).DefaultView.RowFilter = string.Format("Vodic = '{0}'", searchTBoxW.Text);
for (int i = refTofrPlanMain.dGVPlan.Rows.Count - 1; i >= 0; i--)
{
DataGridViewRow item = refTofrPlanMain.dGVPlan.Rows[i];
if (item.Visible)
{
refTofrPlanMain.dGVPlan.Rows.RemoveAt(i);
break;
}
}
}
private void clearFilter_onClick(object sender, EventArgs e){
(refTofrPlanMain.dGVPlan.DataSource as DataTable).DefaultView.RowFilter = null;
}
}
}
See here: How to reset bindingsource filter to nothing
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;
}
}
}
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;
}