Add string to same textBox instead of current string - c#

I have form which should change text inside textbox on button click. Text should be read from List, k.pojam. So on first click to the button text Box should show first element in list, second click second element etc. How to fix problem ?
//List<Karta> list; this list already has some number of Karta objects
int cardCounter=0;
private void btnNext_Click(object sender, EventArgs e)
{
int currentCounter = 0;
foreach (Karta k in list)
{
if(cardCounter==currentCounter)
{
txtBoxPojam.Text = k.Pojam;
txtBoxPojam.Show();
cardCounter++;
}
currentCounter++;
}
edit txtBoxPojam.Text += k.Pojam; // I need something like this, but instead of adding string I would like to write another string instead of current, because obviously txtBoxPojam.Text = k.Pojam; doesnt work?

int mainCounter = 0;
int totalItems = 0;
List<string> LstItems = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
LstItems.Add("Test Item 1");
LstItems.Add("Test Item 2");
LstItems.Add("Test Item 3");
LstItems.Add("Test Item 4");
LstItems.Add("Test Item 5");
totalItems = LstItems.Count();
}
private void btnNext_Click(object sender, EventArgs e)
{
try
{
if (mainCounter > totalItems)
{
//your implementation
return;
}
txtNext.Text = LstItems[mainCounter].ToString();
mainCounter++;
}
catch (Exception)
{
}
}

Instead of txtBoxPojam.Text = k.Pojam; working combination is txtBoxPojam.Text = string.Empty; and txtBoxPojam.Text += k.Pojam;
txtBoxPojam.Text = string.Empty;
int cardCounter=0;
private void btnNext_Click(object sender, EventArgs e)
{
int currentCounter = 0;
foreach (Karta k in list)
{
if(cardCounter==currentCounter)
{
txtBoxPojam.Text += k.Pojam;
txtBoxPojam.Show();
cardCounter++;
}
currentCounter++;
}

Related

cant get my Value in Listview Virtualmode

after clicking my value and pressing my OK_button I cant get the Value out of the listView to save it somewhere else. I cant use listView1.FindItemWithText because I don't have a text to search for.. Idk how to look for the clicked value after I pressed the OK_button
//Create dummy data to display
myData = new string[dataListSize];
for (int i = 0; i < dataListSize; i++)
{
myData[i] = String.Format("{0}", i);
}
}
private void listView1_SearchForVirtualItem(object sender, SearchForVirtualItemEventArgs e)
{
e.Index = Array.FindIndex(myData, s => s == textBox1.Text.ToString());
}
private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
e.Item = new ListViewItem(myData[e.ItemIndex]);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
String MyString = textBox1.Text.ToString();
ListViewItem lvi = listView1.FindItemWithText(MyString.TrimEnd());
//Select the item found and scroll it into view.
if (lvi != null)
{
listView1.SelectedIndices.Clear();
listView1.SelectedIndices.Add(lvi.Index);
listView1.EnsureVisible(lvi.Index);
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e){ }
private void OK_button_Click(object sender, EventArgs e)
{
try
{
// OK -> Daten übernehmen
int iCount = this.listView1.SelectedIndices.Count;
if (iCount != 1)
{
MessageBox.Show("Value is empty");
return;
}
DialogResult = DialogResult.OK;
Close();
}
catch (Exception)
{
//WriteProtokoll(ex.ToString(), 0);
Close();
}
}
I found out that I can get my value with:
string txt = listView1.FocusedItem.Text;

C# Updating Selected ComboBox Item Name from Textbox

I want to update comboBox selectedItem name by changing textbox name. Without losing combobox value, How can I achieve it?
private void addItem_Click(object sender, EventArgs e)
{
nameItem.Enabled = true;
nameItem.Text = "Item " + counter.ToString();
nameItem.Focus();
comboBox1.Items.Add(nameItem.Text);
comboBox1.SelectedItem = nameItem.Text;
counter++;
}
private void nameItem_TextChanged(object sender, EventArgs e)
{
????????
}
This one is simple and it's working, but it maybe a little bit long.
Here I got a combo box, textBox1 and button for adding value to combo box,
and textBox2 for editing selected item.
string[] items = new string[99];
int a = 0;
int i = 0;
private void button1_Click(object sender, EventArgs e)
{
items[i] = textBox1.Text;
i++;
comboBox1.Items.Clear();
for (int n = 0; n < items.Length; n++) {
if(items[n] != null) comboBox1.Items.Add(items[n]);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
a = comboBox1.SelectedIndex;
MessageBox.Show(a.ToString());
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
items[a] = textBox2.Text;
comboBox1.Items.Clear();
for (int n = 0; n < items.Length; n++)
{
if (items[n] != null) comboBox1.Items.Add(items[n]);
}
}
How it works: we have an array for items and two integar variables. one for count of added items and other for selected Items index
Button 1 just addes new item, clears all items and update em again
when u edit text of textbox2, It will update items from 'items' array, and then update the combo box, ez
EDIT: Did not see winform tag - this will not work - will leave in case any ASP people come across.
public void TextBox1_OnTextChanged(object sender, EventArgs e)
{
ddl.DataSource = null;
ddl.DataBind();
ddl.DataTextField = "Text";
ddl.DataValueField = "Value";
ddl.DataSource = (from ListItem b in ddl.Items
select b.Selected ? new ListItem(TextBox1.Text, b.Value) : b).ToList();
ddl.DataBind();
}
ddl is name of the dropdown box
Textbox1 is the name of the textbox.
This will change the name of the selected item. If you need more code let me know in comments.
Thank you for your time and kind answers.
I solved the problem as it looks below;
private void addItem_Click(object sender, EventArgs e)
{
nameItem.Enabled = true;
comboBox1.Items.Add("Item " + counter.ToString());
comboBox1.SelectedItem = "Item " + counter.ToString();
nameMacro.Text = "Item " + counter.ToString();
//comboBox1.SelectedItem = nameItem.Text;
//nameItem.Focus();
// Ad degistirme -> comboBox1.Items[comboBox1.FindStringExact("string value")] = "New Value";
counter++;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
comboBox1.Items[comboBox1.SelectedIndex] = nameItem.Text;
}
it's better to update the source then bind it again.
try this also
private void nameItem_TextChanged(object sender, EventArgs e)
{
string value = nameItem.Text;
var list = (List<KeyValuePair<String, String>>)comboBox1.DataSource;
list.Add(new KeyValuePair<string, string>(value,value));
comboBox1.DataSource = list;
comboBox1.DataBind();
}

autocalculate total value of items in listview when removing item using c#

I'm using a listview as a shopping cart. I need to know how to recalculate the total value of the cart when I remove an item.
Here is my code for adding to listview;
private void btnACart_Click(object sender, EventArgs e)
{
int value = 0;
for (int i = 0; i < lvCart.Items.Count; i++)
{
value += int.Parse(lvCart.Items[i].SubItems[1].Text);
}
rtbTcost.Text = value.ToString();
}
Here is my code for removing items:
private void btnRemoveItem_Click(object sender, EventArgs e)
{
int total = 0;
foreach (ListViewItem item in lvCart.Items)
{
if (lvCart.Items[0].Selected)
{
lvCart.Items.Remove(lvCart.SelectedItems[0]);
total += Convert.ToInt32(item.SubItems[1].Text);
}
}
rtbTcost.Text = total.ToString();
}
I want to recalculate the total value of items an item is removed. How should I do that?
Something like this
On the form level declare
private int _listTotal;
Adding - I think here you have some problems because you should add to total when you add the item
private void btnACart_Click(object sender, EventArgs e)
{
int value = 0;
for (int i = 0; i < lvCart.Items.Count; i++)
{
value += int.Parse(lvCart.Items[i].SubItems[1].Text);
}
// how about lvCart.Items.Add(<myVal>)...???
_listTotal += value; // and here add myVal
rtbTcost.Text = _listTotal.ToString();
}
Then when removing - you don't want to use any "for-loops" on mutating collection. But "while" works perfectly on mutations
private void btnRemoveItem_Click(object sender, EventArgs e)
{
int totalRemoved = 0;
while (lvCart.SelectedItems.Count > 0)
{
totalRemoved += Convert.ToInt32(lvCart.SelectedItems[0].SubItems[1].Text);
lvCart.Items.Remove(lvCart.SelectedItems[0]);
}
_listTotal -= totalRemoved;
rtbTcost.Text = _listTotal.ToString
}
Not tested but should work
You can't change the same list in foreach.
foreach (ListViewItem item in lvCart.Items)
{
if (lvCart.Items[0].Selected)
{
lvCart.Items.Remove(lvCart.SelectedItems[0]);
total += Convert.ToInt32(item.SubItems[1].Text);
}
}
The solution is create duplicate list and change it:
var newList = lvCart;
foreach (ListViewItem item in lvCart.Items)
{
if (lvCart.Items[0].Selected)
{
newList.Items.Remove(lvCart.SelectedItems[0]);
total += Convert.ToInt32(item.SubItems[1].Text);
}
}
private void btnRemoveItem_Click(object sender, EventArgs e)
{
int total = 0;
foreach (ListViewItem item in lvCart.Items)
{
if (lvCart.Items[0].Selected)
{
total+=(int)lvCart.SelectedItems[0].SubItems[1].Text;//Add to total while romving
lvCart.Items.Remove(lvCart.SelectedItems[0]);
//total += Convert.ToInt32(item.SubItems[1].Text);
}
}
rtbTcost.Text = total.ToString();
}

how to make row flash every 3 seconds

I dont know if I'm doing this correctly but I have a grid and I loop through the grid to see if the items matched. If they do I want to make the row flash every 3 seconds. Right now what I have in my code just pretty much highlight the row but no flashing. Can anyone help take a look?
public static void CheckRow(int item, DataGridViewRow row)
{
List<int> col = new List<int>();
//call to db and add to col
foreach (var item in col)
{
if (item == col.Item)
{
currentRow = row;
Timer t = new Timer();
t.Interval = 3000;
t.Tick += new System.EventHandler(Highlight);
t.Start();
}
}
}
private static void Highlight(object sender, EventArgs e)
{
currentRow.DefaultCellStyle.BackColor = Color.Brown;
}
Wouldn't you need to change the color again (to the original) to have a flashing effect?
You should use Threading. Look at the code :)
bool go = false; //for changing cell color
int count = 10; //to stop timer (blinking)
public blinkForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
Thread a = new Thread(blink);
a.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
if (dataGridView1.Columns.Count == 0)
{
//generate new columns for DataGridView
dataGridView1.Columns.Add("user", "User");
dataGridView1.Columns.Add("pcStatus", "PC Status");
dataGridView1.Columns.Add("service", "Servis");
//generate new rows for DataGridView
dataGridView1.Rows.Add("Ali", "PC007", "chrome.exe");
dataGridView1.Rows.Add("Vusal", "PC010", "photoshop.exe");
dataGridView1.Rows.Add("Rahim", "PC015", "chrome.exe");
}
}
private void blink(object o)
{
while (count > 0)
{
while (!go)
{
//change color for binking
dataGridView1.Rows[0].Cells["service"].Style.BackColor = Color.Tomato;
go = true;
//stop for 0.5 second
Thread.Sleep(500);
}
while (go)
{
//change color for binking
dataGridView1.Rows[0].Cells["service"].Style.BackColor = Color.LimeGreen;
go = false;
//stop for 0.5 second
Thread.Sleep(500);
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
count--;
if (count == 0)
{
//stop blinking after 10 second
timer1.Stop();
}
}
Perhaps this, no?
private static void Highlight(object sender, EventArgs e)
{
currentRow.DefaultCellStyle.BackColor = Color.Brown;
System.Threading.Thread.Sleep(2000);
currentRow.DefaultCellStyle.BackColor = Color.White;
}

How to remove multiple selected items in ListBox?

My windows form contains two listboxes. Listbox1 contains some items in it and listbox2 is empty. When I press a button on the form, then multiple selected items from listbox1 should be removed from Listbox1 and copied to Listbox2.
I tried with foreach loop on listbox1.SelectedItems but it removes only 1 item from list.
Anyone has solution or code for this?
You could do all in a single loop. You should use a simple for and loop backwards on SelectedIndices:
private void button1_Click(object sender, EventArgs e)
{
for(int x = listBox1.SelectedIndices.Count - 1; x>= 0; x--)
{
int idx = listBox1.SelectedIndices[x];
listBox2.Items.Add(listBox1.Items[idx]);
listBox1.Items.RemoveAt(idx);
}
}
you must store The values, you want to delete in other palce and then delete them from List,Here is sample code:
private void button1_Click(object sender, EventArgs e)
{
ArrayList tmpArr = new ArrayList();
foreach (object obj in listBox1.SelectedItems)
{
listBox2.Items.Add(obj);
tmpArr.Add(obj);
}
foreach (object obj in tmpArr.ToArray())
{
listBox1.Items.Remove(obj);
}
}
I did this using using the CopyTo method to copy the items to an array the length of the count of selected items and then looped around that array removing each corresponding item from ListBox1.
private void button1_Click(object sender, EventArgs e)
{
object[] itemsToRemove = new object[listBox1.SelectedItems.Count];
listBox1.SelectedItems.CopyTo(itemsToRemove, 0);
foreach (object item in itemsToRemove)
{
listBox1.Items.Remove(item);
listBox2.Items.Add(item);
}
}
For VS2005 I user something similar as I couldn't use .selectedIndices
for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
{
if (ListBox1.Items[i].Selected)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
}
for (int x = listBox1.SelectedIndices.Count - 1; x >= 0; x--)
{
int var = listBox1.SelectedIndices[x];
listBox1.Items.RemoveAt(var);
}
Its Works.
This is my method:
List<String> arr = new List<string>();
private void btnAdd_Click(object sender, EventArgs e)
{
arr.Add(txtItem.Text);
lstItem.DataSource = arr.ToArray();
txtItem.Focus();
}
//When i delete
private void btnRemove_Click(object sender, EventArgs e)
{
foreach (string item in lstItem.SelectedItems)
{
arr.Remove(item);
}
lstItem.DataSource = arr.ToArray();
}
I found this code worked for me. The aim was to delete multiple rows of "items" with the press of the delete key. I have used a ListViewBox for the original input storage.
private void visitorsOnsiteListLB_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyData == Keys.Delete)
{
foreach (ListViewItem item in visitorsOnsiteListLB.SelectedItems)
{
visitorsOnsiteListLB.Items.Remove(item);
}
}
}

Categories