C# Winform Listview selected item issue - c#

I'm trying to copy the selected item's filename and its path to the clipboard and then a textbox from a listview. I can't seem to get this one to work how i want. Here's the code I've been playing around with.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.Items.Count > 0)
{
listView1.Items[0].Selected = true;
Clipboard.SetDataObject(this.listView1.SelectedItems[0]);
textBox1.Paste();
}
}
Can someone get me on the right track?

I'm not sure why you're using the Clipboard here. You can do just fine without it.
listView1.Items[0].Selected = true;
textBox1.Text = this.listView1.SelectedItems[0].ToString();

private void listView1_SelectedIndexChanged(object sender, EventArgs e) {
if (listView1.SelectedItems.Count > 0) {
textBox1.Text = listView1.SelectedItems[0].Text;
}
else {
textBox1.Text = string.Empty;
}
}

Related

How can i store a textboxt text for an array and restore it later?

What i want to do is: I have a "+" button, that creates a new tab every click, and one textbox. I want to do every tab create a text array for the same textbox (1 textbox, having different values according to the selected tabcontrol tab.) i have a maximum value of 5 tabs that i set, how can i do that?
i searched alot on how to create it, but didnt found a specific one for what i need
code of tabcontrols
private void label1_Click(object sender, EventArgs e)
{
if(tabControl1.TabPages.Count != 5)
{
page++;
string title = "Script " + page.ToString();
TabPage tabipage = new TabPage(title);
tabControl1.TabPages.Add(tabipage);
}
else
{
MessageBox.Show("You cant add more tabs!");
}
}
also, doesnt need to be exactly an array, just need to save and restore values between tabs
This is a pretty open ended question, and you'll have to forgive me if I misunderstood. What I'm guessing is you have a tab control, and OUTSIDE the tab control you have a single textbox that should change based on which tab is selected? If so you could do something similar to the below:
class tabWithScript : TabPage
{
public tabWithScript(string title) : base(title)
{ }
public string myscript { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
if (tabControl1.TabPages.Count < 5)
{
string title = "Script " + (tabControl1.TabPages.Count +1 ).ToString();
tabWithScript tabipage = new tabWithScript(title);
tabipage.myscript = $"Oh man, this is my script {tabControl1.TabPages.Count +1}";
tabControl1.TabPages.Add(tabipage);
}
else
{
MessageBox.Show("You cant add more tabs!");
}
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
tabWithScript myTab = (tabWithScript)tabControl1.SelectedTab;
textBox1.Text = myTab.myscript;
}
catch (Exception ex) { }
}
}
Instead of using the default TabPage object you can create your own class that inherits TabPage, then store whatever information you want in there. (You'd likely have to delete the default TabPages the tabcontrol creates).
Alternatively, you can just create a string array and index it appropriately e.g.
string[] myList = new string[5];
private void button1_Click(object sender, EventArgs e)
{
if (tabControl1.TabPages.Count < 5)
{
string title = "Script " + (tabControl1.TabPages.Count +1 ).ToString();
TabPage tabipage = new TabPage(title);
myList[tabControl1.TabPages.Count] = $"Oh man, this is my script {tabControl1.TabPages.Count +1}";
tabControl1.TabPages.Add(tabipage);
}
else
{
MessageBox.Show("You cant add more tabs!");
}
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = myList[tabControl1.SelectedIndex];
}
`
An alternative of #Devon Page's solution is using the Tag property, existing in almost all controls, to store value.
private void label1_Click(object sender, EventArgs e)
{
if (tabControl1.TabPages.Count < 5)
{
page++;
string title = "Script " + page.ToString();
TabPage tabipage = new TabPage(title);
tabipage.Tag = title; //Store whatever you want.
tabControl1.TabPages.Add(tabipage);
}
else
{
MessageBox.Show("You cant add more tabs!");
}
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = tabControl1.SelectedTab.Tag.ToString(); // Cast to restore it
}

How to multiselect DataGridView rows without pressing CTRL key in c#?

I am new to c# and using windows forms.
I have a DataGridView control on a form and I need to allow a user to multi-select rows without pressing the CTRL key and without using check box column. I have already enabled the mutli-select property.
I know this is duplicated question on Here but I tried the first answer (Bolu answer) and it worked but the datagridvied refreshes and flicks every time I select a row.
I wanted to try the "Edit: Better solution" (in the same answer) and it is too complicated for me I did not understand the steps.
My question: How can I get rid of the flicking/ refreshing process and make it smooth when I select a row? (the code shown below), also I am happy to receive any new solutions. Please help me, thank you
DataGridViewRow[] old;
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
old = new DataGridViewRow[dataGridView1.SelectedRows.Count];
dataGridView1.SelectedRows.CopyTo(old, 0);
}
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
foreach (DataGridViewRow gr in old)
{
if (gr == dataGridView1.CurrentRow)
{
gr.Selected = false;
}
else
{
gr.Selected = true;
}
}
}
Try this; keep the datagridview (dgvTopics in my case) mutiselect true:
private void dgvTopics_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (dgvTopics.Rows[e.RowIndex].Selected == true)
dgvTopics.Rows[e.RowIndex].ErrorText = "U";
else
dgvTopics.Rows[e.RowIndex].ErrorText = "S";
}
private void dgvTopics_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
foreach (DataGridViewRow dgvr in dgvTopics.Rows)
{
if(dgvr.ErrorText == "S")
dgvr.Selected = true;
else
dgvr.Selected = false;
}
}

How to deselect item of listbox?

I have a ListBox that when an item is selected, it is shown in a label as well. However, when I want to remove the selected item, program breaks and shows a NullReferenceException.
My code:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = "Your Selected: " + listBox1.SelectedItem.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
label1.Text = "";
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
It may appear, that there's no selected item in the listbox, so you have to check for that:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = null == listBox1.SelectedItem
? ""
: "Your Selected: " + listBox1.SelectedItem.ToString();
}
private void button2_Click(object sender, EventArgs e) {
// Looks redundant, listBox1_SelectedIndexChanged will do
//label1.Text = "";
// Deselect item, but not remove it
if (listBox1.SelectedIndex >= 0)
listBox1.SelectedIndex = -1;
// In case you want to remove the item (not deselect) - comment out the code below
// if (listBox1.SelectedIndex >= 0)
// listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
Edit: as for counting listbox items, there's no event fo this in the current listbox implementation. So you have to do it manually:
if (listBox1.SelectedIndex >= 0) {
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
lbItemsCount.Text = listBox1.Items.Count.ToString();
}
Another way is to use click event of the list box , if we do not want to double click the one list box item for the deselection of another list items.
ex:
private void ListBox_Right_Click(strong textobject sender, EventArgs e)
{
Btn_Left.Enabled = ListBox_Right.SelectedIndex >= 0;
ListBox_Left.ClearSelected(); // to clear the list selection/highlight
Btn_Right.Enabled = false; // for my specification
}
}
private void ListBox_Left_Click(object sender, EventArgs e)
{
Btn_Right.Enabled = ListBox_Left.SelectedIndex >= 0;
ListBox_Right.ClearSelected(); //to clear the list selection/highlight
Btn_Left.Enabled = false;// for my specification
}

Passing params to EventMethod in Visual C#

I have these two methods and I have to use them to modify the selected item in a ListBox and after editing it in the same txtBox I use for filling up the ListBox to get it back and replace It with the old one.
private void txtBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
List.Items.Add(Label.Content);
Label.Content = "";
txtBox.Text = "";
}
}
private void ButtonModify_Click(object sender, RoutedEventArgs e)
{
int index = List.SelectedIndex;
object item = List.SelectedItem;
txtBox.Text = (string)item;
txtBox_KeyUp(????????);
}
My intuition says I have to call the EventMethod I've use to fill the ListBox, but there are some parameter which I can't remove cause I need them in the txtBody_KeyUp(). So my question is what I have to write as an arguments to work or is there some other way for doing this?
You Can Use Below Code.
Text Box PreviewKeyDown Event
private int _tmpIndex = -1;
private void TextBox_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Enter || _tmpIndex == -1) return;
ListBox1.Items[_tmpIndex] = TextBox1.Text;
TextBox1.Text = "";
_tmpIndex = -1;
}
ListBox MouseDoubleClick Event
private void ListBox_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (ListBox1.SelectedIndex <= 0) return;
TextBox1.Text = ListBox1.SelectedItem.ToString();
_tmpIndex = ListBox1.SelectedIndex;
}
Have Fun

C# Windows Forms app - Clear ComboBox selected text from another ComboBox

Windows Forms application (c#).
I have two ComboBoxes.
If I select an item in one, I want the text in the other one to be blank.
This is what I have:
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox2.Text = "";
}
private void ComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox1.Text = "";
}
... but as you can see, when I make a selection in one, the text in both ComboBoxes get cleared.
How to accomplish this?
Thank you.
Try setting the ComboBox.SelectedIndex to -1
A zero-based index of the currently selected item. A value of negative
one (-1) is returned if no item is selected.
or rather based on your specifications try something lie
private bool changed = false;
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!changed)
{
changed = true;
ComboBox2.Text = "";
changed = false;
}
}
private void ComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (!changed)
{
changed = true;
ComboBox1.Text = "";
changed = false;
}
}

Categories