Keeping track of votes in a c# Windows Form app - c#

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.

Related

Accessing buttons outside the form C#

i'm a newbie in C#. My boss has asked me to get co-ordinates of cursor when it's over some button outside the form (e.g on Desktop or any other random Window, like below).
I will then have to access the button properties for some computations (e.g width and height of button or it's name). I'm not sure if it's possible, my boss says it is as he's done something like that in the past. Gave me a hint that it can be done by knowing if the element is clickable and that there's an API for it. Thanks in advance!
Code Below
public partial class Form1 : Form
{
public string label2Y;
public string label1X;
public Form1()
{
InitializeComponent();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
label1.Text = Cursor.Position.X.ToString();
label2.Text = Cursor.Position.Y.ToString();
}
private void Form1_Click(object sender, EventArgs e)
{
label3.Text = Cursor.Position.X.ToString();
label4.Text = Cursor.Position.Y.ToString();
}
}

Keeping track of how many instantiation of a Form is opened or closed

I have an application with three forms. MainForm, QuestionForm, and ViewItemsForm. The MainForm is always displayed. The MainForm contains two buttons and two read-only textboxes. One of the buttons allows the user to open up multiple instance of the QuestionForm which should then display the count of how many are open in one of the textboxes in the MainForm. If one of the QuestionForms is closed, the count inside the textbox should go down to.
I've tried to implement the trigger inside the button that opens the QuestionFrom, and another when the form is closed, but it doesn't seem to work.
public partial class Form1 : Form
{
private void questionFormButton_Click(object sender, EventArgs e)
{
QuestionForm questionFormOpen = new QuestionForm();
questionFormOpen.Show();
}
private void countOfQuestionForm_TextChanged(object sender, EventArgs e)
{
countOfQuestionForm.Text = //Assign the count here
}
}
You can use the Application.OpenForms Property like the following code:
private void questionFormButton_Click(object sender, EventArgs e)
{
QuestionForm questionFormOpen = new QuestionForm();
questionFormOpen.Show();
questionFormOpen.Shown += Fr_Closed;
questionFormOpen.Disposed += Fr_Closed;
}
private void Fr_Closed(object sender, EventArgs e)
{
countOfQuestionForm.Text = Application.OpenForms.OfType<QuestionForm>().Count().ToString();
}

Windows Form 1 Google Search button that opens second Windows Form and loads Page

So here is my setup so you can better understand what I am trying to do. I have the following:
Windows Form 1 (Toolbox) with a textbox for the Google Search and a Search Click button that loads the secondary Windows Form (Search Engine). I am wanting to do the following.
Type the search criteria in my text box and clicking on the Search button which will open the secondary Windows Form (Search Engine) which will load the google search on the (Search Engine) form using "WebBrowser1"
Any assistance would be greatly appreciated. If you can provide code instead of where to go that would be best :)
Updated Code: Which opens the Google Search Engine but does not display on the Webbrowser1. Any additional assistance would be appreciated
Form 1 Search button click Code:
namespace Plumchoice_Toolbox
{
public partial class plumchoice_Form1 : Form
{
public plumchoice_Form1()
{
InitializeComponent();
}
private void plumchoice_Form1_Load(object sender, EventArgs e)
{
}
//Search Engine Form 1 added
//// Search Button that opens the Search Engine
private void button18_Click(object sender, EventArgs e)
{
Google_Search.SearchEngineForm1 frm2 = new Google_Search.SearchEngineForm1();
frm2.searchAddress = "http://www.google.com/webhp?hl=en&tab=ww#hl=en&tbo=d&output=search&sclient=psy-ab&q=" + textBox3.Text.Replace(" ", "+");
frm2.Show();
}
Webbrowser1 Code
namespace Google_Search
{
public partial class SearchEngineForm1 : Form
{
public SearchEngineForm1()
{
InitializeComponent();
}
public string searchAddress;
private void PlumChoiceToolboxForm1_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri(searchAddress);
}
You can do this: In form1 search button click
This code is for Form1 that has a search text box and a button for start searching. I think (but not sure) it is same as your Toolbox form.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Search_Click(object sender, EventArgs e)//When user clicks on search button
{
SearchEngineForm1 frm2 = new SearchEngineForm1();
frm2.searchAddress = "https://www.google.com/search?q=" + txtSearch.Text.Replace(" ", "+");
frm2.Show();
}
}
And below is the Form2 or SearchEngineForm1 that will open after search button click.
public partial class SearchEngineForm1 : Form
{
public SearchEngineForm1()
{
InitializeComponent();
}
public string searchAddress;
private void SearchEngineForm1_Load(object sender, EventArgs e)
{
webBrowser1.Url= new Uri(searchAddress);
}
}
In your code you have a mistake:
private void PlumChoiceToolboxForm1_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri(searchAddress);
}
What is this void? This will never fire. You need to double click on SearchEngineForm1 form (a blank space on form) to add this void to your code:
private void SearchEngineForm1_Load(object sender, EventArgs e)
{
}
Do not forget double click i said. Pasting this only does not help you!
then enter the webBrowser1.Url= new Uri(searchAddress); between its braces {} to have this exactly:
private void SearchEngineForm1_Load(object sender, EventArgs e)
{
webBrowser1.Url= new Uri(searchAddress);
}

Add similar behavior to a group of WinForms controls

I have a form with 6 buttons. These buttons serve to increase/decrease tha value of the respective textbox. Now I'm trying to "animate" the buttons. I want to get another effect on the button when the mouse is over him.
To do that, I have two diferent images in Resources and I am doing this code:
private void btnHoursDown_MouseHover(object sender, EventArgs e) {
btnHoursDown.Image = Game_Helper.Properties.Resources.DownHover;
}
private void btnHoursDown_MouseLeave(object sender, EventArgs e) {
btnHoursDown.Image = Game_Helper.Properties.Resources.Down;
}
This works fine. My question is: it wouldn't be wise to create a class (ButtonBehaviour.cs) and put this code in that class?
So I would have something like this:
ButtonBehaviour buttonBehaviour = new ButtonBehaviour();
private void btnHoursDown_MouseHover(object sender, EventArgs e) {
buttonBehaviour.buttonDownHover();
}
private void btnHoursDown_MouseLeave(object sender, EventArgs e) {
buttonBehaviour.buttonDownLeave();
}
And the class should be:
public class ButtonBehaviour {
public void buttonDownHover() {
// code
}
public void buttonDownLeave() {
// code
}
}
How can I create this Class Behaviour and make the buttons adapt this Behaviour?
if one effect should be applied for all buttons, try to add the same event handlers to them
private void btn_MouseHover(object sender, EventArgs e)
{
(sender as Button).Image = Game_Helper.Properties.Resources.DownHover;
}
private void btn_MouseLeave(object sender, EventArgs e)
{
(sender as Button).Image = Game_Helper.Properties.Resources.Down;
}
button which raised event is available via sender variable
this way you avoid code duplication for every button. creating a ButtonBehaviour or CustomButton is probably an over-engineering unless you need them in many forms

Clone form design and code with button

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.

Categories