I have a project that wants
A method that returns the current count.
A Constructor that sets the count to zero.
I have the first few down but need help with the return count to 0 and then the constructor. I need to do this by adding a counter class but I'm confused about the way to add it.
Can some one help me out?
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 Project10TC
{
public partial class Form1 : Form
{
int zero = 0;
int i = 1;
public Form1()
{
InitializeComponent();
}
private EventHandler myCounter;
// end of Form class
private class myCounter()
{
myCounter = new myCounter( );
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Teancum Clark\nCS 1400\n Project 10");
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = (++i).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = (--i).ToString();
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = (zero).ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
public class Counter
{
public int Value { get; private set; }
public void Increment()
{
Value = Value + 1;
}
public void Decrement()
{
if (Value > 0) Value = Value - 1;
}
public Counter()
{
Value = 0;
}
}
Related
I want to access the latest checkstates and the latest set date value using a function called GetProfileFilter. Now my problem is I'm not able to fetch the latest values when either the checkbox.Checkstate is changed or the datetimepicker is changed.I'm setting the values based on whether the checkboxes are set and I intend to use the IBitWise value elsewhere.Note that at form load all the checkboxes are checked.In the form class I have nothing more than a few variable and the event handlers for buttons and checkboxes and date time pickers.Open to all suggestions!!
class Profile{
public bool GetProfileFilter()
{
if (frmInactive.btnApplyWasClicked == true || frmInactive.btnCancelWasClicked == false)
{
frmInactive.ShowDialog();
MessageBox.Show("Here");
if (frmInactive.chkCancel.Checked == true)
{
MessageBox.Show("Cancel");
IBitWise += Canceled;
}
if (frmInactive.chkDiscon.Checked == true)
{
MessageBox.Show("Discon");
IBitWise += Discontinued;
}
if (frmInactive.chkVoidwoRes.Checked == true)
{
MessageBox.Show("Voidedwo");
IBitWise += VoidedWoutRes;
}
if (frmInactive.chkVoidwRes.Checked == true)
{
MessageBox.Show("Voidedw");
IBitWise += VoidedWRes;
}
MessageBox.Show("Ibit value:" + IBitWise);
return true;
}
else
{
return false;
}
}
}
public partial class FilterView : Form
{
Utilities Utilities = new Utilities();
//FilterView filterView = new FilterView();
public FilterView()
{
InitializeComponent();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox5_CheckedChanged(object sender, EventArgs e)
{
}
private void rsetbtn_Click(object sender, EventArgs e)
{
}
private void aplybtn_Click(object sender, EventArgs e)
{
callonload();
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{
}
Here is sample code with checkbox made public
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Profile profile = new Profile(this);
profile.GetProfileFilter();
}
}
class Profile
{
Form1 form1;
public Profile(Form1 form1)
{
this.form1 = form1;
}
public bool GetProfileFilter()
{
form1.checkBox1.Checked = true;
return true;
}
}
}
So I'm trying to make a window forms guess the number game, simple but when I click guess no matter what the label goes up by one. I think it may be due to my variables as despite having them global userGuess still comes up as a local variable...`
Commenting out userScore removes the problem, but I still do not understand why the logic is failing
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;
namespace Guess_The_Number_Form
{
public partial class Form1 : Form
{
private int userScore;
private int randNum;
private int userGuess;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
txtBoxGuess.Hide()
;
}
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
}
private void btnRandom_Click(object sender, EventArgs e)
{
Random rand = new Random();
int randNum = rand.Next(0, 10);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
userGuess = Convert.ToInt32(txtBoxGuess.Text);
}
private void txtBoxGuess_Enter(object sender, EventArgs e)
{
}
private void btnGuess_Click(object sender, EventArgs e)
{
if (userGuess == randNum)
{
// userScore++;
lbluserScore.Text = userScore.ToString();
lbluserScore.Text = $"{userScore}";
}
else if (userGuess != randNum)
{
userScore--;
lbluserScore.Text = userScore.ToString();
lbluserScore.Text = $"{userScore}";
}
else if (userScore < 0)
{
lbluserScore.Text = Color.Red.ToString();
}
}
}
}
You need to change this:
private void btnRandom_Click(object sender, EventArgs e)
{
Random rand = new Random();
int randNum = rand.Next(0, 10);
}
to
private void btnRandom_Click(object sender, EventArgs e)
{
Random rand = new Random();
randNum = rand.Next(0, 10);
}
That way, you'll set the member variable randNum rather than a local variable randNum. You also want to check that you really are assigning the user-inputted value into userGuess properly. My guess is that it's not, which means you never change the value of either of those variables, and the program thinks the user always guessed the right value.
first of all i want you to know that i know that there a lot of results for this question, but i have searched far and wide still haven't come up with a solution for my problem.
i have tried to do the following:
1.constructor
2.objects
3.properties
4.delegates
but none of my implementation of them really did worked as wanted (in this "solution" i have used properties
when i press "back" on the "pop up" screen i dont in the main screen the value i choose from in the "pop up" screen
basically, it's something like, i have main screen and a "pop up"
the main screen
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 BakaritCV
{
public partial class FrmProdChoose : MetroFramework.Forms.MetroForm
{
CVFeedUtilities utilities = new CVFeedUtilities();
Mixtures mixture;
public string selectedDefault = " 102";
string t;
public FrmProdChoose(string t)
{
InitializeComponent();
this.t = t;
}
public FrmProdChoose()
{
InitializeComponent();
}
private void btnHome_Click(object sender, EventArgs e)
{
FrmMain frmload = new FrmMain();
utilities.moveBetweenScreens(this, frmload);
}
private void mixtureBtn_Click(object sender, EventArgs e)
{
utilities.loadPopUp(this, mixture);
}
private void FrmProdChoose_Load(object sender, EventArgs e)
{
mixture = new Mixtures(this);
mixtureBtn.Text = selectedDefault;
}
public string Selected
{
get { return selectedDefault; }
set { selectedDefault = value; }
}
}
}
the "pop up"
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 BakaritCV
{
public partial class Mixtures : MetroFramework.Forms.MetroForm
{
string[] mixture = new string[] { "102", "103", "104", "105" };
MetroFramework.Controls.MetroTile[] tiles;
FrmProdChoose form;
string selectedDefault;
CVFeedUtilities utilities = new CVFeedUtilities();
public Mixtures(FrmProdChoose form)
{
InitializeComponent();
this.form = form;
}
private void btnHome_Click(object sender, EventArgs e)
{
form.Selected = selectedDefault;
utilities.closePopUp(this, form);
}
private void Mixtures_Load(object sender, EventArgs e)
{
tiles = new MetroFramework.Controls.MetroTile[] { tileOne, tileTwo, tileThree, tileFour};
for (int i = 0; i < mixture.Length; i++)
tiles[i].Text = mixture[i];
}
private void tileOne_Click(object sender, EventArgs e)
{
tileOne.BackColor = Color.ForestGreen;
removeBackColor(1);
}
private void tileTwo_Click(object sender, EventArgs e)
{
tileTwo.BackColor = Color.ForestGreen;
removeBackColor(2);
}
private void tileThree_Click(object sender, EventArgs e)
{
tileThree.BackColor = Color.ForestGreen;
removeBackColor(3);
}
private void tileFour_Click(object sender, EventArgs e)
{
tileFour.BackColor = Color.ForestGreen;
removeBackColor(4);
}
private void tileFive_Click(object sender, EventArgs e)
{
tileFive.BackColor = Color.ForestGreen;
removeBackColor(5);
}
public void removeBackColor(int index)
{
for (int i = 0; i < tiles.Length; i++)
{
if (i == index - 1)
{
selectedDefault = tiles[i].Text;
continue;
}
else tiles[i].BackColor = Color.DeepSkyBlue;
}
}
}
}
and the functions loadPopUp and closePopUp
public void loadPopUp(Form from, Form to)
{
to.Tag = from;
to.Show(from);
}
public void closePopUp(Form from, Form to)
{
to.Tag = from;
if (!to.Visible)
to.Show(from);
from.Hide();
}
I'm trying to create a calculator for an assignment that takes three integer inputs and takes the average and returns that to user. I'm using Visual Basic (it is required) to make the GUI. I'm having trouble with two things, first, I cannot get the aVer to divide by 3 because it is not a integer and second, I do not know how to get an output with the average in the last textbox.
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
private string userInfo1;
private string userInfo2;
private string userInfo3;
private string aVer;
private string num1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int aVer = 0;
aVer = Int32.Parse(userInfo1 + userInfo2 + userInfo3);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int userInfo1 = 0;
userInfo1 = Int32.Parse(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
int userInfo2 = 0;
userInfo2 = Int32.Parse(textBox2.Text);
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
int userInfo3 = 0;
userInfo3 = Int32.Parse(textBox3.Text);
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
int num = Int32.Parse(aVer);
MessageBox.Show(num.ToString());
}
private void label3_Click(object sender, EventArgs e)
{
}
}
}
You are declaring your variables multiple times. The variables inside the function calls will hide the variables you have defined outside in the class declaration, and the class variables will never be set to a value.
It doesn't appear that you are ever using any of the variables in their string form, so all the private string declarations should be changed to private int declarations. This also makes the int userInfo1 = 0; declarations unnecessary. Having them actually breaks the ability to see the values in the button1_Click function.
hey i wrote below code to send my datagridview value seleted row to another form but i got this error my event is double content click and i dont know why this happened
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private void Form5_Load(object sender, EventArgs e)
{
tblClassTableAdapter.Fill(dataSet1.tblClass);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.tblClassTableAdapter.FillBy1(this.dataSet1.tblClass, textBox1.Text);
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
new Form6(int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString())).Show();
}
}
and my form 6
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;
namespace WindowsFormsApplication12
{
public partial class Form6 : Form
{
int classid;
private string p;
public Form6(int myid)
{
classid = myid;
InitializeComponent();
}
public Form6(string p)
{
// TODO: Complete member initialization
this.p = p;
}
public void Form6_Load(object sender, EventArgs e)
{
textBox1.Text = classid.ToString();
}
public DataGridViewRow dataGridViewRow { get; set; }
}
}
thank you guys for helping
DataGridViewCellEventArgs has two important args for you:
e.rowIndex, e.columnIndex which specifying in which cell you pressed.
By the way, you are trying to parse Int from cell, surround it with try/catch for case the parse fails.
try this code instead:
try {
if (e.ColumnIndex > -1 && e.RowIndex > -1)
new Form6(int.Parse(dataGridView1[e.ColumnIndex,e.RowIndex].Value.ToString())).Show();
}
catch (Exception ex) {
MessageBox.Show("Error: " + ex.Message);
}
I think it should help you, mark as answered if yes.