i'm making a program where it will count each answer correct on each window and display the counter as a result. I've already figured out how to count the answer and display it on one page.
If anyone could point me in a direction on how to keep that count throughout multiple pages until the last one? Not asking for people to write me code out just don't have a lot of knowledge of carrying data through multiple windows.
Any help/links are appreciated.
Thanks.
EDIT: I took your advice and assigned it to a static variable, this code updates the lblScore to 1, but when it navigates to the last page, it doesn't show that value in the label. Any advice?
public partial class Question1 : Window
{
public Question1()
{
InitializeComponent();
}
private void Question1_Load(object sender, EventArgs e)
{
lblScore.Content = MyGlobals.Score.ToString();
}
private void btnNext1_Click(object sender, RoutedEventArgs e)
{
if (textBox.Text == "1111")
{
MyGlobals.Score = MyGlobals.Score + 1;
lblScore.Content = MyGlobals.Score.ToString();
MessageBox.Show("Noice");
}
new Question3().Show();
this.Hide();
}
}
}
public static class MyGlobals
{
public static int Score;
}
/*Question3 Window*/
namespace Maths_Quiz
{
/// <summary>
/// Interaction logic for Question3.xaml
/// </summary>
public partial class Question3 : Window
{
public Question3()
{
InitializeComponent();
}
private void Question3_Load(object sender, EventArgs e)
{
lblScore.Content = MyGlobals.Score.ToString();
}
private void btnReturn_Click(object sender, RoutedEventArgs e)
{
new MainWindow().Show();
this.Hide();
}
}
}
Create an overload for the constructor and pass in the data as a parameter or store the data in properties settings
Related
hi guys im trying to create a button which can take my information from one textbox to another but i the informationer is in Contactformula.xaml and i need it over to Mainwindow.xaml hope u understand what i mean here is the code...
Mainwindow.xaml.cs
namespace debug
{
public partial class MainWindow : Window
{
public ContactFormula CF = new ContactFormula();
public object Frame { get; private set; }
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ContactFormula win2 = new ContactFormula();
win2.Show();
}
ContactFormula.xaml.cs
namespace debug
{
/// <summary>
/// Interaction logic for ContactFormula.xaml
/// </summary>
public partial class ContactFormula : Window
{
public ContactFormula()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
}
}
and here is a screenshot of the program so maybe u understand me better
Try making the list of contacts a static property in a static class, that way you can get or add contacts from anywhere in the program.
public static class Session
{
public static List<Contact> Contacts = new List<Contact>();
}
you can add contacts from any page using:
Session.Contacts.Add(contact);
I have seen a few links on attempts at this but I haven't found a solution. I am attempting to access my form textbox and update it with text from another class. I can update the text within my DataOrganizerForm class directly but when I pass text back to the DataOrganizerForm class then it doesn't update on the GUI. Here is what I have:
public partial class DataOrganizerForm : Form
{
//Default constructor
public DataOrganizerForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//Handle a Start/Stop button click
private void start_stop_button_Click(object sender, EventArgs e)
{
SerialNumberSearcher snsearch = new SerialNumberSearcher();
snsearch.searchSN();
}
//Allow simple access to update to notification textbox
public void setNotificationText(string text)
{
notification_textbox.Text = text;
}
}
public class SerialNumberSearcher
{
public void searchSN()
{
DataOrganizerForm formAccess = new DataOrganizerForm();
formAccess.setNotificationText("Updated text from different class");
}
}
Well, it won't update the textbox 'cause you're instantiating another object of the DataOrganizerForm class. What you could do is passing a reference of the form object to the SerialNumberSearcher, like this:
public class SerialNumberSearcher
{
private readonly DataOrganizerForm _form;
public SerialNumberSearcher(DataOrganizerForm form)
{
_form = form;
}
public void searchSN()
{
_form.setNotificationText("Updated text from different class");
}
}
You need to understand at which instance you operate. When you use the new-eperator you create a new instance, like a new copy of that type.
DataOrganizerForm formAccess = new DataOrganizerForm();
The original Form is a different instance then the one you create in the searchSN method.
You need to pass that instance into the method to manipulate it:
public void searchSN(DataOrganizerForm formAccess )
{
formAccess.setNotificationText("Updated text from different class");
}
When you want to call this method you need to use this to reference the current object :
private void start_stop_button_Click(object sender, EventArgs e)
{
SerialNumberSearcher snsearch = new SerialNumberSearcher();
snsearch.searchSN(this);
}
this will access the current instance of the Form, thereby allowing you to manipulate the textbox that you are interested in.
When do you use the “this” keyword? might also be helpfull
Thanks for the help. This is what I was able to do to make my application work. I passed the Textbox object by reference to my other class and was able to display my information that way. Also, I had issues getting my text box to continuously update. I had to add
public partial class DataOrganizerForm : Form
{
//Default constructor
public DataOrganizerForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//Handle a Start/Stop button click
private void start_stop_button_Click(object sender, EventArgs e)
{
SerialNumberSearcher snsearch = new SerialNumberSearcher();
snsearch.searchSN(notification_textbox);
}
//Allow simple access to update to notification textbox
public void setNotificationText(string text)
{
notification_textbox.Text = text;
notification_textbox.Update();
}
}
public class SerialNumberSearcher
{
public void searchSN(Textbox notifyTextbox)
{
notifyTextbox.setNotificationText = "Updated text from different class";
notifyTextbox.Update();
}
}
I am trying to create a form that can keep track of a users vote. I have two buttons that a user can select to vote for which selection they like best. I want to have something in the middle that inputs the vote automatically when a selection is made.I'm not sure what tool would work best and what the code should be like?
So far I just have the messages for the votes, but I haven't been able to find what would work best to tally them when the button is selected:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Eyes2_Click(object sender, EventArgs e)
{
MessageBox.Show("Thank you for your vote!");
}
private void Eyes1_Click(object sender, EventArgs e)
{
MessageBox.Show("Thank you for your vote!");
}
}
Sounds like you need two counters (yes/no?), and some kind of control to display the current tallies?
How about starting simple with a Label control?
Label controls can also be used to add descriptive text to a Form to
provide the user with helpful information.
Something like:
public partial class Form1 : Form
{
private int Yes_Tally = 0;
private int No_Tally = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
UpdateTally();
}
private void Eyes1_Click(object sender, EventArgs e)
{
Yes_Tally++;
UpdateTally();
}
private void Eyes2_Click(object sender, EventArgs e)
{
No_Tally++;
UpdateTally();
}
private void UpdateTally()
{
lblTally.Text = String.Format("Yes: {0}, No: {1}", Yes_Tally, No_Tally);
}
}
Declare a counter, increase that counter each time the button is clicked.
Still in the process of learning C#, but I'm a bit confused on something here.
For example, I have a textbox on my form and it has the name of testTXT. Based on the code below, I've created a new class outside of the public partial one that's there by default, and now I'm trying to access testTXT but I cannot. I'm going to also need to access several other textboxes and things later on as well.
Here's a snippet of the code I'm working with thus far:
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void testButton_Click(object sender, EventArgs e)
{
GeneratedClass gc = new GeneratedClass();
gc.CreatePackage("C:\\Users\\user\\Downloads\\output.docx");
}
private void browseButton_Click(object sender, EventArgs e)
{
var fsd = new FolderSelect.FolderSelectDialog();
fsd.Title = "Select folder to save document";
fsd.InitialDirectory = #"c:\";
if (fsd.ShowDialog(IntPtr.Zero))
{
testTXT.Text = fsd.FileName;
}
}
}
public class GeneratedClass
{
**trying to access testTXT right here, but can't.**
}
}
Any help would be greatly appreciated.
You could do this (see other answers), but you really shouldn't.
Nobody but the containing form has to know about the textboxes in it. Who knows, they might disappear, have their name changed, etc. And your GeneratedClass could become a utility class used by lots of forms.
The appropriate way of doing this, is to pass whatever you need from your textbox to your class, like so:
private void testButton_Click(object sender, EventArgs e)
{
GeneratedClass gc = new GeneratedClass();
gc.CreatePackage(this.testTxt.Text);
}
public class GeneratedClass
{
public void CreatePackage(string name) { // DoStuff! }
}
This is because you have your TextBox type defined in Form1 class as private member. Thus can't be access with another class instance
Your question has little to do with C#, more to do with Object Oriented Concepts.
Instance of TextBox has to be given to 'GeneratedClass' somehow.
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void testButton_Click(object sender, EventArgs e)
{
GeneratedClass gc = new GeneratedClass(testTXT);
gc.DoSomething();
gc.CreatePackage("C:\\Users\\user\\Downloads\\output.docx");
}
private void browseButton_Click(object sender, EventArgs e)
{
var fsd = new FolderSelect.FolderSelectDialog();
fsd.Title = "Select folder to save document";
fsd.InitialDirectory = #"c:\";
if (fsd.ShowDialog(IntPtr.Zero))
{
testTXT.Text = fsd.FileName;
}
}
}
public class GeneratedClass
{
TextBox _txt;
public GeneratedClass(TextBox txt)
{
_txt= txt;
}
public void DoSomething()
{
txt.Text = "Changed the text";
}
}
}
You must make testTXT public.
See Protection level (Modifiers) of controls change automaticlly in .Net.
And access to TextBox as
public class GeneratedClass
{
GeneratedClass(Form1 form)
{
form.testTXT.Text = "1";
}
}
i have a really simple counter application, I made in C#.
Now what i want to know is it possible to clone the form design and code, so there are 2 counter's instead of one. with a button.
they both have to be working.
i'm an beginner.. so that's why i ask if this is possible.
So from this (this is what i currently have, without clone button):
http://i.stack.imgur.com/ASMY4.jpg
to this:
http://i.stack.imgur.com/acluZ.jpg
this is my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void counteradd()
{
int current1 = Convert.ToInt32(totaltb.Text);
current1++;
totaltb.Text = Convert.ToString(current1);
}
public void counterreduce()
{
int current2 = Convert.ToInt32(totaltb.Text);
current2--;
totaltb.Text = Convert.ToString(current2);
}
public void counterreset()
{
totaltb.Text = ("0");
}
private void reducebttn_Click(object sender, EventArgs e)
{
counterreduce();
}
private void resetbttn_Click(object sender, EventArgs e)
{
counterreset();
}
private void addbttn_Click(object sender, EventArgs e)
{
counteradd();
}
}
Simply duplicating the controls and laying them out on the form will result in messy code. The "clone' that you are referring to would be to build the functional piece as a user-control, and then add that as a control to your form.