Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am working on a mini clicker game, it is not anything big, but i am having a problem with enabling my button, but i can disable it. I am still learning and i think it is okay to ask stupid questions like this. :D
Here 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;
namespace Diamond_Clicker
{
public partial class Form1 : Form
{
private int clicks = 0;
private int counter = 1;
public Form1()
{
InitializeComponent();
}
private void myDiamond_MouseUp(object sender, MouseEventArgs e)
{
myDiamond.Image = Image.FromFile("C:\\Matej Dodevski\\Semos\\C#\\Diamond Clicker\\diamond.png");
}
private void myDiamond_MouseDown(object sender, MouseEventArgs e)
{
myDiamond.Image = Image.FromFile("C:\\Matej Dodevski\\Semos\\C#\\Diamond Clicker\\diamondMouseUp.png");
clicks++;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
clicks++;
}
private void timer1_Tick_1(object sender, EventArgs e)
{
counter++;
clicks = clicks + 1;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
clicks = clicks - 50;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
timer1.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
if (clicks > 5)
{
button1.Enabled = true;
}
else
button1.Enabled = false;
}
}
}
The Load Event is intended to get executed one time, and that's just before the form is displayed on the screen. Usually this event is where you would do some kind of one time initialization.
What you need to do instead is put that code into a function:
private void UpdateButton()
{
if (clicks > 5)
button1.Enabled = true;
else button1.Enabled = false;
// This function can be reduced to one line of code:
// button1.Enabled = clicks > 5;
}
Then you need to call this function at the end of your button1_click function, timer1_tick function, mousedown function and your timer1_tick_1 functions. Basically, into any function where the clicks variable can change.
Related
I am new to the C# scene, so don't really have much knowledge - This is my first project.
I am looking to create a very basic calorie counter, which eventually will include other functions.
Here's what I have so far;
I want to know how to take the value from the text box on the click of the 'Add' button - Which adds to the total value (bottom right)
I'm looking for any tips/videos to help so anything is appreciated.
TIA
I made a simple implementation for you, you could refer to it:
using System;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
bool Flag = Int32.TryParse(textBox1.Text, out int result);//Determine if it is a number
if (textBox1.Text == "")//If it is null, falg takes true and skips the next judgment
{ Flag = true; }
else if (!Flag)
{
MessageBox.Show("Input Error");
textBox1.Text = null;
}
}
private void button1_Click(object sender, EventArgs e)
{
Int32.TryParse(textBox1.Text, out int result);
int total =result + Convert.ToInt32(label3.Text);
label3.Text = total.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
label3.Text = "0";
}
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
private void opc(object sender, EventArgs e)
{
//NOTE: buttons 13-16 share same click event
// needs for each loop
button13.Enabled = false;
button14.Enabled = false;
button15.Enabled = false;
button16.Enabled = false;
}
You can try this.
private void opc(object sender, EventArgs e)
{
for (int i = 13; i <= 16; i++)
{
Control[] buttons = this.Controls.Find(String.Format("button{0}", i), false);
if (buttons.Length > 0)
{
Button btn = (buttons[0] as Button);
btn.Click += btn_Click;
btn.Enabled = true; // or false
}
}
}
void btn_Click(object sender, EventArgs e)
{
MessageBox.Show((sender as Button).Name);
}
See code below :
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button13.Click += new System.EventHandler(this.button_Click);
this.button14.Click += new System.EventHandler(this.button_Click);
this.button15.Click += new System.EventHandler(this.button_Click);
this.button16.Click += new System.EventHandler(this.button_Click);
}
private void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
string name = button.Text;
}
}
}
You want to put the assignmens into a loop? With statically defined names for the buttons that is not possible AFAIK. You should put the buttons into an array or collection in the first place:
Button[] button = new Button[numberOfButtons];
for (int i=0; i<numberOfButtons; i++)
{
button[i] = new Button();
button[i].Click += new System.EventHandler(WhateverYouLike);
// ... some other property assignments, probably also rule-based
}
// ... and at a later time:
for (int i=0; i<numberOfButtons; i++) button[i].Enabled = false;
This question already has answers here:
Best way to implement keyboard shortcuts in a Windows Forms application?
(9 answers)
Closed 6 years ago.
i have a point of sale program made with C#
when pressing submit button it submit the sale and print the invoice
i want to make a shortcut for it so when i press a shortcut key on the keyboard
it does the buttons work
here is my button Code:
private void btnCompleteSalesAndPrint_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Do you want to Complete Sale and Print?\n\n -If you need any item [duplicate] (1 item 2 piece) \n -Please Increase item Quantity \n ----- by clicking + sign ", "Yes or No", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (result == DialogResult.Yes)
{
if (txtPaidAmount.Text == "")
{
MessageBox.Show("Sorry ! you have not enough product \n Please Purchase product or Increase Product Quantity");
// detail_info go = new detail_info();
// go.ShowDialog();
}
else
{
try
{
sales_item();
//Save payment info into sales_payment table
payment_item();
// 5 % Rewards Point add to customer Account for total Payable amount
AddCredit();
PrintPage mkc = new PrintPage();
mkc.saleno = txtInvoice.Text;
mkc.vat = txtVATRate.Text;
mkc.dis = txtDiscountRate.Text;
mkc.paidamt = txtPaidAmount.Text;
mkc.subtotal = lblsubtotal.Text;
mkc.ShowDialog();
showincrement();
ClearForm2();
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
}
}
How about using the KeyDown event on the form? Say for example that the shortcut key is "E".
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.E)
{
btnCompleteSalesAndPrint_Click(sender, new EventArgs());
}
}
However this wouldn't really work with multiple keys, so instead you may want to use a system that allows you to check if a certain key is pressed instead. Say that "CTRL" + "E" is the shortcut. Use the "PresentationCore" library in order to access the "System.Windows.Input" namespace.
using System.Windows.Input;
//[...]
private bool ControlPressed//Returns true if the left control button or the right control button is pressed. Returns false if neither are pressed.
{
get
{
return Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);
}
}
private bool E_Pressed//Boolean that returns true if "E" is pressed and false if it isn't.
{
get
{
return Keyboard.IsKeyDown(Key.E);
}
}
And to check at regular intervals to see if these keys are pressed.
public Form1()
{
InitializeComponent();
Timer timer = new Timer();//Create new instance of Timer.
timer.Interval = 1;
timer.Tick += new EventHandler(timer_Tick);//Set an eventhandler to occur after each 1000th of a second.
timer.Enabled = true;
}
private void timer_Tick(object sender, EventArgs e)
{
if (ControlPressed && E_Pressed)
{
btnCompleteSalesAndPrint_Click(sender, new EventArgs());
}
}
Codf bflow:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Input;
namespace hmmmhmh
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Timer timer = new Timer();
timer.Interval = 1;
timer.Tick += new EventHandler(timer_Tick);
timer.Enabled = true;
}
private void timer_Tick(object sender, EventArgs e)
{
if (Control_Pressed && F_Pressed)
{
button1_Click(sender, e);
}
}
private bool Control_Pressed
{
get
{
return Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);
}
}
private bool F_Pressed
{
get
{
return Keyboard.IsKeyDown(Key.E);
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("hlhlh");
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
" When the user clicks the Add Your Scores menu item, add the score for each type of drink to the drink's total, clear the text boxes, and reset the focus."
I am stuck trying to figure out how to add what is quoted above. I think I am over thinking it and making it harder than what it really is.
Here is what I've done so far:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Lab6
{
public partial class NewTester : Form
{
private int AppleAde;
private decimal TotalScore;
private string Winner_Name = "";
int PrunePunch_Score;
int Total_Num_Of_Tasters;
decimal Average_Rating_Of_Each_Drink;
//private decimal Total_Score;
public NewTester()
{
Thread t = new Thread(new ThreadStart(SplashStart));
t.Start();
Thread.Sleep(5000);
InitializeComponent();
t.Abort();
}
public void SplashStart() {
Application.Run(new Form2());
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Lab6 by J Soto \nThis lab was started on 10/9/13","About Lab 6",MessageBoxButtons.OK);
}
private void addYourScoresToolStripMenuItem_Click(object sender, EventArgs e)
{ // Vars for text box values...
PrunePunch_Score = Convert.ToInt32(PrunePunchTextBox.Text);
AppleAde = Convert.ToInt32(AppleAdeTextBox.Text);
//...
if ((PrunePunch_Score <= 10) && (PrunePunch_Score >= 0))
Convert.ToInt32(PrunePunchTextBox.Text);
else
MessageBox.Show("Please enter a number between 0 and 10","Prune Punch");
if ((AppleAde <= 10) && (PrunePunch_Score >= 0))
Convert.ToInt32(AppleAdeTextBox.Text);
else
MessageBox.Show("Please enter a number betwen 0 and 10","Apple Ade");
//...
}
private void PrunePunchTextBox_TextChanged(object sender, EventArgs e)
{
PrunePunch_Score = Convert.ToInt32(PrunePunchTextBox.Text);
}
private void AppleAdeTextBox_TextChanged(object sender, EventArgs e)
{
AppleAde = Convert.ToInt32(AppleAdeTextBox.Text);
}
private void summaryToolStripMenuItem_Click(object sender, EventArgs e)
{
string Summary;
// Summary= "Winner:"+ Winner_Name "Total Number of Taste Testers:" + Total_Num_Of_Tasters "Average rating for each drink:" + Average_Rating_Of_Each_Drink;
}
}
}
Well, that is a big question. I will help out by pointing out that you are calling Convert without storing the returned value.
You have things like Convert.ToInt32(PrunePunchTextBox.Text); instead of var value = Convert.ToInt32(PrunePunchTextBox.Text);.
Hope that helps!
Do not declare the int at the beginning of your form. simply drag and drop a label which starts at 0. and each score will add it up. convert the label into integer and use += to add value to it.
(P.S. do you have a rar of your project that I can work on?)
I have a WinForm text editor.
I would like to be able to allow the user to undo and redo changes in the Rich Text Box, like they can in Microsoft Word.
I have spent the past week or so researching how to do this, and most results seem to be regarding graphics applications.
The standard richTextBox1.Undo(); gives disappointing results, as it undoes everything that the user has written.
Does anybody have any idea how I could implement effective undo/redo? Preferably one which undoes/redoes the action word-by-word as opposed to character-by-character.
This is a very basic idea, and I'm sure that many improvements could be made.
I would create a String Array and incrementally store the value of the RichTextBox (In the TextChanged event, under your own conditions) in the array. As you store the value, increment the value of a counter, say stackcount. When the user undoes, decrement the stackcount and set the RichTextBox.Text = array(stackcount). If they redo, then increment the value of the counter and set the value again. If they undo and then change the text, then clear all values onwards.
I am sure that many other people may have better suggestions/changes for this, so please post in comments and I will update, or edit it yourself!
Example in C#
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 RedoUndoApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string[] RTBRedoUndo;
public int StackCount = 0;
public int OldLength = 0;
public int ChangeToSave = 5;
public bool IsRedoUndo = false;
private void Form1_Load(object sender, EventArgs e)
{
RTBRedoUndo = new string[10000];
RTBRedoUndo[0] = "";
}
private void undo_Click(object sender, EventArgs e)
{
IsRedoUndo = true;
if (StackCount > 0 && RTBRedoUndo[StackCount - 1] != null)
{
StackCount = StackCount - 1;
richTextBox1.Text = RTBRedoUndo[StackCount];
}
}
private void redo_Click(object sender, EventArgs e)
{
IsRedoUndo = true;
if (StackCount > 0 && RTBRedoUndo[StackCount + 1] != null)
{
StackCount = StackCount + 1;
richTextBox1.Text = RTBRedoUndo[StackCount];
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if (IsRedoUndo == false && richTextBox1.Text.Substring(richTextBox1.Text.Length - 1, 1) == " ")//(Math.Abs(richTextBox1.Text.Length - OldLength) >= ChangeToSave && IsRedoUndo == false)
{
StackCount = StackCount + 1;
RTBRedoUndo[StackCount] = richTextBox1.Text;
OldLength = richTextBox1.Text.Length;
}
}
private void undo_MouseUp(object sender, MouseEventArgs e)
{
IsRedoUndo = false;
}
private void redo_MouseUp(object sender, MouseEventArgs e)
{
IsRedoUndo = false;
}
}
}
One way to do this is use the TextChanged event to periodically store the contents of richtextbox.text in an array or list, as a stack. When you undo, "pop the stack" and copy the most recent version on the stack into richtextbox.text.
TextChange can determine whether a change should be saved onto the stack, whether a new word, line, or character.