C# text from textbox to string array - c#

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 = "";
}

Related

C# Get deleted items and changed items to list

I have a question to get all deleted items to a new list and also for the changed items. I'm using a datagridview with a sortable bindingsource. For all new items it's already working(last part of the code)
Thanks!
namespace Levelapp
{
public partial class LevelView : Form
{
FilterLevel m_filterLevel;
int m_filterLevelTotal;
public LevelView()
{
InitializeComponent();
}
public LevelView(FilterLevel opt)
{
InitializeComponent();
m_filterLevel = opt;
bindingSource1.DataSource = typeof(LevelResource);
dataGridView1.DataSource = bindingSource1;
bindingSource1.DataSource = m_filterLevel.FoundLevels;
m_filterLevelTotal = bindingSource1.Count;
}
private void newSheet_Click(object sender, EventArgs e)
{
string newItemName = "Sheet" + " " + "1";
string newItemNumber = "A-00";
LevelResource newItem = new LevelResource();
newItem.Name = newItemName;
newItem.Number = newItemNumber;
bindingSource1.Add(newItem);
}
private void deleteSheet_Click(object sender, EventArgs e)
{
bindingSource1.RemoveCurrent();
}
private void ok_Click(object sender, EventArgs e)
{
for (int i = m_filterLevelTotal; i < bindingSource1.Count; i++)
{
bindingSource1.Position = i;
LevelResource newSheet = bindingSource1.Current as LevelResource;
}
}
}
}
Thanks for the quick response. But i get an Error at the bool result line. This code will be used in Revit. Below the part how i put your code under the deleteSheet button
private void deleteSheet_Click(object sender, EventArgs e)
{
for (int i = 0; i < bindingSource1.Count; i++)
{
bindingSource1.Position = i;
var view = bindingSource1.Current as DataRowView;
bool result = view.Row.RowState == DataRowState.Added || view.Row.RowState == DataRowState.Unchanged;
if (result)
{
// new or didn't modified, work as normal
}
else
{
// add to another list
}
}
}

Running Total of Ones Count in WinForms

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)
{
}
}
}

How can i create an array that accepts user input from a a multiple textboxes and store it in a single array

namespace DivingScorer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double[] judgesScore;
private void Form1_Load(object sender, EventArgs e)
{
}
private void computeScore_Click(object sender, EventArgs e)
{
double degreeDifficulty;
int scoreBox2;
int scoreBox3;
int scoreBox4;
int scoreBox5;
scoreBox2= Convert.ToInt32(textBox2.Text);
scoreBox3 = Convert.ToInt32(textBox3.Text);
scoreBox4 = Convert.ToInt32(textBox4.Text);
scoreBox5 = Convert.ToInt32(textBox5.Text);
judgesScore[scoreBox2] = Convert.ToDouble(textBox2.Text);
judgesScore[scoreBox3] = Convert.ToDouble(textBox3.Text);
judgesScore[scoreBox4] = Convert.ToDouble(textBox4.Text);
judgesScore[scoreBox5] = Convert.ToDouble(textBox5.Text);
}
}
}
I'm not sure what you plan do with that array. Here is how you create an array with data from textboxes.
double[] judgesScore;
private void computeScore_Click(object sender, EventArgs e)
{
judgesScore = new[]
{
Convert.ToDouble(textBox2.Text),
Convert.ToDouble(textBox3.Text),
Convert.ToDouble(textBox4.Text),
Convert.ToDouble(textBox5.Text)
};
}
I personally like using Generic List, unless I'm implementing very high efficiency algorithm.
IList<double> judgesScore;
private void computeScore_Click(object sender, EventArgs e)
{
judgesScore = new List<double>
{
Convert.ToDouble(textBox2.Text),
Convert.ToDouble(textBox3.Text),
Convert.ToDouble(textBox4.Text),
Convert.ToDouble(textBox5.Text)
};
}

Using int values from arrays - c#

I am making a program that calculates energy usage. I have created a single dimensional integer array that stores the power ratings for each of the appliances on the program. When the user clicks on a checkbox I want to get the 1st element of the array and use it in a calculation.
What code needs to go in the checkbox method?
Also, how do I convert the int values to string so I can print them in a textbox?
public Form1()
{
InitializeComponent();
int[] AppliancePower = new int[3];
AppliancePower[0] = 5000;
AppliancePower[1] = 4000;
AppliancePower[2] = 7000;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
What about
public class Form1
{
private int[] AppliancePower = new[]
{
5000,
4000,
7000
};
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
var value = AppliancePower[0];
DoSomeFanyCalculation(value);
this.textBox1.Text = value.ToString();
}
else
{
this.textBox1.Text = String.Empty;
}
}
}
You can do what the other 2 answer suggested, or you can save a step and just do this:
this.textBox1.Text = (checkBox1.Checked) ? AppliancePower[0].ToString() : string.Empty ;
Also, you need to declare the AppliancePower on the class level, and not in your constructor:
// class level:
int[] AppliancePower;
public Form1()
{
InitializeComponent();
AppliancePower = new int[3];
AppliancePower[0] = 5000;
AppliancePower[1] = 4000;
AppliancePower[2] = 7000;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
this.textBox1.Text = (checkBox1.Checked) ? AppliancePower[0].ToString() : string.Empty ;
}
Isn't it easy.
public class Form1
{
public int[] AppliancePower;
public Form1()
{
InitializeComponent();
AppliancePower = new int[3];
AppliancePower[0] = 5000;
AppliancePower[1] = 4000;
AppliancePower[2] = 7000;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(AppliancePower.Length > 0)
{
int FirstValue = AppliancePower[0];
string StringValue = FirstValue .ToString();
}
}
}

Having problems removing a string from a sting list

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

Categories

Resources