I'm writing a code where we need to find all the ones in a character and display them as a total. However, the code updates the total for each character pressed, rather than adding all previous ones counts.
Help would be appreciated.
namespace ica5_eventdriven
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
listBox1.Enabled = true;
lblKeyCode.Text = e.KeyChar.ToString();
listBox1.Items.Add(Convert.ToString(Convert.ToByte(e.KeyChar), 2));
List<byte> byteList = new List<byte>();
byteList.Add((byte)e.KeyChar);
byte binaryVal = Convert.ToByte(e.KeyChar);
int ones = 0;
int zeros = 0;
foreach (byte val in byteList)
{
for (int i = 0; i < 8; i++)
{
if ((binaryVal & 1) == 1)
{
ones++;
lblOnesCount.Text = ones.ToString();
}
else
zeros++;
binaryVal >>= 1;
}
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Related
I need to calculate the Lb1SumF plus Lb2SumF equal Lb3SumF.
I ran it, and somehow the label3 does not display the expected result.
Here is a screenshot from result.
Here is my code.
private void Form1_Load(object sender, EventArgs e)
{
TX1.TabIndex=0;
}
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb1PriceF.Text) * Convert.ToInt32(TX1.Text);
Lb1SumF.Text = Convert.ToString(sumF); //Label1 sum
}
catch
{
Lb1SumF.Text = "0";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb2PriceF.Text) * Convert.ToInt32(TX2.Text);
Lb2SumF.Text = Convert.ToString(sumF); //Label2 sum
}
catch
{
Lb2SumF.Text = "0";
}
}
private void Lb3_TextChanged(object sender, EventArgs e)
{
int i = Convert.ToInt32(Lb1SumF.Text);
int j = Convert.ToInt32(Lb2SumF.Text);
Lb3.Text = Convert.ToString(i+j); // Label3 sum
}
Lb3_TextChanged might never be invoked as you are not changing the text of the label. I would suggest to change it to a private method and not an event handler. Here is what the code could be like:
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb1PriceF.Text) * Convert.ToInt32(TX1.Text);
Lb1SumF.Text = Convert.ToString(sumF); //Label1 sum
// Call to update sum
UpdateSum();
}
catch
{
Lb1SumF.Text = "0";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb2PriceF.Text) * Convert.ToInt32(TX2.Text);
Lb2SumF.Text = Convert.ToString(sumF); //Label2 sum
// Call to update sum
UpdateSum();
}
catch
{
Lb2SumF.Text = "0";
}
}
// private void Lb3_TextChanged(object sender, EventArgs e)
private void UpdateSum()
{
int sum = 0;
if(!string.IsNullOrEmpty(Lb1SumF.Text) && !string.IsNullOrEmpty(Lb2SumF.Text))
{
sum = Convert.ToInt32(Lb1SumF.Text) + Convert.ToInt32(Lb2SumF.Text);
}
Lb3.Text = Convert.ToString(sum);
}
Replace you code with this it will work, you are using * operator where you have to use + operator, I have commented that lines in your code and replaced it for better understanding.
Happy Coding
namespace WindowsFormsApp8
{
public partial class Form1 : Form
{
private void Lb1SumF_Click(object sender, EventArgs e)
{
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TX1.TabIndex=0;
}
private void label4_Click(object sender, EventArgs e)
{
}
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
//sumF = Convert.ToInt32(Lb1PriceF.Text) * Convert.ToInt32(TX1.Text); // You were doing wrong here , you were multiplying these values
sumF = Convert.ToInt32(Lb1PriceF.Text) + Convert.ToInt32(TX1.Text);
Lb1SumF.Text = Convert.ToString(sumF); //Label1 sum
}
catch
{
Lb1SumF.Text = "0";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
//sumF = Convert.ToInt32(Lb2PriceF.Text) * Convert.ToInt32(TX2.Text); //you are doing it wrong here , you are multiplying
sumF = Convert.ToInt32(Lb2PriceF.Text) + Convert.ToInt32(TX2.Text);
Lb2SumF.Text = Convert.ToString(sumF); //Label2 sum
}
catch
{
Lb2SumF.Text = "0";
}
}
private void Lb3_TextChanged(object sender, EventArgs e)
{
int i = Convert.ToInt32(Lb1SumF.Text);
int j = Convert.ToInt32(Lb2SumF.Text);
Lb3.Text = Convert.ToString(i+j); // Label3 sum
}
private void Lb3SumF_Click(object sender, EventArgs e)
{
}
}
}
I'm working on a spambot but there is 1 problem I can't solve. I made buttons you can press, it says how much you want to spam. If you want to spam you will get a certain amount of time to go to the place where you want to spam. I made a function for how much the text will spam. But the problem is when it send 1 message it wait the certain amount of time time and not the 500 miliseconds
The script is written in C#. The target framework is: .NET Framework 4.6.1.
public partial class Form1 : Form
{
public void Time()
{
for (int i = 0; i <= 10;)
{
Stuur();
i++;
}
} // 10x
public void Stuur() // does the sending
{
System.Threading.Thread.Sleep(500);
SendKeys.Send(textBox3.Text);
SendKeys.Send("{ENTER}");
}
public Form1()
{
InitializeComponent();
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Rush B", "5 sec voor spam",
MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Threading.Thread.Sleep(5000);
Time();
}
private void button2_Click(object sender, EventArgs e) // the credit
block
{
if (textBox6.Visible == true)
{
textBox6.Visible = false;
} else {
textBox6.Visible = true;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
}
Maybe something along these lines?
public void Stuur() // does the sending
{
System.Threading.Thread.Sleep(5000);
SendKeys.Send(textBox3.Text);
SendKeys.Send("{ENTER}");
}
public Form1()
{
InitializeComponent();
}
private void textBox4_TextChanged(object sender, EventArgs e) { }
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i <= Int32.Parse(textBox.Text4); i++) {
Stuur();
}
}
private void button2_Click(object sender, EventArgs e) // the credit block
{
if (textBox6.Visible == true)
{
textBox6.Visible = false;
}
else
{
textBox6.Visible = true;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e) { }
}
edit: closed parenthesis
I want to add text from textbox to string[] array by pressing button3 i value will add to new array and clear textbox.
Button1 is to go up on array and Button2 is for go down.
I make this but it wont work:
namespace Test
{
public partial class Form1 : Form
{
public int arr1 = 0;
public int arr2 = 0;
public string[] array = new string[100];
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (arr1 < array.Length - 1)
{
if (array[arr1] != "")
{
arr1++;
textBox1.Text = array[arr1];
}
}
else
{
arr1 = 0;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (arr1 < array.Length - 1)
{
if (array[arr1] != "")
{
arr1--;
textBox1.Text = array[arr1];
}
}
else
{
arr1 = 0;
}
}
private void button3_Click(object sender, EventArgs e)
{
array[arr1] = textBox1.Text;
listBox1.Items.Add(array[arr1]);
arr2++;
textBox1.Text = "";
}
}
}
Can someone help me with this?
Thanks
replace arr2++ by arr1++ as following
private void button3_Click(object sender, EventArgs e)
{
array[arr1] = textBox1.Text;
listBox1.Items.Add(array[arr1]);
arr1++;
textBox1.Text = "";
}
I want to remove an item from a list...
I am obviously missing something...I have tried just about every variation including EXCEPT, REMOVE, etc...
When debugging, I step through each ling, but when it gets to btnRemove_Click, it steps through removing but does not remove anything...it acts as if I never sent a command to remove anything???
Help!
public partial class frmUpdate : Form
{
private Student student = new Student();
private string _scores;
public frmUpdate()
{
InitializeComponent();
}
public string GetUpdatedScores(Student s)
{
txtName.Text = s.Name;
_scores = s.Scores;
FillStudentGrades();
this.ShowDialog();
return _scores;
}
private void FillStudentGrades()
{
lstScores.Items.Clear();
string[] grades = splitGrades(_scores);
foreach (string s in grades)
{
lstScores.Items.Add(s.ToString());
}
}
private void lstScores_SelectedIndexChanged(object sender, EventArgs e)
{
int i = lstScores.SelectedIndex;
}
private void btnAdd_Click(object sender, EventArgs e)
{
frmAddScore addScore = new frmAddScore();
_scores += " " + addScore.AddScore();
FillStudentGrades();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
int i = lstScores.SelectedIndex;
}
private void btnRemove_Click(object sender, EventArgs e)
{
int i = lstScores.SelectedIndex;
}
private void btnRemove_Click(object sender, EventArgs e)
{
if (lstScores.SelectedIndex < 0)
{
MessageBox.Show("You Must Select A Grade.");
btnUpdate.Focus();
}
else
{
int i = lstScores.SelectedIndex;
string[] grades = splitGrades(_scores);
string message = "Are you sure you want to remove " + grades[i].ToString() + "?";
DialogResult button = MessageBox.Show(message, "Confirm Remove",
MessageBoxButtons.YesNo);
if (button == DialogResult.Yes)
{
int count = 0;
foreach (char c in grades[i])
{
if (char.IsDigit(c))
{
count++;
}
}
int a = _scores.IndexOf(grades[i].ToString());
_scores = _scores.Remove(a, (count + 1));
FillStudentGrades();
btnOk.Focus();
}
else
{
btnOk.Focus();
}
}
}
private void btnClearAll_Click(object sender, EventArgs e)
{
}
private void btnOk_Click(object sender, EventArgs e)
{
student.Name = txtName.Text;
student.Scores = _scores;
this.Close();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
public string[] splitGrades(string s)
{
string[] grades = s.Split(' ');
return grades;
}
}
In C#, strings are immutable. _scores.Remove(i); doesn't change _scores. Instead it returns a new string object that you can assign to a variable, for example, back to _scores like this:
_scores = _scores.Remove(i);
you need to use RemoveAt methos - as you are trying to remove index and not the value
I'm new to c#, and after looking through the other threads on this subject I'm still at a loss of how to fix the bug:C.
I'm trying to make a simple calculator and here is the code for that:
//Global Variables
string sign;
double val1;
double val2;
int trackkeypoint = 0;
public void Calculator()
{
InitializeComponent();
}
private void cmd0_Click(object sender, EventArgs e)
{
txtbox.Text = txtbox.Text + cmd0.Text;
}
private void cmd1_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd1.Text;
}
private void cmd2_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd2.Text;
}
private void cmd3_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd3.Text;
}
private void cmd4_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd4.Text;
}
private void cmd5_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd5.Text;
}
private void cmd6_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd6.Text;
}
private void cmd7_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd7.Text;
}
private void cmd8_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd8.Text;
}
private void cmd9_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd9.Text;
}
private void cmdequal_Click(object sender, EventArgs e)
{
val2 = double.Parse(txtbox.Text);
double result;
if(sign=="+")
{
result = val1 + val2;
txtbox.Text = result.ToString();
}
else if(sign=="-")
{
result = val1 - val2;
txtbox.Text = result.ToString();
}
else if(sign=="X")
{
result = val1 * val2;
txtbox.Text = result.ToString();
}
else if(sign=="/")
{
result = val1 / val2;
txtbox.Text = result.ToString();
}
}
private void cmdclear_Click(object sender, EventArgs e)
{
//Clears text
txtbox.Text = "";
val1 = 0;
val2 = 0;
sign = "";
}
private void cmdplus_Click(object sender, EventArgs e)
{
sign = "+";
val1 = double.Parse(txtbox.Text);
txtbox.Text = "";
}
private void cmdsubtract_Click(object sender, EventArgs e)
{
sign = "-";
val1 = double.Parse(txtbox.Text);
txtbox.Text = "";
}
private void cmdmultiply_Click(object sender, EventArgs e)
{
sign = "X";
val1 = double.Parse(txtbox.Text);
txtbox.Text = "";
}
private void cmddivide_Click(object sender, EventArgs e)
{
sign = "/";
val1 = double.Parse(txtbox.Text);
txtbox.Text = "";
}
private void cmdsqrt_Click(object sender, EventArgs e)
{
double v;
v = double.Parse(txtbox.Text);
txtbox.Text = Math.Sqrt(v).ToString();
}
private void cmdsquare_Click(object sender, EventArgs e)
{
double v;
v = double.Parse(txtbox.Text);
txtbox.Text = Math.Pow(v,2).ToString();
}
private void cmdsin_Click(object sender, EventArgs e)
{
double v;
v = double.Parse(txtbox.Text);
txtbox.Text = Math.Sin(v).ToString();
}
private void cmdcos_Click(object sender, EventArgs e)
{
double v;
v = double.Parse(txtbox.Text);
txtbox.Text = Math.Cos(v).ToString();
}
private void cmdtan_Click(object sender, EventArgs e)
{
double v;
v = double.Parse(txtbox.Text);
txtbox.Text = Math.Tan(v).ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void txtbox_TextChanged(object sender, EventArgs e)
{
}
private void txtbox_KeyPress(object sender, KeyPressEventArgs e)
{
int keycode;
keycode = e.KeyChar;
//accept only number from key 0 to key 9, key back, and key dot
if (keycode >= 48 && keycode <= 57 || keycode==8 || keycode==32 || keycode==46)
{
//key dot allowed only one time
if (keycode == 46) ++trackkeypoint;
if (trackkeypoint > 1) { e.Handled = true; --trackkeypoint; }
}
else e.Handled = true;
}
private void txtbox_KeyDown(object sender, KeyEventArgs e)
{
}
}
}
and I get this error:
I tried changing that to CWindowsGUI and that didn't work, or removing the offending bits, or a host of other layman fixes. It also shows up in the designer window:
The namespace is the same on the CWindowsGUI.Designer.cs as the actual code thing
Looking at the class you provided I think you renamed your Form1 to Calculator
So try:
Application.Run(new Calculator());
Edit:
CWindowsGUI.cs
public partial class Calculator : Form
{
public Calculator() // Not public void Calculator()
{
InitializeComponent();
}
CWindowsGUI.Designer.cs
public partial class Calculator
First you need to fix some errors
private void Form1_Load(object sender, EventArgs e)
{
}
you do not have any form named as Form1 it seems that when you created application it created a form for you named as Form1 you just renamed it from solution explorer.
delete this load event.
public void Calculator()
{
InitializeComponent();
}
is it a constructor of your form CWindowsGUI
constructor name should be same as form name correct it.
Do You wants to run CWindowsGUI form first ???
Application.Run();
method defines that which form you wants to set as startup that's all
then try this :
Application.Run(new CWindowsGUI());
instead of
Application.Run(new Calculator());
my suggestion would be that delete this form(CWindowsGUI) and create a new form then code on that page also donot forget to change the form in
Application.Run(new CWindowsGUI());