How do I add a mouseclick event to a winform treenode? - treenode

How do I add a mouseclick event to a winform treenode?
Update
Note that I want to do this at runtime.

To do this dynamically you need to handle the TreeView's NodeMouseClick event thusly:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
treeView1.NodeMouseClick +=
new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick);
treeView1.Nodes.Add(new TreeNode("Node 1"));
treeView1.Nodes.Add(new TreeNode("Node 2"));
}
void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
Console.WriteLine("Clicked: " + e.Node.Text);
}
}
}

Related

Unity Error CS1520: Method must have a return type

I'm trying to trigger the transition from an idle animation to a running animation when the "d" button is being pressed.
(A link to a tutorial covering a transition like this could also work).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IdleToJump : MonoBehaviour
{
bool keyHold = false;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (keyHold == true)
{
anim.Play("Run_Anim");
}
}
private void Key_up(object sender, KeyEventArgs e)
{
Key key = (Key)sender;
if (key == "d") {
keyHold = false;
}
}
private void Key_down(object sender, KeyEventArgs e)
{
Key key = (Key)sender;
if (key == "d")
{
keyHold = true;
}
}
}
Cross-check, you're code with this.
Form1 looks like a constructor that somehow got autogenerated. Just comment it out, and add the last two methods inside the class not outside.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IdleToJump : MonoBehaviour
{
bool keyHold = false;
//public Form1()//Not sure this constructor is relevant in class, so just comment it
//{
//InitializeComponent();
//}
private void timer1_Tick(object sender, EventArgs e)
{
if (keyHold == true)
{
anim.Play("Run_Anim");
}
private void Key_up(object sender, KeyEventArgs e)
{
Key key = (Key)sender;
if (key == "d") {
keyHold = false;
}
}
private void Key_down(object sender, KeyEventArgs e)
{
Key key = (Key)sender;
if (key == "d")
{
keyHold = true;
}
}
}
}

Windows application freezes most of the time on button click

I am working on a windows application which freezes most of the time on button click events on Home Page. Please find the code below for your reference. Thanks
using System;
using System.Windows.Forms;
namespace FileMigrationAgen
{
public partial class HomePage : Form
{
public HomePage()
{
InitializeComponent();
}
private void tableLayoutPanel4_Paint(object sender, PaintEventArgs e)
{
}
private async void button1_Click(object sender, EventArgs e)
{
SharepointMigration sharepointMigration = new SharepointMigration();
sharepointMigration.Show();
this.Hide();
}
private async void button2_Click(object sender, EventArgs e)
{
OneDriveMigration oneDriveMigration = new OneDriveMigration();
oneDriveMigration.Show();
this.Hide();
}
private void HomePage_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
}
}
I wouldn't recommend performing navigation in the manner that you have - hiding parent forms etc.
Have a look at this thread it has an example of using a static class to perform the navigation and keep track of the navigation stack.

How to Share Interface Implementation

I am facing a situation in which both the codebehind file of an ASPX master page and that of a regular ASPX page that does not use the master implement an interfance. The implementation is exactly the same.
Is it possible to make the two codebehinds share the implemenation instead of each has its copy of the same implementation? If yes, how should I approach this?
Thank you in advance for any insight.
John
How about using composition, on which both, the Master and the ASPX page have a reference to a class that implements the interface?
public interface IFace
{
int MyProperty { get; set; }
void MyMethod(string pVariable);
}
[Serializable]
public class ClassA:IFace
{
public ClassA()
{
}
#region IFace Members
public int MyProperty
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public void MyMethod(string pVariable)
{
throw new NotImplementedException();
}
#endregion
}
public partial class MasterPage : System.Web.UI.MasterPage
{
private ClassA IntefaceImplementor = new ClassA();
protected void Page_Load(object sender, EventArgs e)
{
}
}
public partial class _Default : System.Web.UI.Page
{
private ClassA InterfaceImplementor = new ClassA();
protected void Page_Load(object sender, EventArgs e)
{
}
}

Form Method on another thread not invoking the events

I am trying to achieve an update form.
I use a library to open a form when there is an updated file and download using edtFTPNet
In the form I pass the FTP object and start download, in FormLoad i handle two events and i use Thread to StartDownload(). My two events never invoking, i use them to set a progress bar.
public partial class UpdateProgressForm : XtraForm
{
public FTPConnection FtpConn { get; set; }
public string UpdateFileName { get; set; }
public UpdateProgressForm()
{
InitializeComponent();
}
private void OnLoad(object sender, EventArgs e)
{
FtpConn.Downloading += FileDownLoading;
FtpConn.BytesTransferred += FileBytesTransfered;
}
private void FileDownLoading(object sender, FTPFileTransferEventArgs e)
{
progressBar.Properties.Maximum = (int) e.FileSize;
}
private void FileBytesTransfered(object sender, BytesTransferredEventArgs e)
{
progressBar.Position = (int) e.ByteCount;
}
public void StartDownload()
{
FtpConn.DownloadFile(#".\" + UpdateFileName, UpdateFileName);
}
private void OnShown(object sender, EventArgs e)
{
Thread tt = new Thread(StartDownload) {IsBackground = true};
tt.Start();
}
}
Library method calling the Form:
private void DownloadUpdateFile(string updateFileName)
{
using (ProgressForm = new UpdateProgressForm { FtpConn = FtpConn, UpdateFileName = updateFileName })
{
ProgressForm.ShowDialog();
}
}
Any help? Thank you.
Take a look in the designer and make sure you subscribe to those events
Make sure you Instanciate and Show the from from the Main Thread.
Are you sure that the event handlers are not invoked? I think your problem rather is that you try to update the progress bar on the worker thread on which the event handlers are invoke (which is not the thread on which the GUI was created). You should make sure that the GUI updates are performed on the correct thread:
private void FileDownLoading(object sender, FTPFileTransferEventArgs e)
{
progressBar.Invoke((MethodInvoker) delegate
{
progressBar.Properties.Maximum = (int) e.FileSize;
});
}

How do you trigger an event across classes?

I am writing a Class Library that will be used by other applications. I am writing it in C#.NET. I am having a problem with triggering events across classes. Here is what I need to do...
public class ClassLibrary
{
public event EventHandler DeviceAttached;
public ClassLibrary()
{
// do some stuff
OtherClass.Start();
}
}
public class OtherClass : Form
{
public Start()
{
// do things here to initialize receiving messages
}
protected override void WndProc (ref message m)
{
if (....)
{
// THIS IS WHERE I WANT TO TRIGGER THE DEVICE ATTACHED EVENT IN ClassLibrary
// I can't seem to access the eventhandler here to trigger it.
// How do I do it?
}
base.WndProc(ref m);
}
}
Then in the application that is using the class library I will do this...
public class ClientApplication
{
void main()
{
ClassLibrary myCL = new ClassLibrary();
myCL.DeviceAttached += new EventHandler(myCl_deviceAttached);
}
void myCl_deviceAttached(object sender, EventArgs e)
{
//do stuff...
}
}
Probably the easiest option is to add a method to ClassLibrary which raises the event...i.e.
internal void RaiseDeviceAttached(object sender, EventArgs e)
{
if (DeviceAttached != null) DeviceAttached(sender, e);
}
Then, in OtherClass, simply call that method of ClassLibrary.
Another option is to go down the reflection route to trigger the event.