C# Adding items with unique numbers in ComboBox with Button Click - c#

I am trying to create comboBox items with unique number sequencer.
With each click on button comboBox items should be added like this;
Item 1
Item 2
Item 3
...
If I modify The name of Item 1; Next click has to name new item as Item 1.
I am unable to create this basic sequencer and require your help.
private void addNew_Click(object sender, EventArgs e)
{
//textBox1.Enabled = true;
//textBox1.Focus();
comboBox1.Items.Add("item", i);
}

define counter on the scope of your form, under form constructor, for example. Like this:
int counter = 1;
public Form1()
{
InitializeComponent();
}
in your method for adding items add item with counter value and then increment counter.
private void addNew_Click(object sender, EventArgs e)
{
//textBox1.Enabled = true;
//textBox1.Focus();
comboBox1.Items.Add("item " + counter.ToString());
counter++;
}

Related

Clicking the same button but different action - c#

When i click on a button,text will appear in textbox1 but then i want it to change focus to another textbox(textbox2) and when i click the same button again,display the same text but in textbox2.
private void btna_Click(object sender, EventArgs e)
{
textBox1.Text = ("A");
if (textBox1.Text.Length > 0)
{
textBox2.Focus();
}
If you want to alternate between different TextBoxes in your click event to determine which one to update, you can track them in a private variable, and then just switch which one you're using based on the current value, for example:
private TextBox textBoxToUpdate = null;
private void button1_Click(object sender, EventArgs e)
{
// Swap the textBoxToUpdate between textBox1 and textBox2 each time
textBoxToUpdate = (textBoxToUpdate == textBox1) ? textBox2 : textBox1;
textBoxToUpdate.Text = "A";
}
Another way to do this if you're updating multiple controls is to store them in a list and increment an integer that defines the index to the next item.
// holds the text boxes we want to rotate between
private List<TextBox> textBoxesToUpdate = new List<TextBox>();
private void Form1_Load(object sender, System.EventArgs e)
{
// add some text boxes to our list
textBoxesToUpdate.Add(textBox1);
textBoxesToUpdate.Add(textBox2);
textBoxesToUpdate.Add(textBox3);
textBoxesToUpdate.Add(textBox4);
}
// stores the index of the next textBox to update
private int textBoxToUpdateIndex = 0;
private void button1_Click(object sender, EventArgs e)
{
textBoxesToUpdate[textBoxToUpdateIndex].Text = "A";
// increment the index but set it back to zero if it's equal to the count
if(++textBoxToUpdateIndex == textBoxesToUpdate.Count) textBoxToUpdateIndex = 0;
}

How to assign arrayList values into related labels in C#

This is a sign-up form with 3 textboxes and one dateTimePicker. There also have to be 4 buttons(add, show, prev and next) - when you click add button, it's supposed to store data typed by an each user and show button must print out information related to each one to the labels, then I should be able to walk through on eveyone's data when I click on prev and next buttons.
Simple as it sounds but I don't know how to assign values from textboxes into labels using ArrayList collections. How to do that?
ArrayList list = new ArrayList();
int ind = 0;
private void Add_Click(object sender, EventArgs e)
{
list.Add(textBox1.Text);
list.Add(textBox2.Text);
list.Add(textBox3.Text);
list.Add(dateTimePicker1.Text);
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
private void button3_Click(object sender, EventArgs e)
{
{
label5.Text =
label6.Text =
label7.Text =
label8.Text =

c# loop through list view items and display each in a text box a row at a time with a click of a button

I need to displaying items of the 4th column in a textbox row by row after clicking 'next' button. When the user clicks next, the next row should be displayed until the last row.
Below is the code I have so far, that only displays the last row when button 'next' is clicked. I cannot seem to get the loop right.
public partial class RecordingView : Form
{
public RecordingView()
{
InitializeComponent();
}
public void UpdateListView(ListView listView1)
{
foreach (ListViewItem item in listView1.Items)
{
this.listView1.Items.Add((ListViewItem)item.Clone());
}
}
private void RecordingView_Load(object sender, EventArgs e)
{
//Add column header
listView1.Columns.Add("#", 60);
listView1.Columns.Add("Start time", 100);
listView1.Columns.Add("End time", 100);
listView1.Columns.Add("Duration", 100);
listView1.Columns.Add("Text", 350);
}
private void btnNext_Click(object sender, EventArgs e)
{
for (int i = 0; i < listView1.Items.Count; i++)
{
string line = listView1.Items[i].SubItems[4].Text; //picks list item
rtbCurrentLine.Text = line;
}
}
}
Below is my user interface
user interface
kindly help me figure out where I have gone wrong.
I suggest this, declare a global variable that will get your index of next or previous click:
int intIndex = -1;
Then on your btnNext click event handler, increment it per click:
private void btnNext_Click(object sender, EventArgs e)
{
if (intIndex <= listView1.Items.Count)
{
intIndex++;
string line = listView1.Items[intIndex].SubItems[4].Text;
rtbCurrentLine.Text = line;
}
}

scroll to focused item in listbox dynamically

I have two list box in WPF which looks something like this:
Lets say, the left one is lbLeft and right one is lbRight. The ">" button adds one selected item from lbLeft to lbRight. And, "<" removes the selected item form lbRight from the list. The ">>" adds all the item from lbLeft to lbRight and "<<" clears lbRight.
When I double click an item from lbLeft, it is added to the lbLeft and that newly added item is focused. Also, if i try to add an item from lbLeft which already exists in lbRight, it places a focus in that selected item(so that items are not repeated). But when lots of item are added in lbRight, I have to manually scroll down to the point where focus is placed. How can I make the scrolling of listbox automatic to the point where focus is placed?
I have done the following:
private void select_Click(object sender, RoutedEventArgs e) // > button
{
addingItemToSelectedList();
}
private void remove_Click(object sender, RoutedEventArgs e) // < button
{
if (lbRight.SelectedItem != null) {
lbRight.Items.RemoveAt(lbRight.SelectedIndex);
}
}
private void selectall_Click(object sender, RoutedEventArgs e) // >> button
{
lbRight.Items.Clear();
foreach (string item in column1) {
lbRight.Items.Add(item);
}
}
private void lbLeft_MouseDoubleClick_1(object sender, MouseButtonEventArgs e)
{
addingItemToSelectedList();
}
private void addingItemToSelectedList() {
if (lbLeft.SelectedItem != null)
{
string item = lbLeft.SelectedItem.ToString();
addFocus(item);
}
}
private void addFocus(string item) {
if (!lbRight.Items.Contains(item))
{
lbRight.Items.Add(item);
lbRight.SelectedIndex = lbRight.Items.Count - 1;
lbRight.Focus();
}
else
{
int index = lbRight.Items.IndexOf(item);
lbRight.SelectedIndex = index;
lbRight.Focus();
}
}
private void removeall_Click_1(object sender, RoutedEventArgs e) //<< button
{
lbRight.ItemsSource = null;
lbRight.Items.Clear();
}
column1 in the code is a list of items which populate lbLeft.
UPDATE:
I tried to use lbRight.ScrollIntoView(lbRight.SelectedIndex); But it has no effect
ScrollIntoView() worked now. What i had to do was:
lbRight.ScrollIntoView(lbRight.Items[lbRight.SelectedIndex]);
It now passes the actual item rather than index of it.

How can i set the listBox1.SelectedIndex to 0 only to be effected on part of the code?

This is the code:
private void Lightnings_Mode_Load(object sender, EventArgs e)
{
this.Size = new Size(416, 506);
this.Location = new Point(23, 258);
listBoxIndexs();
this.listBox1.SelectedIndex = 0; // This will make the listBox when showing it first time first item to be already selected !!!!!!
}
private void listBoxIndexs()
{
for (int i = 0; i < Form1.lightningsRegions.Count; i++)
{
listBox1.Items.Add(Form1.lightningsRegions[i]);
}
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
item = listBox1.SelectedItem.ToString();
this.f1.PlayLightnings();
f1.pdftoolsmenu();
if (item != null && !pdf1.Lightnings.Contains(item.ToString()))
{
pdf1.Lightnings.Add(item.ToString());
}
}
Im using the variable item in two places in Form1.
Once to extract the string and play the numbers inside and once to add item to the List Lightnings.
In the first time for playing the numbers i want it to be:
this.listBox1.SelectedIndex = 0;
Since i want to be able to play already the first item once i clicked a button and showed/opened the listBox.
In the second place where im adding the item to the Lightnings List i want that it will add the item only if i clicked first on any item.
Since i did :
this.listBox1.SelectedIndex = 0;
It will add item automatic to Lightnings once i show/open the listBox
I need it to be added to the List only if i click first an item in the other hand i also want to be selectedindex = 0 since i want it to be selected so i can play it.
So how can i separate between the SelectedIndex = 0 for playing and for adding the item to the List ?
If I understand correctly, you can simply add a flag.
bool allowItemAdding;
private void Lightnings_Mode_Load(object sender, EventArgs e)
{
allowItemAdding = false; //setting false here because *sometimes* Load event is called multiple times.
this.Size = new Size(416, 506);
this.Location = new Point(23, 258);
listBoxIndexs();
this.listBox1.SelectedIndex = 0;
allowItemAdding = true; //set flag to true after selecting the index initially
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
item = listBox1.SelectedItem.ToString();
this.f1.PlayLightnings();
f1.pdftoolsmenu();
if (allowItemAdding)
{
if (item != null && !pdf1.Lightnings.Contains(item.ToString()))
{
pdf1.Lightnings.Add(item.ToString());
}
}
}
The flag will then stay true until you explicitly change it to false, so you can control when items should be added or not.
use _SelectionChangeCommitted instead of listBox1_SelectedIndexChanged

Categories