c# List and object - c#

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

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

DataGridView.DefaultView clear filter [duplicate]

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

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

Dll does not seem to be read, why?

I am creating a application form to view/change a tag from a software called InTouch.
I added the dll as a reference and I would like to use the Read(string tagName) fct in the IOM.InTouchDataAccess. VS does not see the fct Read when I write InTouchWrapper TagType = new read(). It only sees InTouchWrapper as I wrote in the code which gives me the error IOM.InTouchDataAccess.InTouchWrapper' does not contain a constructor that takes 0 arguments
I don't understand why is this happening. I am running the InTouch software while coding, maybe there is an access conflict with the software.
MyCode
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 IOM.InTouchDataAccess;
namespace TagBrowser
{
public partial class TagBrowser : Form
{
public TagBrowser()
{
InitializeComponent();
}
private void TagBrowser_Load(object sender, EventArgs e)
{
}
private void TagBox_TextChanged(object sender, EventArgs e)
{
}
private void TypeBox_SelectedIndexChanged(object sender, EventArgs e)
{
InTouchWrapper TagType = new InTouchWrapper();
}
The dll
using System;
using System.Collections.Generic;
using System.Text;
using NDde.Client;
namespace IOM.InTouchDataAccess
{
public class InTouchDdeWrapper : IDisposable
{
private int DDE_TIMEOUT = 60000;
private DdeClient _ddeClient;
public InTouchDdeWrapper()
{
_ddeClient = new DdeClient("View", "Tagname");
}
~InTouchDdeWrapper()
{
Dispose();
}
public void Initialize()
{
_ddeClient.Connect();
}
public string Read(string tagName)
{
return _ddeClient.Request(tagName, DDE_TIMEOUT).Replace("\0", "");
}
I'm putting this here in case somebody else would get the same problem:
Are you sure it's the correct dll you referenced? Try to open the
exact referenced dll in a decompiler (JustDecompile free,
Reflector or dotPeek free) and see if it's the code you
expect.

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