Why DragLeave event isn't fired when node dragged out of TreeView? - c#

As I read in other questions, mostly peoples asks why it firing unexpectedly, but for me it never fires at all.
using System;
using System.Windows.Forms;
namespace TestDrag
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
treeView1.ItemDrag += treeView1_ItemDrag;
treeView1.DragLeave += treeView1_DragLeave;
groupBox1.AllowDrop = true;
groupBox1.DragEnter += groupBox1_DragEnter;
groupBox1.DragDrop += groupBox1_DragDrop;
treeView1.Nodes.Add("asd");
treeView1.Nodes.Add("dsa");
}
void groupBox1_DragDrop(object sender, DragEventArgs e)
{
throw new NotImplementedException();
}
void groupBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
treeView1.DoDragDrop(e.Item, DragDropEffects.Copy);
}
void treeView1_DragLeave(object sender, EventArgs e)
{
MessageBox.Show(this, "This message never shows when node dragged out of treeView1", "This is bad", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
I want to hide TreeView once item dragged out, but this event never occurs >_<
What could be the reason and how to fix or bypass this?

You have to make AllowDrop = True;

Related

Richtextbox drag drop different than form drag drop

so I have a richtextbox and a form, and have attached a drag enter and drop event to them. The code for both of them are EXACTLY the same, I've made sure of it, but for some reason, the richtextbox event activates the Text Changed event. The form would activate the text changed event aswell, but I have a variable in the method that I call from the drag event that blocks it. I've tried to reproduce it on a different form, but the main kicker is that the code for the forms drag and drop events are EXACTLY the same as the richtextbox drag drop events. How could they possibly be different? Here is the drop drop code:
private void TestBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
private async void TestBox_DragDrop(object sender, DragEventArgs e)
{
string filePath = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
await AsyncStuff();
}
private async Task AsyncStuff()
{
block = true;
await OtherAsyncMethodThatActivatesTextChanged();
block = false;
}
private bool block;
private void TestBox_TextChanged(object sender, EventArgs e)
{
if (block) return;
MessageBox.Show("Text Changed called"); //Activates on the richtextbox drag, doesn't on the form
}
I am able to reproduce it now, here is the code. I attached the form drag drop and enter events through the designer.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
richTextBox1.AllowDrop = true;
richTextBox1.TextChanged += TestBox_TextChanged;
richTextBox1.DragDrop += TestBox_DragDrop;
richTextBox1.DragEnter += TestBox_DragEnter;
AllowDrop = true;
}
private void TestBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
private async void TestBox_DragDrop(object sender, DragEventArgs e)
{
string filePath = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
await AsyncStuff();
}
private async Task AsyncStuff()
{
block = true;
richTextBox1.AppendText("asd");
block = false;
}
private bool block;
private void TestBox_TextChanged(object sender, EventArgs e)
{
if (block) return;
MessageBox.Show("Text Changed called"); //Activates on the richtextbox drag, doesn't on the form
}
private async void Form1_DragDrop(object sender, DragEventArgs e)
{
string filePath = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
await AsyncStuff();
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
}

How to make a screen share application

I am trying to make a simple screen share application in C# and found this guide: https://www.c-sharpcorner.com/uploadfile/ulricht/how-to-create-a-simple-screen-sharing-application-in-C-Sharp/ and followed it but it doesn't work i tried it on the same computer and on two different PCs but nothing seems to work
//Host
public partial class ScreenShareHandler : Form
{
RDPSession x = new RDPSession();
public ScreenShareHandler()
{
InitializeComponent();
}
private void ScreenShareHandler_Load(object sender, EventArgs e)
{
}
private void Incoming(object Guest)
{
IRDPSRAPIAttendee MyGuest = (IRDPSRAPIAttendee)Guest;//???
MyGuest.ControlLevel = CTRL_LEVEL.CTRL_LEVEL_INTERACTIVE;
}
private void button1_Click(object sender, EventArgs e)
{
x.OnAttendeeConnected += Incoming;
x.Open();
}
private void button2_Click(object sender, EventArgs e)
{
IRDPSRAPIInvitation Invitation = x.Invitations.CreateInvitation("Trial", "MyGroup", "", 10);
textBox1.Text = Invitation.ConnectionString;
}
private void button3_Click(object sender, EventArgs e)
{
x.Close();
x = null;
}
}
//Client
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string Invitation = textBox1.Text;// "";// Interaction.InputBox("Insert Invitation ConnectionString", "Attention");
axRDPViewer1.Connect(Invitation, "User1", "");
}
private void button2_Click(object sender, EventArgs e)
{
axRDPViewer1.Disconnect();
}
}
As written in my comments:
Have you hooked up the eventhandlers correctly? If you click on the button in the designer you can go to the Events Tab in the Property-window and check if the Click-event points to the right eventhandler. Another way to check if the correct handler is used is to put a breakpoint inside each handler. Then debug and check if you get into the right method when you click the button. If not you didn't hook up the Eventhandlers correctly.

Show TextBox on every TabPage

I attached a TextBox to the first TabPage of a TabControl. I would like to display the same TextBox object on every TabPage. I tried to add the control to the tabControl Collection but unfortunately it's not working.
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox);
}
Button b;
public Form1()
{
InitializeComponent();
b = new Button() { Text = "Prueba" };
}
private void Form1_Load(object sender, EventArgs e)
{
AddButtonToTabControl();
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
AddButtonToTabControl();
}
public void AddButtonToTabControl()
{
tabControl1.SelectedTab.Controls.Add(b);
}
I missed two methods. It's working now!
tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);
void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox);
}

Add items from a listbox to another listbox on another form

Here is my code.
private void PlaceOrder_Click(object sender, EventArgs e)
{
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
I want to add what is in the menu box to a another list box on another form.
Update
(added by jp2code)
Form1 (Main):
namespace WindowsFormsApplication1 {
public partial class RESTAURANT : Form
{
double soup = 2.49;
double ordertotal;
public RESTAURANT()
{
InitializeComponent();
}
private void RESTAURANT_Load(object sender, EventArgs e)
{
}
private void Add_Click(object sender, EventArgs e)
{
MenuBox.Items.Add("Soup");
TotalBox.Items.Add(String.Format("{0:C}", soup));
ordertotal += soup;
total.Text = Convert.ToString(String.Format("{0:C}", ordertotal));
}
private void TotalBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void PlaceOrder_Click(object sender, EventArgs e)
{
new AreYouSure().Show();
this.Show();
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
}
}
Form2 (Confirmation)
namespace WindowsFormsApplication1 {
public partial class Confirmation : Form
{
public Confirmation()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Confirmation_Load(object sender, EventArgs e)
{
}
private void MenuBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
When clicking the 'Send Order' button the items from 'MenuBox' in form 1 need to be sent to the 'MenuBox' in form 2
OtherForm.OtherListbox.Items.Clear();
foreach(var itm in MenuBox.Items)
OtherForm.OtherListbox.Items.Add(itm);
Form1 (Main):
namespace WindowsFormsApplication1 {
public partial class RESTAURANT : Form
{
double soup = 2.49;
double ordertotal;
public RESTAURANT()
{
InitializeComponent();
}
private void RESTAURANT_Load(object sender, EventArgs e)
{
}
private void Add_Click(object sender, EventArgs e)
{
MenuBox.Items.Add("Soup");
TotalBox.Items.Add(String.Format("{0:C}", soup));
ordertotal += soup;
total.Text = Convert.ToString(String.Format("{0:C}", ordertotal));
}
private void TotalBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void PlaceOrder_Click(object sender, EventArgs e)
{
new AreYouSure().Show();
this.Show();
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
}
}
Form2 (Confirmation)
namespace WindowsFormsApplication1 {
public partial class Confirmation : Form
{
public Confirmation()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Confirmation_Load(object sender, EventArgs e)
{
}
private void MenuBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
When clicking the 'Send Order' button the items from 'MenuBox' in form 1 need to be sent to the 'MenuBox' in form 2
It is better that the controls on your other form (ListBox, in this case) are set to Private by default.
In that case, you would either need to set the control's visibility to Public (bad form, in my opinion) or create a method in your other form to accept the parameters from your form.
Consider something like this:
public void ListBoxData(object[] array)
{
listBox1.Clear();
listBox1.AddRange(array);
}
To get the data or selected item information back to your main form, you would likewise create another public object that you could check, like the property below:
public object SelectedItem { get { return listBox1.SelectedItem; } }
I hope that is what you were looking for.
UPDATE:
Using the code you supplied in the post below, I can see you do not have anything in your Confirmation form to send data to, much less a way to pass that information.
If you had a ComboBox, you could do something like follows:
public partial class Confirmation : Form
{
private ComboBox comboBox1;
public void AddRange(object[] array)
{
comboBox1.Items.AddRange(array);
}
}
That does not place the ComboBox anywhere on your form. You would need to work that out.
With that done, I'm guessing you need to edit your PlaceOrder_Click routine:
private void PlaceOrder_Click(object sender, EventArgs e)
{
//new AreYouSure().Show();
//this.Show();
using (var obj = new Confirmation())
{
var list = new List<object>(MenuBox.Items.Count);
foreach (var o in MenuBox.Items)
{
list.Add(o);
}
obj.AddRange(list.ToArray());
if (obj.ShowDialog(this) == DialogResult.OK)
{
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Items.Clear();
ordertotal = 0;
}
}
}
If you are struggling with this, you might need to look into some C# Windows "multiple forms" tutorials.
Here is a YouTube (that I did not sit all the way through): https://www.youtube.com/watch?v=qVVtCPDu9ZU

How to add data to the listbox by clicking Save Button :

How can i add the data to listbox1 by pressing the Save button.I have done the code of listbox1 but not of the button.Below is the code
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
listBox1.Items.Add(textBox2.Text);
listBox1.Items.Add(textBox3.Text);
listBox1.Items.Add(textBox4.Text);
}
}
}
Delete the listBox1_SelectedIndexChanged event and put the content to the button1_Click event.
It should look like this:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
listBox1.Items.Add(textBox2.Text);
listBox1.Items.Add(textBox3.Text);
listBox1.Items.Add(textBox4.Text);
}
}
}
And don't forget to remove the listBox1_SelectedIndexChanged event from the ListBox in the UI editor too.
If you want on button click the same thing to happen as on SelectedIndexChanged(), you just copy the code.
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
listBox1.Items.Add(textBox2.Text);
listBox1.Items.Add(textBox3.Text);
listBox1.Items.Add(textBox4.Text);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
/*listBox1.Items.Add(textBox1.Text);
listBox1.Items.Add(textBox2.Text);
listBox1.Items.Add(textBox3.Text);
listBox1.Items.Add(textBox4.Text);*/
}
If this is not it, you have to give more information about what should happen.

Categories

Resources