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;
}
Related
I am trying to display an item from a CSV file onto listbox(this part works) and then display individual parts of that item in separate labels.
public partial class InventoryForm : Form
{
public InventoryForm()
{
InitializeComponent();
}
public List<ItemsList> itemsLists(string csvPath)
{
var query = from l in File.ReadAllLines(csvPath)
let data = l.Split('|')
select new ItemsList
{
Name = data[0],
Type = data[1],
DMGTyp = data[2],
DMG = data[3],
Weight = int.Parse(data[4]),
Price = int.Parse(data[5]),
Description = data[5]
};
return query.ToList();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog filePath = new OpenFileDialog();
filePath.ShowDialog();
textBox1.Text = filePath.FileName;
}
private void btnLoad_Click(object sender, EventArgs e)
{
listBox1.DataSource = itemsLists(textBox1.Text);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//to do show individual pieces in labels
}
public class ItemsList
{
public string Name { get; set; }
public string Type { get; set; }
public string DMGTyp { get; set; }
public string DMG { get; set; }
public int Weight { get; set; }
public int Price { get; set; }
public string Description { get; set; }
}
}
The items are broken up into 6 parts and the list box only shows the name of the item but I want the label to show the rest of the item's properties. Any clues on how to do that?
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; }
}
I have a Data class where I store certain values like State, Initials, etc.
I have a get/set for these values.
It's a windows form app, so I made another view sort of like this
public partial class Actions : Form
{
public Actions()
{
InitializeComponent();
}
private void Actions_Load(object sender, EventArgs e)
{
testLabel.Text = ;
}
}
So just as a test case I want to set this labels .Text value to my string from Data which is just like
class Data
{
public string State { get; set; }
public string Initials { get; set; }
public Data()
{
}
}
Data is being set like this from the home class
Data dat = new Data();
dat.State = "IN";
I saw that online the best way to do this is pass it as a value, but I'm not sure of the best way to go about this.
If you need State available on form load event, then best way is passing state value (or your Data object if you need more than just state string) to form's constructor:
public Actions(string state) // or public Actions(Data data)
{
InitializeComponent();
State = state;
}
Then
private void Actions_Load(object sender, EventArgs e)
{
testLabel.Text = State;
}
Is this what you are trying to do?
public partial class Actions : Form
{
private Data myData;
public Actions()
{
myData = new Data();
myData.State = "California";
//the best state :D\\
InitializeComponent();
}
private void Actions_Load(object sender, EventArgs e)
{
testLabel.Text = myData.State;
}
}
EDIT
public partial class Actions : Form
{
private Data myData;
public Actions(Data otherDataObject)
{
myData = otherDataObject;
testLabel.Text = myData.State; //here
InitializeComponent();
}
private void Actions_Load(object sender, EventArgs e)
{
testLabel.Text = myData.State; //or here
}
}
When loading the form, pass your data object to the form and it will be available anywhere within this form
You could add a Data property to the form.
public class Data
{
public string State { get; set; }
}
public partial class Actions : Form
{
public Data Data { get; set; }
private void Actions_Load(object sender, EventArgs e)
{
testLabel.Text = data.State;
}
}
Elsewhere, if you already have a Data object dat
var actionForm = new Actions();
actionForm.Data = dat;
Okay, I've figured it out.
I was doing the right thing initially, but I needed to make my Data class public.
My Code is:
public partial class Actions : Form
{
public Actions(Data data)
{
InitializeComponent();
testLabel.Text = data.State;
}
}
public class Data
{
public string State { get; set; }
public string Initials { get; set; }
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.
i have MedicicineModel (MainViewModel class ) and MedicineData (ItemViewModel class ) . i have three properties defined in MedicineData class as follows :
namespace MedicinePlus.ViewModels
{
public class MedicineData
{
public string ProblemName { get; set; }
public string ProblemDesc { get; set; }
public string ProblemImageFilePath { get; set; }
}
}
MedicineModel class is as follows :
namespace MedicinePlus.ViewModels
{
public class MedicineModel
{
public List<MedicineData> Problems { get; set; }
public MedicineModel()
{
this.Problems = new List<MedicineData>();
}
public bool IsDataLoaded { get; set; }
public void LoadData()
{
//place design time datat here
IsDataLoaded = true;
this.Problems.Add(new MedicineData() { ID = 0, ProblemName = "Fever", ProblemDesc = "rise in body temperature", ProblemImageFilePath = "/Assets/Images/fever.png" });
this.Problems.Add(new MedicineData() { ID = 2, ProblemName = "sprain", ProblemDesc = "Caused due to muscle pull", ProblemImageFilePath = "/Assets/Images/sprain1.png" });
this.Problems.Add(new MedicineData() { ID = 3, ProblemName = "bruise", ProblemDesc = "irritation in the concerned area", ProblemImageFilePath = "/Assets/Images/headache.png" });
}
}
}
MainPage.xaml.cs is as follows :
namespace MedicinePlus
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// Set the data context of the listbox control to the sample data
DataContext = App.ViewModel;
// Sample code to localize the ApplicationBar
BuildLocalizedApplicationBar();
}
// Load data for the ViewModel Items
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
}
private void submitMenu_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
QUERY : now i want to access ProblemName , ProblemDesc and ProblemImgPath in code block of submit_Click event handler . how do i do it
You haven't really supplied enough information for us to answer definitively, so all we can do is to guess. For example, what object is in the App.ViewModel property? Is it a MedicineModel instance? If it is, then you seem to have overlooked that property. Surely you can access your view model and its data using it?:
private void submitMenu_Click(object sender, EventArgs e)
{
MedicineModel viewModel = App.ViewModel;
List<MedicineData> problems = viewModel.Problems;
string problemName = problems[0].ProblemName;
}
If this is not what you're after, then perhaps you'd be good enough to actually tell us in more detail exactly what your want and what problems you are having.
You have to create an instance of MedicineData,
MedicineData medicineData = new medicineData();
and then you can access them as simple as:
medicineData.ProblemName
medicineData.ProblemDesc
and
medicineData.ProblemImageFilePath