I need help with program, where I have a listbox with names and I need to check if there is more than 3 same names in the listbox when I press the buttonControl, the MessegeBox is displayed and it says "There are too many of the same names in the list as allowed". The code is like this for now:
private void buttonVlozjmeno_Click(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
string add_name = textBoxName.Text;
if (index >= 0)
listBox1.Items.Insert(index, add_name);
else
listBox1.Items.Add(add_name);
textBoxJmeno.Text = null;
}
private void buttonADD_Click(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(index);
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("Adam");
listBox1.Items.Add("Adam");
listBox1.Items.Add("Adam");
listBox1.Items.Add("John");
listBox1.Items.Add("John");
listBox1.Items.Add("Eva");
listBox1.Items.Add("John");
listBox1.Items.Add("John");
}
private void buttonControl_Click(object sender, EventArgs e)
{
}
You can try querying the box with a help of Linq, e.g. for WinForms it can be
using System.Linq;
...
bool tooManySameNames = myBox
.Items
.OfType<String>()
.GroupBy(name => name)
.Any(group => group.Count() > 3);
Related
my problem is that i'm trying to have a textbox display the number of times that I have clicked on the screen. I'm having the issue of not being able to take the textbox's text and convert it to a int. Thanks!
{
public partial class Form1 : Form
{
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
button1.Location = new Point(rand.Next(0, 750), rand.Next(0, 750));
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
{
int mouseclick = 0;
textBox1.Text = Int32.Parse(mouseclick);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Try the following changes:
{
public partial class Form1 : Form
{
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
button1.Location = new Point(rand.Next(0, 750), rand.Next(0, 750));
}
int mouseclick = 0;
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
{
mouseclick++;
}
textBox1.Text = mouseclick.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
I have moved mouseclick out of the event so that it can maintain its value and i am only incrementing mouseclick if the user clicks with the left mouse button.
The textbox datatype is a string, but its assignment is a number being parsed as a number. Perhaps the following example would be helpful.
string mouseclick = "0";
int intMouseClick = Int32.Parse(mouseclick);
textBox1.Text = intMouseClick.ToString();
You tried to convert an int into string bei using Int32.Parse(...).
But this function does the reverse conversion. It converts a string to an int.
First off, you are telling the program to set mouseclicks to 0 every single time it is clicked. You might instead want the code to read:
if (e.Button != MouseButtons.Right)
{
int mouseclick = mouseclick + 1;
textBox1.Text = Int32.Parse(mouseclick);
}
or move the int out of the if statement and just do mouseclick++;
On top of that, if you are trying to set the text boxes text, you can do it much easier by just doing this:
textBox1.Text = mouseclick.ToString();
i'm making my own sudoku game but i don't know where to start.
I've created buttons but i don't know how to group them for creating a conditions
can anyone please help me. this is my project! thank you! I know this is a 9x9 set of boxes but i'm starting at first 9 boxes. this is my code or if anyone have they're own sudoku code please let me know so i can review it. thank you very much.
private void b11_Click(object sender, EventArgs e)
{
b11.Text = tbNum.Text;
Algo = '1';
}
private void b18_Click(object sender, EventArgs e)
{
b18.Text = tbNum.Text;
Algo = '8';
}
private void b17_Click(object sender, EventArgs e)
{
b17.Text = tbNum.Text;
Algo = '7';
}
private void b16_Click(object sender, EventArgs e)
{
b16.Text = tbNum.Text;
Algo = '6';
}
private void b15_Click(object sender, EventArgs e)
{
b15.Text = tbNum.Text;
Algo = '5';
}
private void b14_Click(object sender, EventArgs e)
{
b14.Text = tbNum.Text;
Algo = '4';
}
private void b13_Click(object sender, EventArgs e)
{
b13.Text = tbNum.Text;
Algo = '3';
}
private void b12_Click(object sender, EventArgs e)
{
b12.Text = tbNum.Text;
Algo = '2';
}
private void b19_Click(object sender, EventArgs e)
{
b19.Text = tbNum.Text;
Algo = '9';
}
private void Form1_Load(object sender, EventArgs e)
{
}
I have a form in C# Windows Forms, every time a button is clicked I change my index, and I want to change the content of a Label based on this index. The only choice I found is label_click however I want the change to be automatically. Any idea about this?
// lines a gloab list of strings and index changes from a button click
private void label1_Click(object sender, EventArgs e)
{
label1.Text = "videos/" + lines[index] + ".mp4";
}
private void button4_Click(object sender, EventArgs e)
{
index++;
}
private void button3_Click(object sender, EventArgs e)
{
if (index >= 1)
index--;
}
Try the following
List<string> lines = new List<string>(){/*initialization here*/}
int index = 0;
private void button4_Click(object sender, EventArgs e)
{
//Ensure index is inside List bounds.
index = Math.Min(lines.length -1 , index + 1);
ChangeLabelText()
}
private void button3_Click(object sender, EventArgs e)
{
//Ensure index is inside List bounds.
index = Math.Max(0 , index - 1);
ChangeLabelText()
}
void ChangeLabelText() => label1.Text = "videos/" + lines[index] + ".mp4";
I have 40 buttons that all do something slightly different when clicked, I would like to condense this down if I can. I also want to say, if one of the buttons is clicked, create a timestamp which can be accessed by the class.
Here is the code for 2 out of 40 of the buttons:
private void Btn1_Click(object sender, RoutedEventArgs e)
{
for (int i = 1; i < 5; i++)
{
CheckBox CheckBox = (this.FindName(string.Format("Check{0}", i)) as CheckBox);
if (CheckBox != null)
{
CheckBox.IsChecked = true;
}
}
}
private void BtnDisable1_Click(object sender, RoutedEventArgs e)
{
for (int i = 1; i < 5; i++)
{
CheckBox CheckBox1 = (this.FindName(string.Format("Check_D{0}", i)) as CheckBox);
if (CheckBox1 != null)
{
CheckBox1.IsChecked = false;
}
}
}
I think one way of doing it is putting it in an array and whenever one of the 40 buttons are clicked it looks in the array on what to do next? I'm not really sure, thank you!
You can make this simple using one method.
Answer is updated based on this discussion
private void DoWork(int checkboxGroup, bool enable)
{
int start = checkboxGroup * 4;
for (int i = start; i < start + 4; i++)
{
CheckBox CheckBox = this.FindName("CheckBox" + i) as CheckBox;
if (CheckBox != null)
{
CheckBox.IsChecked = enable;
}
}
}
private void Btn1_Click(object sender, RoutedEventArgs e)
{
DoWork(1 , true);
}
private void BtnDisable1_Click(object sender, RoutedEventArgs e)
{
DoWork(1 , false);
}
Because there are 40 methods like this you can use Expression bodied methods. You must have C#6 to use this feature.
private void Btn1_Click(object sender, RoutedEventArgs e) => DoWork(1 , true);
private void BtnDisable1_Click(object sender, RoutedEventArgs e) => DoWork(1 , false);
private void Btn2_Click(object sender, RoutedEventArgs e) => DoWork(2, true);
private void BtnDisable2_Click(object sender, RoutedEventArgs e) => DoWork(2, false);
// and so on
First I'm going to select an item from ListBox1 then if I select an item in ListBox1, the corresponding index of ListBox2 should be selected also.
my code for ListBox1
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.SelectedIndex = listBox2.SelectedIndex;
}
here's for ListBox2
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.SelectedIndex = listBox2.SelectedIndex;
}
I didn't understand the method clearly and there must be a confusion happening.
I just need help in this part and hope you guys could share some knowledge about this.
Change your code like this:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox2.Items.Count >= listBox1.SelectedIndex + 1)
{
listBox2.SelectedIndex = listBox1.SelectedIndex;
}
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.Items.Count >= listBox2.SelectedIndex + 1)
{
listBox1.SelectedIndex = listBox2.SelectedIndex;
}
}