Hi i am having a problem here. So you can see whenever the txtBtn0 and txtBtn1 is clicked it increments its own array which is then used for squareChecked string. But what I want to do first is to give out an error message if txtBtn0 nor txtBtn1 is not clicked. But it does not pop up anything.
public partial class MainForm : Form
{
public int[] clickNumBoxArray = Enumerable.Repeat(1, 81).ToArray();
public MainForm()
{
InitializeComponent();
} ... ... ...
private void btn1_Click(object sender, EventArgs e) {
UserSquare checkClickedBox = new UserSquare();
string checkClickBox = checkClickedBox.squareChecked();
if (checkClickedBox == null) {
MessageBox.Show("You did not enter any text on the form");
}
}
private void txtBtn1_Click(object sender, EventArgs e) {
clickNumBoxArray[1]++;
if (clickNumBoxArray[1] % 2 == 0) {
txtBtn1.BackColor = System.Drawing.Color.DarkOrange;
} else {
txtBtn1.BackColor = System.Drawing.Color.WhiteSmoke;
}
}
private void txtBtn0_Click(object sender, EventArgs e) {
clickNumBoxArray[0]++;
if (clickNumBoxArray[0] % 2 == 0) {
txtBtn0.BackColor = System.Drawing.Color.DarkOrange;
} else {
txtBtn0.BackColor = System.Drawing.Color.WhiteSmoke;
}
}
This is the other class
class UserSquare {
public string squareChecked() {
string clickedBoxes = null;
MainForm numBoxArray = new MainForm();
int[] clickNumBoxArray = numBoxArray.clickNumBoxArray;
for (int i = 0; i < 81; i++) {
if (clickNumBoxArray[i] % 2 == 0) {
clickedBoxes += "txtBtn" + i + ", ";
}
} return clickedBoxes;
}
The line:
for (int i = 0; i < 81; i++)
should be:
for (int i = 0; i < 80; i++)
The array clickNumBoxArray only had 80 elements, indices 0 to 79. You're looping through 81 items, indices 0 to 80.
About IndexOutOfRange Exception Your problem is that you implement your list here
public int[] clickNumBoxArray = Enumerable.Repeat(1, 80).ToArray();
as starts from 1 and finished 80 = 80 elements
but in method your for loop starts from 0 and finished 81 = 81 elements
and about controlling by error.. simply and tricky.. you can implement an internal / public boolean as default false and set to true in a mouse event as you need (i.e.mouseclick event)..end of your error method or where you need else, set to back false to be ready for another click-event controlling
About ButtonClick not fire its event (if im not misunderstood) : Did you deleted your some methods of buttons ? could you been forgot to implement it back ?
open your design mode click once onto your controls which you want to check, and then open properties => events.. if your events implemented then it should be as so : (i.e.)
(property) MouseClick (value) Button1_MouseClick
I fixed it by moving
public int[] clickNumBoxArray = Enumerable.Repeat(1, 81).ToArray();
to UserSquare class. I figured it out that UserSquare class only gets the value when program is run but not updating.
Related
I am currently working on a homework assignment that I am struggling with. The goal of the program is to:
Create a program that allows users to record and view names of up to 1000 events. The names of the events are to be stored in an array. The names can be stored and viewed sequentially only: I,e, the first time the user presses “Set” to store a name, it would be placed in index 0 of the array; the second time the user stores a name it would be stored in index 1, etc. Similarly, the first time the user presses “View”, the item found in index 0 of the array is shown to the user; the second time the user presses “View”, the item found in index 1 of the array is shown, etc.
When the user presses the “Set” button (btnSet), an event name is inserted into the array in the next free index, sequentially as described above. The name to insert to the array is taken from txtEventName.
When the user presses the button “View” (btnView), the name of the next event to view sequentially (as described above) is shown to the user. The event name is shown to the user in txtName.
I currently have the code:
namespace ArrayHW
{
public partial class Form1 : Form
{
int[] eventsArray;
const int SIZE = 1000;
public Form1()
{
InitializeComponent();
}
private void btnSet_Click(object sender, EventArgs e)
{
eventsArray = new int[SIZE];
for (int index = 0; index < eventsArray.Length - 1; index++)
{
eventsArray[index] = Convert.ToInt32(txtEventName.Text);
}
}
private void btnView_Click(object sender, EventArgs e)
{
for (int index = 0; index < eventsArray.Length- 1; index++)
{
Debug.WriteLine(eventsArray[index]);
txtName.Text = Convert.ToString(eventsArray[index]);
}
}
}
}
When I run the form, I only get the result for index = 0,1, 2, 3, etc or whatever I had just input into the array in
private void btnSet_Click(object sender, EventArgs e)
{
eventsArray = new int[SIZE];
for (int index = 0; index < eventsArray.Length - 1; index++)
{
eventsArray[index] = Convert.ToInt32(txtEventName.Text);
}
}
rather than it showing up in sequential order like it is supposed to. Could anyone show me a better way to approach this problem, or help me find out what I am doing wrong? Thank you very much.
Please read the comments in the code block. Hopefully that will help you resolve your problem.
public partial class Form1 : Form
{
const int SIZE = 1000;
int[] eventsArray = new int[SIZE];
//as per your requirement, you would need these
//to display and set items at proper index in the array.
int _setIndex = 0;
int _viewIndex = 0;
public Form1()
{
InitializeComponent();
}
private void btnSet_Click(object sender, EventArgs e)
{
//this is where your problem is. Every time the Set btn is clicked,
//you are creating a new array. Therefore you are only seeing what you added in the
//last click. Anything that was added to the array on the prior clicks are lost because
//you just created a new array with the line below.
//eventsArray = new int[SIZE];
//You would need to comment the line above because this is code block is being
//executed on every btnSet click. New up this array only once by moving this to global scope.
//for (int index = 0; index < eventsArray.Length - 1; index++)
//{
// eventsArray[index] = Convert.ToInt32(txtEventName.Text);
//}
//Since we have created fields to keep track of _setIndex, all we need to do is:
if (_setIndex < SIZE)
{
eventsArray[_setIndex] = Convert.ToInt32(txtEventName.Text);
_setIndex++;
}
}
private void btnView_Click(object sender, EventArgs e)
{
//this wont be necessary.
//for (int index = 0; index < eventsArray.Length - 1; index++)
//{
// Debug.WriteLine(eventsArray[index]);
// txtName.Text = Convert.ToString(eventsArray[index]);
//}
if(eventsArray.Length > _viewIndex)
{
txtName.Text = Convert.ToString(eventsArray[_viewIndex]);
//this is to fulfill the requirement below:
// Similarly, the first time the user presses “View”, the item found in index 0
// of the array is shown to the user; the second time the user presses “View”,
// the item found in index 1 of the array is shown, etc.
_viewIndex++;
}
}
}
Try this:
int[] eventsArray = new int[SIZE];
int index = 0;
const int SIZE = 1000;
private void btnSet_Click(object sender, EventArgs e)
{
eventsArray[index++] = Convert.ToInt32(txtEventName.Text);
}
I'm currently trying call a function for each entry inside a dataGridView until a condition returns true, this would take long, since the dataGridView could have a total count of 100 entrys.
Question: Is there a way to call the function for the first 5 elements at the same time and then continue with the next 5 elements? This way those "100" entrys could be done faster. Or is there another way to do this?
Current code Example:
private void button2_Click(object sender, EventArgs e)
{
new Thread(new ThreadStart(function)) { IsBackground = true }.Start();
}
private void function()
{
bool loopRunStop = false;
while (loopRunStop == false)
{
int count;
for (count = 0; count < dataGridView1.RowCount; count++)
{
bool response = anotherFunction(string parameter1, string parameter2);
if(response == true)
{
loopRunStop = true;
return;
}
}
}
}
count in initially 0.
array list contain 3 questions at 0 1 2 index.
using click event on button it shows only the 0 index value whenever i click on button.
i want that whenever i click on button array index incremented and show the next question.
public void Button1_Click(object sender, EventArgs e)
{
if (count != 3)
{
lbl_question.Text = question_list[count].ToString();
rdb_op1.Text = op1_list[count].ToString();
rdb_op2.Text = op2_list[count].ToString();
rdb_op3.Text = op3_list[count].ToString();
rdb_op4.Text = op4_list[count].ToString();
count = count + 1;
}
declare a static variable so that it doesn't reset when you load the page
public static int count { get; set; }
and then use it in your event
public void Button1_Click(object sender, EventArgs e)
{
if (count != 3)
{
lbl_question.Text = question_list[count].ToString();
rdb_op1.Text = op1_list[count].ToString();
rdb_op2.Text = op2_list[count].ToString();
rdb_op3.Text = op3_list[count].ToString();
rdb_op4.Text = op4_list[count].ToString();
count = count + 1;
}
else
{
count = 0;
}
I'm working on a small programme for booking seats on an airplane - And I keep getting this error. i want the programme to show me which seats on the plane (flysaeder) are being booking by what passenger (passagerer). Only, If I enter in more seats than I have passengers, it won't run - I need it to allow open seats (less "passagerer" than "flysaeder"). What am I doing wrong?
I'm kinda new at this, so I apologize for poor explanation.
Error occurs on "listeOverPassagerer[index] = listeOverPassagerer[i];".
namespace eksamenvingerne
{
public partial class Form1 : Form
{
int flysaeder;
int passagerer;
Random tilfældighed = new Random();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
listBox2.Items.Clear();
{
int.TryParse(txtsaeder.Text, out flysaeder);
int.TryParse(txtantalpassagere.Text, out passagerer);
if (passagerer > flysaeder)
{
MessageBox.Show("Ingen frie pladser!");
}
else
{
int[] listeOverPassagerer = Enumerable.Range(0, passagerer).ToArray();
int[] flypladser = new int[flysaeder];
for (int i = 0; i < flysaeder; i++)
{
int index = tilfældighed.Next(0, passagerer);
flypladser[i] = tilfældighed.Next(i, passagerer);
flypladser[i] = listeOverPassagerer[index];
listeOverPassagerer[index] = listeOverPassagerer[i];
}
for (int i = 0; i < flypladser.Length; i++)
{
listBox1.Items.Add("Sæde #" + i + ": Passagernr.:" + flypladser[i]); //listboxen udskriver indholdet af hver eneste plads.
}
}
}
}
}
}
Your logic actually is causing this problem:
First you make sure that passagerer <= flysaeder
if (passagerer > flysaeder)
{
MessageBox.Show("Ingen frie pladser!");
}
Then you do a for loop from 0 to flysaeder -1
for (int i = 0; i < flysaeder; i++)
But flysaeder might be larger than passagerer hence your access of listeOverPassagerer[i] will throw an exception since listeOverPassagerer is of length passagerer
I'm trying to make 'find/find next' function in my windows store application.
Word which I want to search and select is in textBox named 'tboxFind'.
Textbox 'EditorWindow' contains all my text.
My function works good only if there is one line of text in 'editorWindow'.
Otherwise, selection is moved forwards by number of new lines.
How to fix it?
Is there any simple way to create find next function?
private void btnFind_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
if ((tmpPos) == pos && tmpWord == tboxFind.Text && !String.IsNullOrEmpty(editorWindow.Text))
{
string tmpString = editorWindow.Text.Substring(pos + tboxFind.Text.Length);
tmpPos = tmpString.ToLower().IndexOf(tboxFind.Text.ToLower());
if (tmpPos != -1)
{
editorWindow.Focus(Windows.UI.Xaml.FocusState.Keyboard);
editorWindow.SelectionStart = pos + tmpPos + tboxFind.Text.Length;
editorWindow.SelectionLength = tboxFind.Text.Length;
pos = pos + tmpPos + tboxFind.Text.Length;
}
}
tmpWord = tboxFind.Text;
tmpPos = pos;
}
// EDIT:
I found a different way to create that function. Here is my code:
private void btnFind_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
numOfNewLines = 0;
pos = (tmpWord == tboxFind.Text) ? editorWindow.Text.ToLower().IndexOf(tboxFind.Text, pos + tboxFind.Text.Length)
: editorWindow.Text.ToLower().IndexOf(tboxFind.Text);
if (pos != -1)
{
foreach (char s in editorWindow.Text.Substring(0, pos))
{
if (s == '\n')
{
numOfNewLines++;
}
}
pos -= numOfNewLines;
editorWindow.Focus(Windows.UI.Xaml.FocusState.Keyboard);
//tmpPos = editorWindow.Text.ToLower().IndexOf(tboxFind.Text);
editorWindow.Select(pos, tboxFind.Text.Length);
pos += numOfNewLines;
}
tmpWord = tboxFind.Text;
}
I'm not 100% sure what's wrong with your code because I can't fully replicate it, but consider the following SSCCE in a basic Windows application:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
foreach (var i in FindIndicies("text"))
{
this.textBox1.SelectionStart = i;
this.textBox1.SelectionLength = "text".Length;
var result = MessageBox.Show(
"Move to the next index?",
"Next?",
MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.No) { break; }
}
}
private List<int> FindIndicies(string textToFind)
{
var indicies = new List<int>();
var offset = 0;
var i = 0;
while ((i = this.textBox1.Text.IndexOf(
textToFind,
offset,
StringComparison.CurrentCultureIgnoreCase)) > 0)
{
indicies.Add(i);
offset = (i + textToFind.Length);
}
return indicies;
}
}
given that textBox1 has a set text value of:
Here is a set of text
and I'm going to find the word text
Even when there are multiple lines of text.
It finds each index properly, and selects them properly.
I would consider finding all indicies up front with the method I wrote and then simply iterate through them on demand. In my case I'm using a message box to determine when I want to move to the next index, but you'll use something different.