Panel Drag and Drop not working in winForms Application - c#

I am developing a winForm application using c#.NET. On the form i have placed a panel, and i have 2 methods :
private void panel1_DragDrop(object sender, DragEventArgs e){
//some code here
}
private void panel1_DoubleClick(object sender, EventArgs e){
//some code here
}
Whenever i run the program in debug mode(x64), i cannot seem to hit panel1_DragDrop method. I have also tried using a breakpoint , but it is also not being hit.
I have put property AllowDrop=true for this panel, but still nothing is happening.
What could be the possible reason?

Did you tried DragEnter event :
private void panel1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
}
private void panel1_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (Path.GetExtension(files[0]).ToLower() == ".pdf")//jpg,bmp,docx,....
{
//Code
}
}
Also please check if your application isn't running as another user privilege like Run as Administrator.
"Run as Administrator" prevents drag and drop working.

Related

Why when I close a sub-form in a Windows forms Add-in on Revit , it closes the entire add-in?

I have a Windows Forms Add-in on Revit using Revit-API and C#. The project contains two forms ( Main form and About form). the About form opens by a button click in the main form. The problem is that when I try to close the About form through the Control box or by using a button inside it About.ActiveForm.Close() it automatically closes all the Forms. I tried About.ActiveForm.Hide() and also doesn't work. Also, I tried to link the Cancel Button of the about form to that button and it didn't work.
Code of initiating the About form
private void button3_Click(object sender, EventArgs e)
{
About abt = new About();
abt.ShowDialog(this);
}
Code of Closing the About Form
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
I also tried this for the closing part
private void button3_Click(object sender, EventArgs e)
{
Close();
}
and I tried this also for the initiating part
private void button3_Click(object sender, EventArgs e)
{
About abt = new About();
abt.ShowDialog();
}

Drag and Drop event no longer firing

I was having issues with my project with drag and drop no longer working.
I created a new simple project to isolate the issue. (based on this)
I simply created a listview (listView1), changed the background to blue. I then added 3 events from the TaskPaneControl designer which I then added a line of code as follows:
private void listView1_DragDrop(object sender, DragEventArgs e)
{
string test = "";
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
string test = "";
}
private void listView1_DragOver(object sender, DragEventArgs e)
{
string test = "";
}
I put a breakpoint in each line and expected it to be hit. The breakpoint will hit TaskPane_VisibleChanged but for some reason it won't hit the listview1_DragXXX
I made a change to the designer and in doing so I inadvertently set allowdrop on the user control be set to false. Once I set it to true it worked.

Bring custom control to front

I recently started to learn C# and right now I want to mess arouwnd with the Form[Design].
Right now I'm trying to BringToFront() custom controls each time I hover over a button (Ive got a few buttons close to each other, each time I hover over them I get a certain User Control).
This is what I've got so far:
private void button1_Hover(object sender, EventArgs e)
{
costumControl1.BringToFront();
}
private void button1_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
private void button2_Hover(object sender, EventArgs e)
{
costumControl2.BringToFront();
}
private void button2_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
private void button3_Hover(object sender, EventArgs e)
{
costumControl3.BringToFront();
}
private void button3_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
But bringing to front the user control costumControl0 when I hover from a button to another it's not what I want.
But I don't know how to go about this.
Is it possible to just add a if statement where I check if I'm not hovering the buttons close to my current one and then display the costumControl0.
Or a timer is necessary to delay the display of the costumControl0 and skip the command if I'm starting another event.
If the timer is needed, can I use one timer for all of the buttons or do I need to create one for each?
Or whats the best approach for this?

in c#, how can you disable a button after the 1st click, and enable it again after you click another button?

I cannot figure this out
i'm making a windows form application with visual basic in c#
i have a scan button and it scans everything in the folder and lists all of the files in the listbox
if you click it another time the list of files appear again
how can you make it so you can only press the scan button once, and then you can press it again if you click the browse button?
the browse button is to select the folder you want to scan
thanks
This is pretty trivial
private void ScanButtonClick(object sender, EventArgs e)
{
// do something
(sender as Button).Enabled = false;
}
private void BrowseButtonClick(object sender, EventArgs e)
{
ScanButton.Enabled = true;
}
Its a bit unclear if you're writing in C# or vb.net, but since the question is tagged as C#...
private void btnScan_Click(object sender, EventArgs e) {
btnScan.Enabled = false;
// other code here
}
private void btnBrowse_Click(object sender, EventArgs e) {
btnScan.Enabled = true;
//other code here
}
I tried this in my windows form application in C# and it works fine!
private void button3_Click_1(object sender, EventArgs e)
{
int count = 0;
count++;
//add your code here
if (count == 1) {
button3.Enabled = false;
//only one click allowed
}
}

How do I make a button show the listbox when clicked?

I am making a form where if a button is clicked it will go to my list box and run the functions I have in there though I am a bit confused on how to make it realise when the button has been clicked and for the listbox to work. Here is my code >_>
private void button1_Click(object sender, EventArgs e)
{
}
public void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
Ping.PlayConsole();
}
You just need:
private void button1_Click(object sender, EventArgs e)
{
Ping.PlayConsole();
}
It's ok to call the same function under different handlers.
Try this :
private void button1_Click(object sender, EventArgs e)
{
ListBox_SelectedIndexChanged(sender,e);
}
Good Luck !!
private void button1_Click(object sender, EventArgs e)
{
ListBox.Focus();
Ping.PlayConsole();
}
Both event handlers share the same signature void (object , EventArgs), so they are call-compatible.
If you are connecting the event visually using the form designer:
Go to the Property Inspector's Events pane and instead of double-clicking it to create an event handler stub for button1.Click, click the dropdown box icon that appears on the right side. Visual Studio will show all the event handlers present in the form that have a compatible signature, you should be able to choose ListBox_SelectIndexChanged for the button1.Click handler. They will be sharing the same handler.
If you are connecting the handler by code then this should work too:
ListBox1.SelectIndexChanged += new System.EventHandler(ListBox_SelectedIndexChanged);
button1.Click += new System.EventHandler(ListBox_SelectedIndexChanged);

Categories

Resources