C# Drag and Drop delete - c#

I was just wondering how I would go about Dragging an item from a list box and dropping it into an image that will then delete the value from the list box.
This is the code I have so far;
private void Form1_Load(object sender, EventArgs e)
{
textBox1.AllowDrop = true;
pictureBox1.AllowDrop = true;
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if ( listBox1.Items.Count > 0 )
listBox1.DoDragDrop( /* ? */ , DragDropEffects.Move); //This
statement is incomplete!
}
private void textBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
//Code to copy selected List Box entry to Text Box and remove original entry
}
private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
//Code to remove selected entry from List Box
}
Any help will be appreciated,
Many thanks

Related

c# dotnetbar two listboxadv sync scroll not working

i have two ListBoxAdv1 and ListBoxAdv2 and i want sync them scroll
i use this code and just scroll up or down but didn't update showing items in other ListBoxAdv
what should i do?
please help
i try this:
private void listBoxAdv1_Scroll(object sender, ScrollEventArgs e)
{
listBoxAdv2.VScrollBar.Value = listBoxAdv1.VScrollBar.Value;
}
private void listBoxAdv2_Scroll(object sender, ScrollEventArgs e)
{
listBoxAdv1.VScrollBar.Value = listBoxAdv2.VScrollBar.Value;
}
and this:
private void listBoxAdv1_Scroll(object sender, ScrollEventArgs e)
{
listBoxAdv2.Focus();
ScrollEventArgs scrollEventArgs = new ScrollEventArgs(ScrollEventType.SmallIncrement, e.OldValue, e.NewValue, ScrollOrientation.VerticalScroll);
listBoxAdv2_Scroll(listBoxAdv2, scrollEventArgs);
}
private void listBoxAdv2_Scroll(object sender, ScrollEventArgs e)
{
}
thanks for many responses!
i find solution:
bool Scrolling = true;
private void listBoxAdv1_Scroll(object sender, ScrollEventArgs e)
{
if (Scrolling == true)
{
Scrolling = false;
listBoxAdv2.BeginUpdate();
listBoxAdv2.AutoScrollPosition = new Point(listBoxAdv1.AutoScrollPosition.X, listBoxAdv1.AutoScrollPosition.Y);
listBoxAdv2_Scroll(sender, e);
listBoxAdv2.EndUpdate();
Scrolling = true;
}
}
private void listBoxAdv2_Scroll(object sender, ScrollEventArgs e)
{
if (Scrolling == true)
{
Scrolling = false;
listBoxAdv1.BeginUpdate();
listBoxAdv1.AutoScrollPosition = new Point(listBoxAdv2.AutoScrollPosition.X, listBoxAdv2.AutoScrollPosition.Y);
listBoxAdv1_Scroll(sender, e);
listBoxAdv1.EndUpdate();
Scrolling = true;
}
}

C# ListView Drag and Drop

I am trying to drag and drop a ListViewItem from one ListView to another in a Windows Form Application. I have two ListViews : lvImageFolder and lvWebServer. I am trying to drag from lvImageFolder to lvWebServer.
My code is shown below :
private void lvImageFolder_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
txtCursor.Text = Cursor.Position.ToString();
lvWebServer.DoDragDrop(lvImageFolder.Items[0].Text, DragDropEffects.Move);
}
}
private void lvWebServer_DragDrop(object sender, DragEventArgs e)
{
string dragToItem = e.Data.GetData(DataFormats.Text).ToString();
lvWebServer.Items.Add(dragToItem);
}
private void lvWebServer_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}
The issue I am having is that the string dragToItem is empty. I have tried passing in a serialized object to the DoDragDrop method also but this gave me nothing but issues.
Can anyone please shed any light on this issue?
Change the line below it works, but as adv12 wrote, copy always the first item:
lvWebServer.DoDragDrop(lvImageFolder.Items[0].ToString(), DragDropEffects.Move);
Change it to the code below to dragdrop the selected element:
lvWebServer.DoDragDrop(lvImageFolder.SelectedItem, DragDropEffects.Move);
Because you start drag and drop with list view item position [0], and possibly it's an empty item. You should start drag and drop with list view's selected items, for instance:
private void lvImageFolder_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//txtCursor.Text = Cursor.Position.ToString();
if (lvImageFolder.SelectedItems.Count > 0)
{
var lst = new List<string>();
foreach (ListViewItem item in lvImageFolder.SelectedItems)
{
lst.Add(item.Text);
}
lvWebServer.DoDragDrop(lst, DragDropEffects.Move);
}
}
}
private void lvWebServer_DragDrop(object sender, DragEventArgs e)
{
List<string> lst = e.Data.GetData(typeof(List<string>)) as List<string>;
foreach (var item in lst)
{
lvWebServer.Items.Add(item);
}
}
private void lvWebServer_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(List<string>)))
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}

Display dropdown list of comboBox when clicked/entered?

How to display the dropdown list of a comboBox when I click/enter it?
private void comboBox1_Click(object sender, EventArgs e)
{
}
or
private void comboBox1_Enter(object sender, EventArgs e)
{
}
Since you have not mentioned if this is a Web/Windows/WPF so I am suggesting to do this
Winforms
((ComboBox)sender).DroppedDown = true;
WPF
((ComboBox)sender).IsDropDownOpen = true;

How to stop GIF at specific Frames in winfoms

basically my question is how to stop GIF at specific frame.
i have a picture box that is already set its image with GIF and i created a 2 events Enter and Leave
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.LOGODEFAULT1;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.LOGOLEAVE1;
}
what i want is to stop the GIF at last frame(all gif's consist of 7 frames)
i tried the ImageAnimator and ImageStop
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
ImageAnimator.Animate(pictureBox1.Image, OnFrameChanged1);
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
ImageAnimator.StopAnimate(pictureBox1.Image, OnFrameChanged2);
}
private void OnFrameChanged1(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.LOGODENTER;
}
private void OnFrameChanged2(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.LOGOLEAVE1;
}
I think you can use the Image.SelectActiveFrame Method.

DragMove prevent preview mouse up

I have an application that I want to move the window the problem is that all images that had a preview mouse up now are not working.
This is the windo event:
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
and this is the image event:
private void image1_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("WTF IS WPF?");
}
If I remove the DragMove function the image event works.
why execute DragMove() all the time?
MouseButtonState _mouseButtonState;
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
_mouseButtonState = e.ButtonState;
}
private void Window_MouseMove(object sender, MouseEventArgs e)
{
if(_mouseButtonState == MouseButtonState.Pressed)
DragMove();
}
I would also put a check in image1_PreviewMouseUp
private void image1_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
if(_dragging) return;
//else do your preview
}

Categories