I have a List of Employee. I just want to perform simple Drag and Drop. i.e. drag and remove an Employee from SourceListBox and Add that Employee Object in TargetListBox.
I am having two problems.
Removing the employee from SourceListBox gives me an exception and the value displayed in TargetListBox is not the Employee Object but the string Drag_and_Drop.Employee
private List<Employee> emp = new List<Employee>();
emp.Add(new Employee { EmployeeId = 1, Name = "Arslan" });
emp.Add(new Employee { EmployeeId = 2, Name = "Talha" });
SourceListBox.ItemsSource = emp;
SourceListBox.DisplayMemberPath = "Name";
SourceListBox.SelectedValuePath = "EmployeeId";
private void SourceListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DragDropEffects dde = DragDrop.DoDragDrop(SourceListBox, SourceListBox.SelectedItem, DragDropEffects.All);
var empi = (Employee) SourceListBox.SelectedItem;
if(dde == DragDropEffects.All)
{
emp.Remove(empi);
SourceListBox.ItemsSource = null;
SourceListBox.ItemsSource = emp;
}
}
private void TargetListBox_DragEnter(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.All;
TargetListBox.Items.Add(e.Data.GetData(typeof(Employee)));
}
Basic example from C# Drag and Drop From listBox
public object lb_item = null;
private void listBox1_DragLeave(object sender, EventArgs e)
{
ListBox lb = sender as ListBox;
lb_item = lb.SelectedItem;
lb.Items.Remove(lb.SelectedItem);
}
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (lb_item != null)
{
listBox1.Items.Add(lb_item);
lb_item = null;
}
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
lb_item = null;
if (listBox1.Items.Count == 0)
{
return;
}
int index = listBox1.IndexFromPoint(e.X, e.Y);
string s = listBox1.Items[index].ToString();
DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All);
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
lb_item = null;
}
and for to expand knowledge http://www.codeproject.com/Articles/5883/Two-ListBoxes-Drag-and-Drop-Example
Related
I am doing a memory matching game in c#
when the user matches 2 images i want them to disappear or make them invisible
i am still new to coding and this is what i did so far but the images wont be invisible
private void apple1_Click(object sender, EventArgs e)
{
apple1.Image = Properties.Resources.apple;
bool condition = true;
if (apple1.Image==apple2.Image)
{
apple1.Visible = false;
apple2.Visible = false;
}
}
private void apple2_Click(object sender, EventArgs e)
{
apple2.Image = Properties.Resources.apple;
}
As others have stated you'll need to store all of the Resources just once. Here is a possible example:
class MyForm
{
private Dictionary<String, Image> images = new Dictionary<String, Image>();
public void Init()
{
images["apple"] = Properties.Resources.apple;
}
public void Dispose()
{
foreach(var item in myDictionary.Values)
{
item.Dispose();
}
}
private void apple1_Click(object sender, EventArgs e)
{
apple1.Image = images["apple"];
bool condition = true;
if (apple1.Image==apple2.Image)
{
apple1.Visible = false;
apple2.Visible = false;
}
}
private void apple2_Click(object sender, EventArgs e)
{
apple2.Image = images["apple"];
}
}
When you set apple1.Image = Properties.Resources.apple; Note that a copy of the value (Image) of the Properties.Resources.apple is set to apple1.Image, same thing happens when you assign to apple2.Image, so although visually they seem to have the same image, but they are pointing to different images (in memory).
You may do one of following:
1-Set your images to a dictionary and then load apple1.Image and apple2.Image from it:
Dictionary<string, Image> Images = new Dictionary<string, Image>();
Images.Add("apple", Properties.Resources.apple);
Image apple = Properties.Resources.apple;
private void apple1_Click(object sender, EventArgs e)
{
apple1.Image = Images["apple"];
if (apple1.Image==apple2.Image)
{
apple1.Visible = false;
apple2.Visible = false;
}
}
private void apple2_Click(object sender, EventArgs e)
{
apple2.Image = Images["apple"];
}
2-Just use another property to compare equality:
private void apple1_Click(object sender, EventArgs e)
{
apple1.Image = Properties.Resources.apple;
apple1.Tag = "apple";
if ((string)apple1.Tag==(string)apple2.Tag)
{
apple1.Visible = false;
apple2.Visible = false;
}
}
private void apple2_Click(object sender, EventArgs e)
{
apple2.Image = Properties.Resources.apple;
apple2.Tag = "apple";
}
I have an object list view that has two text columns. When I edit the left column and hit tab to go to the right I receive a "ArgumentOutOfRangeException" with an index of -1. Looks like the index is something internal to the list view because I debugged my application and found no errors. Here is the code :
public partial class SummaryOverviewSettingsDlg : Form
{
private List<SummaryDataset> _localSummaryDatasets = new List<SummaryDataset>();
private bool _includeLimits;
private SummaryOverviewSettings _summaryOverviewSettings;
public bool IncludeLimits { get { return _includeLimits; } }
public SummaryOverviewSettingsDlg(SummaryOverviewSettings summaryOverviewSettings)
{
InitializeComponent();
if (summaryOverviewSettings.Datasets != null)
{
_localSummaryDatasets.AddRange(summaryOverviewSettings.Datasets);
}
_summaryOverviewSettings = summaryOverviewSettings;
}
private void DataFilesListDlg_Load(object sender, EventArgs e)
{
foreach(var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
LimitsCheckbox.Checked = _summaryOverviewSettings.ShowLimits;
}
private void OlvFilePaths_CellRightClick(object sender, CellRightClickEventArgs e)
{
var contextMenuSymbol = new ContextMenuStrip();
ToolStripItem item;
item = contextMenuSymbol.Items.Add("Add sample");
item.Click += ContextMenuAddFilePath;
if (e.Model != null)
{
contextMenuSymbol.Items.Add("-");
item = contextMenuSymbol.Items.Add("Delete sample");
item.Click += ContextMenuDeleteFilePath;
}
olvFilePaths.ContextMenuStrip = contextMenuSymbol;
}
private void ContextMenuAddFilePath(object sender, EventArgs e)
{
var item = new SummaryDataset()
{
SampleName = "Sample",
Path = "Path"
};
_localSummaryDatasets.Add(item);
// Rebuild the list in the GUI
olvFilePaths.ClearObjects();
foreach (var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
olvFilePaths.AutoResizeColumns();
}
private void ContextMenuDeleteFilePath(object sender, EventArgs e)
{
if (olvFilePaths.SelectedObject != null)
{
var item = (SummaryDataset)olvFilePaths.SelectedObject;
olvFilePaths.RemoveObject(item);
_localSummaryDatasets.Remove(item);
}
}
private void OlvFilePaths_CellEditFinished(object sender, CellEditEventArgs e)
{
if (e.Control is TextBox textBox)
{
var oldValue = (string)e.Value;
var newValue = (string)e.NewValue;
var col = e.Column.AspectName;
var index = e.ListViewItem.Index;
if (newValue != oldValue)
{
if (col == "SampleName")
{
_localSummaryDatasets[index].SampleName = newValue;
}
else
{
_localSummaryDatasets[index].Path = newValue;
}
}
}
// Rebuild the list in the GUI
olvFilePaths.ClearObjects();
foreach (var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
olvFilePaths.AutoResizeColumns();
}
private void OkButton_Click(object sender, EventArgs e)
{
_summaryOverviewSettings.Datasets.Clear();
_summaryOverviewSettings.Datasets.AddRange(_localSummaryDatasets);
_summaryOverviewSettings.ShowLimits = _includeLimits;
DialogResult = DialogResult.OK;
Close();
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void LimitsCheckbox_CheckedChanged(object sender, EventArgs e)
{
_includeLimits = LimitsCheckbox.Checked;
}
}
after selecting values from another listbox I save multiple listbox value into a single column of a table using linq to entity
protected void link1_Click(object sender, EventArgs e)
{
if (lb1.SelectedItem != null)
{
lb2.Items.Add(new ListItem(lb1.SelectedItem.Text, lb1.SelectedItem.Value));
lb1.Items.RemoveAt(lb1.SelectedIndex);
}
}
protected void link2_Click(object sender, EventArgs e)
{
if (lb2.SelectedItem != null)
{
lb1.Items.Add(new ListItem(lb2.SelectedItem.Text, lb2.SelectedItem.Value));
lb2.Items.RemoveAt(lb2.SelectedIndex);
}
}
protected void Button5_Click(object sender, EventArgs e)
{
string Skill = lb2.SelectedItem.Text;
employee e1 = new employee();
e1.emp_skill = Skill;
je.employee.AddObject(e1);
je.SaveChanges();
mv.ActiveViewIndex = 4;
}
Are you looking for something like this ? I iterate throught all elements of the listbox Id = "lb2"
protected void Button5_Click(object sender, EventArgs e)
{
for(var i=0;i<lb2.Items.Count;i++)
{
var e1 = new employee() { emp_skill = lb2.Items[i].Text };
je.employee.AddObject(e1);
}
je.SaveChanges();
}
if you only want the selected items in the listbox Id= "lb2" :
protected void Button5_Click(object sender, EventArgs e)
{
ListItem item = null;
for(var i=0;i<lb2.Items.Count;i++)
{
item = lb2.Items[i];
if (item.Selected)
{
var e1 = new employee() { emp_skill = item.Text };
je.employee.AddObject(e1);
}
}
je.SaveChanges();
}
I have this simple code in my DevExpress LookUp control (should be identical with a normal combobox)
private void lookUpUsers_EditValueChanged(object sender, EventArgs e)
{
lookUpRolesPréÉdit.EditValue = null;
}
private void lookUpRolesPréÉdit_EditValueChanged(object sender, EventArgs e)
{
lookUpUsers.EditValue = null;
}
The problem is that when I select a value in lookUpUsers, is resets the other lookup which then resets lookUpUsers. So when I pick a value, both combobox become null. What I want is that when you pick a value in combobox 1, combobox 2 resets its value.
How about this:
private void lookUpUsers_EditValueChanged(object sender, EventArgs e)
{
if(lookUpUsers.EditValue != null)
lookUpRolesPréÉdit.EditValue = null;
}
private void lookUpRolesPréÉdit_EditValueChanged(object sender, EventArgs e)
{
if(lookUpRolesPréÉdit.EditValue != null)
lookUpUsers.EditValue = null;
}
There might be an easier way than this, as my knowledge of C# is limited (especially their libraries like you are using them here). Nevertheless, this is an answer that uses no magic provided by libraries:
private bool localEdit = false;
private void lookUpUsers_EditValueChanged(object sender, EventArgs e)
{
if (!localEdit)
{
localEdit = true;
lookUpRolesPréÉdit.EditValue = null;
localEdit = false;
}
}
private void lookUpRolesPréÉdit_EditValueChanged(object sender, EventArgs e)
{
if (!localEdit)
{
localEdit = true;
lookUpUsers.EditValue = null;
localEdit = false;
}
}
Here's a solution I've come up with
private void lookUpUsers_EditValueChanged(object sender, EventArgs e)
{
if (!isEditFinished)
{
isEditFinished = true;
lookUpRolesPréÉdit.EditValue = null;
}
isEditFinished = false;
}
private void lookUpRolesPréÉdit_EditValueChanged(object sender, EventArgs e)
{
if (!isEditFinished)
{
isEditFinished = true;
lookUpUsers.EditValue = null;
}
isEditFinished = false;
}
How can I use a value from the reading function in the button1_Click function?
public void reading(object sender, EventArgs e)
{
DialogResult reading_from_folder = new DialogResult();
reading_from_folder = folderBrowserDialog1.ShowDialog();
if (reading_from_folder == DialogResult.OK)
{
string[] files_in_folder = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
...
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (string file in files_in_folder) // How do I access files_in_folder?
{
ListViewItem li = new ListViewItem(file);
}
}
You need to store it somehow, for example as a private member:
string some_value = null;
public void reading(object sender, EventArgs e)
{
some_value = "Foobar";
}
private void button1_Click(object sender, EventArgs e)
{
if (some_value != null)
{
// ...
}
}
// Make it a member variable
private string[] mFilesInFolder = null;
public void reading(object sender, EventArgs e)
{
DialogResult reading_from_folder = new DialogResult();
reading_from_folder = folderBrowserDialog1.ShowDialog();
if (reading_from_folder == DialogResult.OK)
{
mFilesInFolder = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
}
}
private void button1_Click(object sender, EventArgs e)
{
DoFileInFolderOperation();
}
private void DoFilesInFolderOperation()
{
if(mFilesInFolder != null)
{
foreach (string file in mFilesInFolder)
{
ListViewItem li = new ListViewItem(file);
}
}
}