Unable to call method's objects into the Form - c#

As you can see I instantiated the classes I need into the form_load, in order to use methods and classes features. The problem is that I need to Call the item NuovoCliente from CreateClientemethod, but I don't know how to do, since intellisense, even when I try to type, does not show any link to NuovoCliente.
The class you can see with the method is ClienteModel.
Which is basically structured like this:
public class ClienteModel
{
public int IDCliente { get; set; }
public string Cognome { get; set; }
public string Nome { get; set; }
public string Indirizzo { get; set; }
}
This is my method, which is placed in DBMemoryManager class:
public class DBMemoryManager : DBManager
{
//Array
ClienteModel[] MemoryClienti = new ClienteModel[0];
public int CreateCliente(ClienteModel model)
{
ClienteModel NuovoCliente = new ClienteModel();
int MaxCID = MemoryClienti.Select(ClienteModel => ClienteModel.IDCliente).Max();
MemoryClienti[0] = NuovoCliente;
NuovoCliente.IDCliente = MaxCID++;
return NuovoCliente.IDCliente;
}
This is how my Form start:
public partial class Form1 : Form
{
DBMemoryManager dbMemoryManager = null;
ClienteModel clienteModel = null;
OrdineModel ordineModel = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dbMemoryManager = new DBMemoryManager();
clienteModel = new ClienteModel();
ordineModel = new OrdineModel();
}

Return a ClienteModel from this method.
public ClienteModel CreateCliente(ClienteModel model)
{
ClienteModel NuovoCliente = new ClienteModel();
int MaxCID = MemoryClienti.Select(ClienteModel => ClienteModel.IDCliente).Max();
MemoryClienti[0] = NuovoCliente;
NuovoCliente.IDCliente = MaxCID++;
return NuovoCliente;
}
Now access data from Form_load
public partial class Form1 : Form
{
ClienteModel clienteModel = null;
OrdineModel ordineModel = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
clienteModel = new ClienteModel();
ordineModel = new OrdineModel();
DBMemoryManager dbMemoryManager = new DBMemoryManager(); //initialize here
ClienteModel nuovoCliente = dbMemoryManager.CreateCliente(clienteModel)
//here you can get all data from nuovoCliente
}
here is the data.

Related

How do i get a list of object in a Listbox?

I was trying to create a personal list using WinForms. I try to create a new entry via button click. I have a list of objects with string properties Name and Number.
How can I show the list of objects in my ListBox?
namespace sometestname
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Personallistshow(object sender, EventArgs e)
{
}
public void NewItemButton_Click(object sender, EventArgs e)
{
Personallist.Add(new Entrys() { Name = NameBox.Text, Number = Numbox.Text });
}
public List<Entrys> Personallist= new List<Entrys>();
}
public partial class Entrys
{
public string Name { get; set; }
public string Number { get; set; }
}
}
The user has 2 Textboxfields. If they click the NewItemButton, than create a new Entrys object. This new Entrys object should be added to Personallist object and ListBox should show the updated list.
List<Entrys> someList = ...;
Personallist.DataSource = someList;
You should use a bindinglist and set the datasourcebinding after initializing your form.
Something like:
public partial class Form1 : Form
{
public BindingList<Entrys> Personallist = new BindingList<Entrys>();
public Form1()
{
InitializeComponent();
comboBox1.DataSource = Personallist;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Number";
}
private void button1_Click(object sender, EventArgs e)
{
Personallist.Add(new Entrys() { Name = "TESTNAME", Number = "TESTNR" });
}
}
public partial class Entrys
{
public string Name { get; set; }
public string Number { get; set; }
}

Select object and edit on form

I have a class an some objects from class. Now I want to select one of this object and display on my form. At the same time I want to edit the selected one. I used INotifyPropertyChanged and able to display selected object. Bu I have some troubles.
1- When I use myDislayingObject = myObject1 It does not work. So I have to use
myDislayingObject.property1 = myObject1.property1
myDislayingObject.property2 = myObject1.property2
I want to copy my object with all properties with one equality including events etc.
2- I am displaying properties on textboxes. When I edit the textboxes I does not changes the source object.
namespace DisplayObjectsInForm
{
public partial class Form1 : Form
{
public Araba Araba1 = new Araba();
public Araba Araba2 = new Araba();
public Araba Araba3 = new Araba();
public Araba DisplayingAraba = new Araba();
public Form1()
{
InitializeComponent();
Araba1.sName = "Araba1";
Araba1.sColor = "Kirmizi";
Araba1.nModel = 1999;
Araba2.sName = "Araba2";
Araba2.sColor = "Mavi";
Araba2.nModel = 2005;
Araba3.sName = "Araba3";
Araba3.sColor = "Gri";
Araba3.nModel = 2018;
textBox1.DataBindings.Add("Text", DisplayingAraba, "sName");
textBox2.DataBindings.Add("Text", DisplayingAraba, "sColor");
textBox3.DataBindings.Add("Text", DisplayingAraba, "nModel");
}
public class Araba : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
public string sNameInternal;
public string sName
{
get
{
return sNameInternal;
}
set
{
if (sNameInternal != value)
{
sNameInternal = value;
NotifyPropertyChanged("sName");
}
}
}
public string sColorInternal;
public string sColor
{
get
{
return sColorInternal;
}
set
{
if (sColorInternal != value)
{
sColorInternal = value;
NotifyPropertyChanged("sColor");
}
}
}
public int nModelInternal;
public int nModel
{
get
{
return nModelInternal;
}
set
{
if (nModelInternal != value)
{
nModelInternal = value;
NotifyPropertyChanged("nModel");
}
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch(comboBox1.SelectedIndex)
{
case 0:
{
DisplayingAraba.sName = Araba1.sName;
DisplayingAraba.sColor = Araba1.sColor;
DisplayingAraba.nModel = Araba1.nModel;
break;
}
case 1:
{
DisplayingAraba.sName = Araba2.sName;
DisplayingAraba.sColor = Araba2.sColor;
DisplayingAraba.nModel = Araba2.nModel;
break;
}
case 2:
{
//Not working
DisplayingAraba = Araba3;
break;
}
}
}
}
}
It won't work because you initially set DisplayAraba to a newAraba.Try the following
namespace DisplayObjectsInForm
{
public partial class Form1 : Form
{
public Araba Araba1 = new Araba();
public Araba Araba2 = new Araba();
public Araba Araba3 = new Araba();
public Araba DisplayingAraba;//Remove the initialization from here.
public Form1()
{
InitializeComponent();
DisplayingAraba = new Araba(); //Add this line here
Araba1.sName = "Araba1";
Araba1.sColor = "Kirmizi";
Araba1.nModel = 1999;
Araba2.sName = "Araba2";
Araba2.sColor = "Mavi";
Araba2.nModel = 2005;
Araba3.sName = "Araba3";
Araba3.sColor = "Gri";
Araba3.nModel = 2018;

Send Parameters using Frame.Navigate in UWP

A class to temporarily store my input value here.
namespace App2
{
class methC
{
public string tb1 { get; set; }
public string tb2 { get; set; }
public string tb3 { get; set; }
public string tb4 { get; set; }
public string tb5 { get; set; }
public string tb6 { get; set; }
public methC(string tb1, string tb2, string tb3)
{
this.tb1 = tb1;
this.tb2 = tb2;
this.tb3 = tb3;
//this.right1 = right1;
//this.right2 = right2;
//this.right3 = right3;
//this.Box = Box;
//this.Grade = Grade;
}
}
}
first page that take in the parameter
namespace App2
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
methC mc = new methC(tb1.Text, tb2.Text, tb3.Text);
Frame.Navigate(typeof(BlankPage1));
}
}
}
How to get the data stored in tb1,tb2,tb3 to display in BlankPage1?
namespace App2
{
public sealed partial class BlankPage1 : Page
{
public BlankPage1()
{
this.InitializeComponent();
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
string four, five, six;
four = tb4.Text;
five = tb5.Text;
six = tb6.Text;
box.Text = four + five + six;
}
private void TextBlock_SelectionChanged(object sender, RoutedEventArgs e)
{
}
}
}
How's the thing suppose to work, MainPage take in 3 entry, store in methC and get from BlankPage to display tb1,tb2,tb3. How can i make this thing work?
As long as you Navigate to another page using a Type parameter you can't use a constructor to pass the data. so Microsoft provided another way of doing that work.
You can pass your object using this overload of Navigate function:
methC mc = new methC(tb1.Text, tb2.Text, tb3.Text);
string Param = JsonConvert.SerializeObject(mc);
Frame.Navigate(typeof(BlankPage1), Param);
override OnNavigatedTo in the BlankPage1:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
methC myData = JsonConvert.DeserializeObject<methC>((string)e.Parameter);
Labelx.Text = myData.x;
, ....
base.OnNavigatedTo(e);
}
this is from MSDN
and the worst case is to assign them through static members, so in the BlankPage1 class you can add:
public static methC Data { get; set; }
public BlankPage1()
{
mylabel.Text = Data.X;
, ....
}
and you can assign the values before Navigation:
methC mc = new methC(tb1.Text, tb2.Text, tb3.Text);
BlankPage1.Data = mc;
Frame.Navigate(typeof(BlankPage1));
In your mainpage, you can pass the parameter of the class object directly.
methC mc = new methC(tb1.Text, tb2.Text, tb3.Text);
Frame.Navigate(typeof(BlankPage1), mc);
In your next page, you can receive it as a class object
protected override void OnNavigatedTo(NavigationEventArgs e) {
navigationHelper.OnNavigatedTo(e);
methC item = e.Parameter as methC;
}

Why it doesn't maintain reference to the object?

I can't figure out why MessageBox show "false" if nuovo.matrice refers to the same object but not maintain the array reassignment done by the method. Why nuovo.matrice == mat is false if they refers to the same object?
namespace WindowsFormsApplication15
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class Class1
{
public ClassType[] matrice;
public class ClassType
{
public string a { get; set; }
public int b { get; set; }
}
}
Class1.ClassType[] mat;
private void Form1_Load(object sender, EventArgs e)
{
Class1 test = new Class1();
Class1.ClassType prova = new Class1.ClassType();
test.matrice = new Class1.ClassType[1];
test.matrice[0] = prova;
mat = test.matrice;
mat[0].a = "rtuier";
mat[0].b = 94;
Modify nuovo = new Modify(mat);
nuovo.inizia();
MessageBox.Show((nuovo.matrice == mat).ToString());
}
class Modify
{
public Class1.ClassType[] matrice;
public Modify(Class1.ClassType[] mat)
{
matrice = mat;
}
public void inizia()
{
matrice[0].a = "asuidh";
matrice[0].b = 123;
Class1.ClassType[] newMatrice = new Class1.ClassType[2];
Class1.ClassType ins = new Class1.ClassType { a = "pollo", b = 456 };
newMatrice[0] = matrice[0];
newMatrice[1] = ins;
matrice = newMatrice;
}
}
}
}
The problem is, they don't ref the same object.. because you cannot alter the mat variable in the class. You get a copy of a reference and you're altering the copy. If you want to be able to modify a reference, you should wrap it in a class. Then you'll get a copy of the wrapper reference, but the Class1 field is unique.
Class wrap example:
public class ClassType
{
public string a { get; set; }
public int b { get; set; }
}
public class Class1
{
public ClassType[] classType;
}
public class Wrapper
{
public Class1 WrappedClass1;
}
public class Class2
{
public Wrapper Wrapped;
public Class2(Wrapper wrapper)
{
Wrapped = wrapper;
}
public void ChangeClass1()
{
WrappedClass1.WrappedClass1 = new Class1();
}
}
Class1 class1 = new Class1();
Wrapper wrapper = new Wrapper();
wrapper.WrappedClass1 = class1;
Class2 class2 = new Class2(wrapper);
class2.ChangeClass1();
MessageBox.Show(wrapper.WrappedClass1 == class2.Wrapped.WrappedClass1); // <--- true

Getting StackOverflowException in connecting list to listbox

I have created class Auction.cs with this code behind :
namespace WebApplication5
{
public class Auction
{
public string Productname { get; set; }
public string Lastbidder { get; set; }
public int Bidvalue { get; set; }
private List<Auction> listaAukcija = new List<Auction>();
public List<Auction> ListaAukcija
{
get { return listaAukcija; }
set { listaAukcija = value; }
}
public void getAll()
{
using (SqlConnection conn = new SqlConnection(#"data source=JOVAN-PC;database=aukcija_jovan_gajic;integrated security=true;"))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = #"SELECT a.id AS aid, p.name AS pn, u.name AS un, a.lastbid AS alb FROM (Auction a INNER JOIN Product p ON a.productid = p.id) INNER JOIN User u ON a.lastbider = u.id";
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
listaAukcija.Clear();
while (reader.Read())
{
Auction auction = new Auction();
if (reader["aid"] as int? != null)
{
auction.Productname = reader["pn"] as string;
auction.Lastbidder = reader["un"] as string;
auction.Bidvalue = (int)reader["alb"];
}
listaAukcija.Add(auction);
}
}
}
}
public override string ToString()
{
return base.ToString();
}
}
}
I called it's methods in another class called DbBroker.cs :
public class DbBroker : Home
{
Auction aukcija = new Auction();
public void executeQuery()
{
aukcija.getAll();
}
public void getArr()
{
List<string[]> lista = aukcija.ListaAukcija.Cast<string[]>().ToList();
var x = ListBox1.Text;
x = lista.ToString();
}
}
And called getArr on Home page :
public partial class Home : System.Web.UI.Page
{
DbBroker dbb = new DbBroker();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label3.Text = Session["Username"].ToString();
dbb.getArr();
}
}
}
The problem is, I get StackOverflowException error on Auction aukcija = new Auction(); in DbBroker.cs class. I don't know why or how to solve it.
You are creating a list of Auctions objects within itself = Stackoverflow.
This is your problem:
public class Auction {
private List<Auction> listaAukcija = new List<Auction>();
}
You will need to separate the Auction model from the service or repository that gets the data.
For example:
//the model
public class Auction {
public string Productname { get; set; }
public string Lastbidder { get; set; }
public int Bidvalue { get; set; }
public override string ToString()
{
return base.ToString();
}
}
//the service (or can replace this with a repository)
public class AuctionService {
private List<Auction> listaAukcija = new List<Auction>();
public List<Auction> ListaAukcija
{
get { return listaAukcija; }
set { listaAukcija = value; }
}
public void getAll()
{
//get the data and populate the list
}
}
UPDATE
You will need to instantiate the AuctionService in DbBroker. DbBroker does not inherit Home anymore (commented out).
public class DbBroker //: Home <-- circular reference
{
AuctionService auctionService = new AuctionService();
public void executeQuery()
{
auctionService.getAll();
}
public void getArr()
{
string[] lista = auctionService.ListaAukcija.ConvertAll(obj => obj.ToString()).ToArray();
ListBox1.Text = string.Join("\n", lista);
}
}
and on Page_Load() - you did not call executeQuery() function to populate the list.
public partial class Home : System.Web.UI.Page
{
DbBroker dbb = new DbBroker();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label3.Text = Session["Username"].ToString();
dbb.executeQuery(); //populate list.
dbb.getArr(); //convert to string and update textbox
}
}
}
PS. With the new update, AuctionService should actually be the repository, and DbBroker can act as the Service layer. However, this still works for educational purposes.
I hope this helps.

Categories

Resources