Loading / storing the 3 floats (ID=1, Loc1=3, Loc2=100) into the public class MInput works fine. However, I like to access / use the same dataset in the Forms2 class, which unfortunately gives me zero values only. What is wrong with the call in Forms2 for textBox1.text and textBox2.text ? Thanks for your ideas.
namespace WinForms01
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
MInput testDat = new MInput
{
ID = 1F,
Loc1 = 3F,
Loc2 = 100F,
};
{
}
}
namespace WinForms01
{
public class MInput
{
[ColumnName("ID"), LoadColumn(0)]
public float ID { get; set; }
[ColumnName("loc1"), LoadColumn(1)]
public float Loc1 { get; set; }
[ColumnName("loc2"), LoadColumn(2)]
public float Loc2 { get; set; }
[ColumnName("loc3"), LoadColumn(4)]
public float Loc3 { get; set; }
}
}
namespace WinForms01
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MInput testDat = new MInput();
textBox1.Text = Convert.ToString(testDat.ID);
textBox2.Text = Convert.ToString(testDat.Loc1);
}
}
}
If you want Form2 to access an object you create elsewhere you have to pass the object to form2. Some instance of class X doesn't just magically get access to an instance of Y just because both different places do a new Y and call them the same name, for the same reason that you buying an iPhone and your brother buying an iPhone of the same model and specification, doesn't mean you can read his messages
namespace WinForms01
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
MInput testDat = new MInput
{
ID = 1F,
Loc1 = 3F,
Loc2 = 100F,
};
Application.Run(new Form2(testDat));
}
}
}
namespace WinForms01
{
public class MInput
{
[ColumnName("ID"), LoadColumn(0)]
public float ID { get; set; }
[ColumnName("loc1"), LoadColumn(1)]
public float Loc1 { get; set; }
[ColumnName("loc2"), LoadColumn(2)]
public float Loc2 { get; set; }
[ColumnName("loc3"), LoadColumn(4)]
public float Loc3 { get; set; }
}
}
namespace WinForms01
{
public partial class Form2 : Form
{
private MInput _minput;
public Form2(MInput minput)
{
_minput = minput;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = Convert.ToString(_minput.ID);
textBox2.Text = Convert.ToString(_minput.Loc1);
}
}
}
Related
I have the following code, I would like to call the function RefreshProcess(SaveEventTriggerModelArgs obj) from MainWindow_Loaded.
However the problem I am running into due to lack of knowledge working with window apps I calling this method inside.
It will not let me because of the arguments SaveEventTriggerModelArgs obj and if I add those into RefreshProcess, they are different from void MainWindow_Loaded(object sender, RoutedEventArgs e). How to do it?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded+= Window_Loaded;
}
private void RefreshProcess(SaveEventTriggerModelArgs obj)
{
var rect = new Rect();
Dispatcher.Invoke(() =>
{
obj.CurrentEventTriggerModel.ProcessInfo = new ProcessInfo()
{
ProcessName = "Nox" != null ? $"Nox" : "",
Position = rect,
};
});
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
}
public class SaveEventTriggerModelArgs : INotifyEventArgs
{
public Model CurrentEventTriggerModel { get; set; }
}
public class MousePointEventArgs : INotifyEventArgs
{
public ViewModel MousePointViewMode { get; set; }
}
public class ViewModel
{
}
public class Model
{
public ProcessInfo ProcessInfo { get;set;}
}
public class ProcessInfo
{
public string ProcessName { get;set;}
public Rect Position { get;set;}
}
I am new to c#, I am creating a quiz program (I am at the very beginning) and I have generated a class with a question, 4 answers and a correct answer. I have created one question in Public form1, How do I make the correct answer show up in a messagebox when button1 is clicked?
namespace Prog02
{
public class Question
{
public string Que { get; }
public string Ans1 { get; }
public string Ans2 { get; }
public string Ans3 { get; }
public string Ans4 { get; }
public string CorrectAns { get; set; }
public Question(string que, String ans1, String ans2, String ans3, String ans4, String correctans)
{
Que = que;
Ans1 = ans1;
Ans2 = ans2;
Ans3 = ans3;
Ans4 = ans4;
CorrectAns = correctans;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//define question1
Question Question1 = new Question("What number is lowest", "1", "2", "3", "4", "1");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Question1.CorrectAns);
}
}
}
You have created an instance of the Question class directly in the constructor of the form (within Form1, below the method call InitializeComponent). The specified variable Question1 is only valid in this local scope of this specific function. So if you want to access it from outside of this function, you have to make it available in the class. This can be achieved by declaring a field within the class like private Question question; directly before the constructor call and changing your assignment to question = new Question(...);
public partial class Form1 : Form
{
private Question question;
public Form1()
{
InitializeComponent();
this.question = new Question("What number is lowest", "1", "2", "3", "4", "1");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.question.CorrectAns);
}
}
You should move out the question instance and make it a field.
Also there were some other wrong parts with the code.
Here is the full code with the fixes.
Added comments for describing the changes.
using System;
using System.Collections.Generic;
namespace Prog02
{
public class Question
{
// Added "private set" to the properties below
public string Que { get; private set;}
public string Ans1 { get; private set;}
public string Ans2 { get; private set;}
public string Ans3 { get; private set;}
public string Ans4 { get; private set;}
public string CorrectAns { get; private set; }
public Question(string que, String ans1, String ans2, String ans3, String ans4, String correctans)
{
Que = que;
Ans1 = ans1;
Ans2 = ans2;
Ans3 = ans3;
Ans4 = ans4;
CorrectAns = correctans;
}
}
public partial class Form1 : Form
{
// Created a private field for holding the question instance.
private Question question1;
public Form1()
{
InitializeComponent();
//define question1
question1 = new Question("What number is lowest", "1", "2", "3", "4", "1");
}
private void button1_Click(object sender, EventArgs e)
{
// Use the instance here ("question1" instead of "Question1")
MessageBox.Show(question1.CorrectAns);
}
}
}
I want to store Objects of the class Rezept in the list List RezepteListe. After that I want to filter that RezepteListe regarding their NameID. But obviously I dont get the Filter() Method run on my RezepteListe.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public List<Rezept> RezepteListe = new List<Rezept>();
private void button1_Click(object sender, EventArgs e)
{
Rezept Kartoffelsalat = new Rezept("Kartoffelsalat");
textBox1.Text = Kartoffelsalat.NameID;
RezepteListe.Add(Kartoffelsalat);
textBox1.Text = RezepteListe.Where(x => x.NameID == "Kartoffelsalat");
List<String> liste2 = new List<string>();
liste2.Add("Hallo");
textBox1.Text= liste2.Find(x => x == "Hallo");
}
}
public class Rezept
{
public List<string> Zutat { get; set; }
public string NameID { get; set; }
public Rezept(string NameID)
{
this.NameID = NameID;
}
}
I've been tasked with a project where I have to use c# to create forms that digest a list of objects from a file, and is then able to pass the list to another window.
public class Food
{
public string Name;
public string Category;
public int Price;
}
public class Ingredient
{
public string Name;
public string Category;
public decimal PricePerUnit;
public decimal Quantity;
public Ingredient(string pName, string pCategory, decimal pPricePerUnit, decimal pQuantity)
{
Name = pName;
Category = pCategory;
PricePerUnit = pPricePerUnit;
Quantity = pQuantity;
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Ingredient> Inventory = CallInventoryFile();
}
private void inventoryButton_Click(object sender, RoutedEventArgs e)
{
InventoryWindow wnd = new InventoryWindow();
wnd.ShowDialog();
}
public List<Ingredient> CallInventoryFile()
{
List<Ingredient> ProcessedInventory = new List<Ingredient>();
try
{
string[] fileLines = File.ReadAllLines("Inventory.txt");
//Reading in the file
for (int i = 0; i < fileLines.Length; i++)
{
string[] CurrentLine = fileLines[i].Split(',');
string Name = CurrentLine[0].Trim();
string Category = CurrentLine[1].Trim();
decimal PricePerUnit = decimal.Parse(CurrentLine[2].Trim());
decimal Quantity = decimal.Parse(CurrentLine[3].Trim());
Ingredient IngredientToAdd = new Ingredient(Name, Category, PricePerUnit, Quantity);
ProcessedInventory.Add(IngredientToAdd);
}
return ProcessedInventory;
}
catch
{
//if we get here read in failed
MessageBox.Show("There was an error reading in the file");
return ProcessedInventory;
}
}
Which I then have to move onto this window
public InventoryWindow()
{
InitializeComponent();
categoryComboBox.Items.Add("All");
categoryComboBox.Items.Add("Pizza");
categoryComboBox.Items.Add("Burger");
categoryComboBox.Items.Add("Sundry");
categoryComboBox.SelectedValue = "All";
}
private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void categoryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}
My question is how can i pass the results of Inventory from MainWindow to InventoryWindow.
You can just pass inside the constructor ,
InventoryWindow wnd = new InventoryWindow(Inventory);
wnd.ShowDialog();
Then,
public InventoryWindow(List<Ingredient> inputList)
{
}
I'm working on a custom UI for Tracking stocks, although one UI I've made has caused an issue that I can't locate. I keep getting the error: "Inconsistent accessibility: parameter type 'SCM_Addin.Funds.TrackFund[]' is less accessible than method 'SCM_Addin.Forms.frm_FundTracker.frm_FundTracker(SM_Addin.Funds.TrackFund[])'
I've checked the protection in my classes, but I can't find any private variables that would hinder the accessibility in my code. Here is the code:
frm_FundTracker:
namespace SCM_Addin.Forms
{
public partial class frm_FundTracker : Form
{
public frm_FundTracker()
{
InitializeComponent();
}
public frm_FundTracker(String[] fundsToAdd, Double[] ePrices, Double[] cPrices)
{
InitializeComponent();
int index = 0;
foreach (String fund in fundsToAdd)
{
ListViewItem newFundItem = new ListViewItem(fund);
newFundItem.SubItems.Add(ePrices[index].ToString());
newFundItem.SubItems.Add(cPrices[index].ToString());
this.list_Tracker.Items.Add(newFundItem);
index++;
}//END LOADING COLUMNS
}//END FRM_FUNDTRACKER WITH ARRAYS
public frm_FundTracker(TrackFund[] funds)
{
InitializeComponent();
foreach (TrackFund fund in funds)
{
ListViewItem newFundItem = new ListViewItem(fund.symbol);
newFundItem.SubItems.Add(fund.entryPrice.ToString());
newFundItem.SubItems.Add(fund.currentPrice.ToString());
this.list_Tracker.Items.Add(newFundItem);
}
}//END FRM_FUNDTRACKER WITH FUNDS
private void btn_Done_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Close Form?", "Close Form?", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
this.Dispose();
}
}
}
}
Fund Class:
namespace SCM_Addin
{
class Fund
{
public String symbol { get; set; } //Symbol of the fund to be used
private int fundRow { get; set; } //Fund's spot in the Stats Sheet.
private String url1, url2, url3;
private HtmlAgilityPack.HtmlDocument doc;
private DataPuller puller;
private Dictionary<String, String> fundStats;
private SqlConnection conn;
public Fund(String sym)
{
this.symbol = sym;
this.doc = new HtmlAgilityPack.HtmlDocument();
this.puller = new DataPuller();
this.url1 = "http://finance.yahoo.com/q?s=" + this.symbol;
this.url2 = "http://finance.yahoo.com/q/ks?s=" + this.symbol;
this.url3 = "http://www.profitspi.com/stock-quote/" + this.symbol + ".aspx";
this.fundStats = new Dictionary<string, string>();
this.conn = null;
}
TrackFund class (Extends Fund)
namespace SCM_Addin.Funds
{
class TrackFund : Fund
{
public double entryPrice { get; set; }
public double currentPrice { get; set; }
public TrackFund(String sym, double entryP, double curP) : base(sym)
{
this.entryPrice = entryP;
this.currentPrice = curP;
}
}
}
This is my first time really extending a class in C#, so if I'm extending wrong, I guess that could be it.
Default accessibility for a class will be internal, so:
class Fund
//and
class TrackFund : Fund
...should be
public class Fund
//and
public class TrackFund : Fund
Make the TrackFund class public.
public class TrackFund : Fund
{
....
}