I have a problem in a same namespace:
public partial class frmForm1 : Form // Form1
{
public class Account
{
public string Username;
public string Password;
}
public class ListAcc
{
public static int count = 0;
private static List<Account> UserList;
public static List<Account> Data()
{
return UserList;
}
}
}
public partial class frmForm2 : Form // Form2
{
private void button2_Click(object sender, EventArgs e)
{
frmForm1.Account A;
string m = frmForm1.ListAcc<A>.[0].Username; //ERROR
}
}
How could i access the attributes (Username, Password...) in frmForm1? Someone help me? Thanks!
string m = frmForm1.ListAcc.Data()[0].Username
But, you must have first element in your username list.
Complete Source code:
public class Account
{
public string Username;
public string Password;
}
public class ListAcc
{
public static int count = 0;
private static List<Account> UserList;
public static List<Account> Data()
{
return UserList;
}
ListAcc()
{
UserList = new List<Account>();
UserList.Add(new Account() { Username = "x", Password = "y" });
}
}
public partial class frmForm1 : Form // Form1
{
public static ListAcc;
}
public partial class frmForm2 : Form // Form2
{
private void button2_Click(object sender, EventArgs e)
{
string m = frmForm1.ListAcc.Data()[0].Username;
}
}
Related
I'm currently trying to send customer names from one listbox in form1 to another listbox in a separate form (form2). I want to do this by when the user clicks the add button the customer name it will automatically send it to the other form without the second form opening. Is this possible?
Also currently if I exit from form1 and go back into it without closing the program all the input in the listbox is lost, is it possible to save the input in there some how?
Form 1:
public partial class Form1 : Form
{
// Creates a controller object
Controller control = new Controller();
public Form1()
{
InitializeComponent();
DisplayCustomers();
// FormAccounts formA = new FormAccounts(listBoxCustomer);
}
// Add Customer
// Completed
private void buttonAddCustomer_Click(object sender, EventArgs e)
{
Customer c = new Customer();
c.firstName = textBox1.Text;
control.Add(c);
// control.list.Add(c);
listBoxCustomer.Items.Add(c.firstName);
}
// Displays Customers to list box
public void DisplayCustomers()
{
listBoxCustomer.Items.Clear();
foreach(Customer c in control.list)
{
listBoxCustomer.Items.Add(c.FirstName);
}
}
}
}
Form 2:
public partial class Form2 : Form
{
Controller control = new Controller();
public FormAccounts()
{
InitializeComponent();
DisplayCustomers();
}
// Displays Customers to list box
public void DisplayCustomers()
{
listBoxCustomerName.Items.Clear();
foreach (Customer c in control.list)
{
listBoxCustomerName.Items.Add(c.FirstName);
}
}
}
Controller class:
class Controller
{
public List<Customer> list = new List<Customer>();
// Method to create customer object
public void CreateCustomer(string firstName)
{
list.Add(new Customer(firstName));
}
// Add Customer
public void Add(Customer c)
{
list.Add(c);
}
}
Customer class:
class Customer
{
private List<Account> customerAccounts;
private static int nextID = 1;
public int customerId;
public string firstName;
public int CustomerId { get => customerId; set => customerId = value; }
public string FirstName { get => firstName; set => firstName = value; }
public Customer()
{
CustomerId = nextID;
nextID++;
}
public Customer(string newFirstName) : this()
{
FirstName = newFirstName;
}
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 am using Windows Form;
I want to set TextBox tbCommandName1.Text which is on my Form;
I got the value from GetInfo() and how can I send string from Second.GetInfo() to my main Form like to class Favorit ?
I don't want to create an instance of Form; Because it makes to Initialize all my components again.
I bet I have to use get/set.
Give me please, a few hints;
THanks a lot to you, my guru!
namespace ParserFavorit
{
public partial class Favorit : Form, IForm
{
public Favorit()
{
InitializeComponent();
}
public string CommandName1
{
get { return tbCommandName1.Text; }
set { tbCommandName1.Text = value; }
}
private void bStart_Click(object sender, EventArgs e)
{
string ID = tbGetID.Text;
Second.StartBrowser(Second.GetDriver(), ID);
}
}
public class Second : Form
{
private static ChromeDriver driver = null;
public static ChromeDriver GetDriver()
{
if (driver == null)
{
driver = new ChromeDriver();
}
return driver;
}
public static void StartBrowser(ChromeDriver driver, string IDevent)
{
driver.Navigate().GoToUrl("myURL" + IDevent);
GetInfo();
}
public static void GetInfo()
{
System.Threading.Thread.Sleep(2000);
string CommandName1 = driver.FindElement(By.XPath(".//*[#id='react-root']/div/div[2]/div/div[1]/div/div[1]/div/div[1]/div/header/div[2]/span[1]")).Text;
string CommandName2 = driver.FindElement(By.XPath(".//*[#id='react-root']/div/div[2]/div/div[1]/div/div[1]/div/div[1]/div/header/div[2]/span[2]")).GetAttribute("innerHTML");
}
}
interface IForm
{
string CommandName1 { get; set; }
}
}
I am sure that I have a TextBox with name tbCommandName1;
My form doesn't show the value which I got from website, but method PrintName got it.
How so?
namespace ParserFavorit
{
public partial class Favorit : Form
{
public Favorit()
{
InitializeComponent();
}
private void bStart_Click(object sender, EventArgs e)
{
string ID = tbGetID.Text;
Second.StartBrowser(Second.GetDriver(), ID);
}
public void PrintName(string Command1Name)
{
string Name = Command1Name;
tbCommandName1.Text = Name;
}
}
public class Second
{
private static ChromeDriver driver = null;
public static ChromeDriver GetDriver()
{
if (driver == null)
{
driver = new ChromeDriver();
}
return driver;
}
public static void StartBrowser(ChromeDriver driver, string ID)
{
driver.Navigate().GoToUrl("https://m.favorit.com.ua/uk/live/events/" + ID);
GetInfo();
}
public static void GetInfo()
{
System.Threading.Thread.Sleep(2000);
string CommandName1 = driver.FindElement(By.XPath(".//*[#id='react-root']/div/div[2]/div/div[1]/div/div[1]/div/div[1]/div/header/div[2]/span[1]")).Text;
Favorit favorit = new Favorit();
favorit.PrintName(CommandName1);
}
}
}
I would like to use MVP Design pattern for a WinForm App but i'm facing the problem of calling a View Update from another thread.
Here's my code
MODEL
public class Model : IModel
{
public string Status { get; set; }
public async void LongOperation(IHomeView View)
{
for (int i = 0; i < 1000; i++)
{
View.StatusListView = i.ToString();
}
}
}
PRESENTER
public class HomePresenter
{
IHomeView _IView;
IModel _IModel;
Model _Model = new Model();
public HomePresenter(IHomeView IView)
{
_IView = IView;
}
public async void LaunchLongOperation()
{
await Task.Run(() => _Model.LongOperation(_IView));
}
}
INTERFACE VIEW-PRESENTER
public interface IHomeView
{
string StatusListView { get; set; }
}
INTERFACE PRESENTER-MODEL
public interface IModel
{
string Status { get; set; }
}
FORM:
public partial class frmMain : Form, IHomeView
{
HomePresenter _Presenter;
public frmMain()
{
InitializeComponent();
_Presenter = new HomePresenter(this);
}
public string StatusListView
{
get
{
return lstActivityLog.Text;
}
set
{
lstActivityLog.Items.Add(value);
}
}
private void btnAvvia_Click(object sender, EventArgs e)
{
_Presenter.launchLongOperation();
}
}
i would like to update a list view in the Main form during the long operations of the Model class.
Which is the best way to do that?
Try this code without debugging, you'll be surprised about it works!
The quick and dirty way to make it work in debugging mode as well is to add Control.CheckForIllegalCrossThreadCalls = false; into the constructor of your form.
public partial class MainForm : Form, IHomeView
{
HomePresenter _Presenter;
public MainForm()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false; //<-- add this
_Presenter = new HomePresenter(this);
}
public string StatusListView
{
get
{
return lstActivityLog.Text;
}
set
{
lstActivityLog.Items.Add(value);
}
}
private void button1_Click(object sender, EventArgs e)
{
_Presenter.LaunchLongOperation();
}
}
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)
{
}