Creating a 3 Form C# application. - c#

A project that I received from school has simple instructions: Create a working 3 form C# application. I decided to create a form which will let the user choose from 3 different options (In this case: 1 ticket, 2 tickets, and 3 tickets). Then it will switch to a 2nd form and also let the user choose from 3 options (In this case: 1 bag of popcorn, A large soda, and a bag of chips). The Problem I am having is when it tries to display the total cost its always ends up being 0. I would really appreciate any help.
Code:
Form1:
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 WongGregory9_part3ThreeForms
{
public partial class movieForm : Form
{
public movieForm()
{
InitializeComponent();
}
private void exitButton_Click(object sender, EventArgs e)
{
//Close form
this.Close();
}
private void displayButton_Click(object sender, EventArgs e)
{
//create a variable named snack for snackForm
snackForm snack = new snackForm();
//show the form snack
snack.ShowDialog();
}
}
Form2(snackForm):
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 WongGregory9_part3ThreeForms
{
public partial class snackForm : Form
{
//define varible ticket as 50
int ticket = 50;
//define varible twoTicket as 90
int twoTickets = 90;
//define varible threeTicket as 130
int threeTickets = 130;
//define varible popcorn as 65
int popcorn = 65;
//define varible soda as 30
int soda = 30;
//define varible chips as 40
int chips = 40;
//define varible ticketCost
int ticketCost;
//define varible snackCost
int snackCost;
//define varible totalCost;
int totalCost;
//create a variable named movie for movieForm
movieForm movie = new movieForm();
public snackForm()
{
InitializeComponent();
}
private void snackForm_Load(object sender, EventArgs e)
{
//if ticketRadioButton is checked then..
if (movie.ticketRadioButton.Checked)
{
//ticketCost = ticket
ticketCost = ticket;
}
//if ticketRadioButton2 is checked then..
if (movie.ticketRadioButton2.Checked)
{
//ticketCost = twoTicket
ticketCost = twoTickets;
}
//if ticketRadioButton3 is checked then..
if (movie.ticketRadioButton3.Checked)
{
//ticketCost = threeTicket
ticketCost = threeTickets;
}
//if popcornRadioButton is checked then..
if (popcornRadioButton.Checked)
{
//snackCost = popcorn
snackCost = popcorn;
}
//if sodaRadioButton is checked then..
if (sodaRadioButton.Checked)
{
//snackCost = soda
snackCost = soda;
}
//if chipsRadioButton is checked then..
if (chipsRadioButton.Checked)
{
//snackCost = chips
snackCost = chips;
}
}
private void button1_Click(object sender, EventArgs e)
{
//close form
this.Close();
}
private void displayButton_Click(object sender, EventArgs e)
{
//create a variable named movie for movieForm
displayForm display = new displayForm();
//totalCosts equals ticketCost plus snackCost
totalCost = ticketCost + snackCost;
//display totalCost to displayLabel
display.displayLabel.Text = totalCost.ToString();
//show the dialog entered into displayForm
display.ShowDialog();
}
}
Form3(displayForm):
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 WongGregory9_part3ThreeForms
{
public partial class displayForm : Form
{
public displayForm()
{
InitializeComponent();
}
}

Yup, two issues I can see, both in snackForm:
As per Sergey's comment, you are creating a new instance of movieForm, which doesn't know anything about the data on the original movieForm instance. You need to pass data from the original movieForm to your snackForm, in the same way as you pass the total cost from snackForm to displayForm.
You are checking the radio button selections in snackForm while the form is loading, i.e. before the user has had a chance to click them. Move these checks into snackForm.displayButton_Click

Related

How to use created Instance on all forms / How to make my instance public C# windowsforms

Hello it is probably easy question for you, I'm a beginner and I'm making my own simple game and I want to use a Class:Gamer, which I want to initialize in MainWindow(Form1.cs) from a save file. From then, I want to use it on another Forms aswell, but somehow I can't make the instance go public.
Could you tell me what I'm doing wrong? Or is there another way how to solve this?
Thank you :)
Code on Form1:
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.Drawing.Text;
using System.IO;
namespace THE_GAME
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static Gamer Player;
private void MainWindow_Load(object sender, EventArgs e)
{
//load from savefile lvl;hp;money;gun;armor,name
string allData = File.ReadAllText("../../saveFile/save.txt");
string[] dataFromSave = new string[5];
dataFromSave = allData.Split(';');
Player = new Gamer(dataFromSave[0], dataFromSave[1], dataFromSave[2], dataFromSave[3], dataFromSave[4], dataFromSave[5]);
}
}
}
Code on secondForm2:
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.Drawing.Text;
namespace THE_GAME
{
public partial class Statistics : Form1
{
public Statistics()
{
InitializeComponent();
}
private void Statistics_Load(object sender, EventArgs e)
{
//labels stats
labelName.Text = Form1.Player.GetName();
labelHealth.Text = Form1.Player.GetHealth().ToString();
labelMoney.Text = Form1.Player.GetMoney().ToString();
}
private void buttonBack_Click(object sender, EventArgs e)
{
MainMenu menu = new MainMenu();
menu.Show();
this.Close();
}
}
}
Thank you for your time.
To get at the Gamers Player object from a different Form just do
Form1.Player;
ie
var nam = Form1.Player.Name;
Form1.Player.Die();
etc
PS As I said in a comment - its extremely odd to dereive a form of yours from another one of your forms. Like this
public partial class Statistics : Form1

Select printer name without showing PrintDialog

I have this code. When I need to print my report, it shows me a Print Dialog. I want to select my print name by code, and print my report without showing PrintDialog.
This is my code.
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.Drawing.Printing;
namespace POS.Reports
{
public partial class ProductsReceiptPreview : Form
{
BindingSource _data;
string _total, _cashr, _cashc;
public ProductsReceiptPreview(BindingSource data, string total,
string cashr, string cashc)
{
InitializeComponent();
_total = total; _cashr = cashr; _cashc = cashc;
_data = data;
}
private void ProductsReceipt_Load(object sender, EventArgs e)
{
DataReceiptBindingSource.DataSource = _data;
Microsoft.Reporting.WinForms.ReportParameter[] param = new
Microsoft.Reporting.WinForms.ReportParameter[]
{
new Microsoft.Reporting.WinForms.ReportParameter("ptotal",
_total),
new Microsoft.Reporting.WinForms.ReportParameter("pcashr",
_cashr),
new Microsoft.Reporting.WinForms.ReportParameter("pcashc",
_cashc),
new Microsoft.Reporting.WinForms.ReportParameter
("pdate",DateTime.Now.ToString())
};
this.rptProductsReceipt.LocalReport.SetParameters(param);
this.rptProductsReceipt.RefreshReport();
this.rptProductsReceipt.ZoomMode =
Microsoft.Reporting.WinForms.ZoomMode.PageWidth;
}
private void btnReports_Click(object sender, EventArgs e)
{
rptProductsReceipt.PrintDialog();
}
}
}
i never use your method but i use this
rptProductsReceipt.PrintToPrinter(1, false, 0, 0);
after using adapter and data table

How do I get a string from a TextBox in VS and then compare it to an integer using an if statement?

Alright, I'm pretty new to C# and I'm trying to figure out how I could grab a string or a number from my TextBox in Visual Studio(Windows Form Application) and then figure out if that string is 0.
I've tried doing
if(Calculations.Text == 0)
{
Calculations.Text = 1
}
but to my avail it did not work.
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 WindowsFormsApp1
{
public partial class Calculatrice : Form
{
public Calculatrice()
{
InitializeComponent();
}
private void One_Click(object sender, EventArgs e)
{
if(Calculations.Text)
{
}
}
private void Calculatrice_Load(object sender, EventArgs e)
{
}
}
}
This is all I have right now I'm pretty stuck.
I want to be able to use the if statement to make comparisons with int values.
You should enclose your string in quotes before using them
if(Calculations.Text.Trim () == "0")
{
Calculations.Text = "1";
}
//Example:if Calculations is your textbox id,then
string input = calculations.text.tostring();
//then compare zero with " "
if(input == "0")
{
calculations.text= 1;
}
A user can put anything in a TextBox. Use TryParse which will provide a zero even if it fails (returns false)
private void OPCode()
{
int.TryParse(Calculations.Text, out int i);
if (i == 0)
{
Calculations.Text = 1.ToString();
}
}

How do I make my DataGridView read info of a text file C#

so my issue is that, I can´t make my DataGridView read information from a text file already created, don´t know what to do really, I am kinda new to C#, so I hope you can help me :D
Here is my code to save the values from my grid:
private void buttonGuardar_Click(object sender, EventArgs e)
{
string[] conteudo = new string[dataGridView1.RowCount * dataGridView1.ColumnCount];
int cont = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
conteudo[cont++] = cell.Value.ToString();
}
}
File.WriteAllLines("dados.txt", conteudo);
And now, from a different form, there is another Grid that must be fill with the values saved in that File
The present code:
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.IO;
namespace WindowsFormsApplication31
{
public partial class Form3 : Form
{
DateTime start, end;
private void Form3_Load(object sender, EventArgs e)
{
textBox1.Text = start.ToString("dd-MM-yyyy");
textBox2.Text = end.ToString("dd-MM-yyyy");
}
public Form3(DateTime s, DateTime e)
{
InitializeComponent();
start = s;
end = e;
}
}
}
In conclusion, both of the grid should have 4 Cells:
0-Designação
1-Grupo
2-Valor
3-Data
And the second one from Form3, should read the text file, in the right order
Hope you can help me, Thanks.
Please try this below.
I have exported the grid data in to a text file as same structure as how it appears in grid as below.
private void button1_Click(object sender, EventArgs e)
{
TextWriter writer = new StreamWriter("Text.txt");
for (int i = 0; i < DataGridView1.Rows.Count; i++)
{
for (int j = 0; j < DataGridView1.Columns.Count; j++)
{
writer.Write(DataGridView1.Rows[i].Cells[j].Value.ToString() + "\t");
}
writer.WriteLine("");
}
writer.Close();
}
Created a new class with properties as the column names and a method to load the exported data into a list collection of class as shown below.Here in this example ,my grid has two columns Name and Marks.so i declared those two properties in my user class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace WindowsFormsApplication1
{
public class User
{
public string Name { get; set; }
public string Marks { get; set; }
public static List<User> LoadUserListFromFile(string path)
{
var users = new List<User>();
foreach (var line in File.ReadAllLines(path))
{
var columns = line.Split('\t');
users.Add(new User
{
Name = columns[0],
Marks = columns[1]
});
}
return users;
}
}}
Now on the form load event of second form,call the above method and bind to the second grid as shown below
private void Form2_Load(object sender, EventArgs e)
{
dataGridView2.DataSource = User.LoadUserListFromFile("Text.txt");
}
Pleas mark this as answer,if it helps.

C# - Tier Separation - How to use these delegates?

Here's the relevant code:
ClickMeGame.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary
{
public class ClickMeGame
{
public OnClickMe onClickMeCallback;
public int score;
public ClickMeGame()
{
score = 0;
}
private void IncrementScore()
{
score++;
}
}
}
ClickMeCallBackDefinitions.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary
{
public delegate void OnClickMe();
}
MainWindow.cs (Windows Form)
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 ClassLibrary;
namespace ClickMe
{
public partial class mainWindow : Form
{
private ClickMeGame game;
public mainWindow()
{
InitializeComponent();
game = new ClickMeGame();
game.onClickMeCallback = clickMeButton_Click();
}
private void clickMeButton_Click(object sender, EventArgs e)
{
UpdateUI();
}
private void UpdateUI()
{
scoreLabel.Text = string.Format("The score is: {0}", game.score);
}
}
}
So what I'm trying to do is, when the user clicks a button present on the form, I want a label on the form to update with the game score which increments with every click.
I'm learning about/want to be able to do this with delegates in that I want to separate the project into 2 tiers; Presenation and Logic. I know it's unnecessary to do so, but I'd like to make it such that when you click the button, the Windows Form receives information about the game score via delegates/callback methods. I'm unsure how to do this, but I tried making the callback definition and referencing it, but I'm lost from there.
Assuming that the UI button uses the click event clickMeButton_Click then here you go.
public partial class mainWindow : Form
{
private ClickMeGame game;
public mainWindow()
{
InitializeComponent();
game = new ClickMeGame();
game.onClickMeCallback = param => UpdateUI();
}
private void clickMeButton_Click(object sender, EventArgs e)
{
game.onClickMeCallback.Invoke();
}
private void UpdateUI()
{
scoreLabel.Text = string.Format("The score is: {0}", game.score);
}
}

Categories