Change automatically a label in c# forms - c#

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

Related

Names in the listbox

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);

TextBox to Display Mouse Clicks

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();

Loop through a ListBox and set TextBox Text

How do I auto select a item in a ListBox then set it as text in a TextBox then wait 3 seconds and move onto the next line down and repeat?
private void button2_Click(object sender, EventArgs e)
{
timer.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
foreach (var item in listBox1.Items)
{
textBox2.Text = listBox1.Text;
}
}
The ListBox has a lot of useful properties:
private void timer1_Tick(object sender, EventArgs e)
{
textBox2.Text = listBox1.SelectedItem.ToString();
listBox1.SelectedIndex = (listBox1.SelectedIndex + 1) % listBox1.Items.Count;
}
% is the modulo operation and returns the remainder of the division. It ensures that always values between 0 and SelectedIndex - 1 are returned and makes the indexes repeat.
Also, if no item is selected, you will get a SelectedIndex of -1 and SelectedItem will be null. So be sure to either avoid these cases by setting appropriate initial conditions or add checks.
E.g.:
if (listBox1.Items.Count > 0) {
if (listBox1.SelectedIndex == -1) {
listBox1.SelectedIndex = 0;
}
... // code of above
}
Your timer1_Tick should be something like this:
public Form1()
{
InitializeComponent();
timer1.Interval = 3000;
}
private void timer1_Tick(object sender, EventArgs e)
{
Random rnd = new Random();
int i = rnd.Next(0, listBox1.Items.Count - 1);
textBox2.Text = listBox1.Items[i].ToString();
}
EDIT:
int i;
private void timer1_Tick(object sender, EventArgs e)
{
if (i > listBox1.Items.Count - 1)
{
i = 0;//Set this to repeat
return;
}
textBox2.Text = listBox1.Items[i].ToString();
i++;
}
And also set the timer's Interval to 3000.

Condensing lots of Button Click Events

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

Selecting both listbox item by just selecting on one listbox item from another listbox

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

Categories

Resources