I'm creating a custom context menu using a WebBrowser inside a WindowsFormsHost. But for some reason, the click event I assign to the menu item is not firing. Other events I assign are.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser();
browser.IsWebBrowserContextMenuEnabled = false;
System.Windows.Forms.ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem menuItem = new System.Windows.Forms.MenuItem()
{
Text = "Add comment"
};
// not firing
menuItem.Click += new EventHandler(menuItem_Click);
// is firing
menuItem.Select += new System.EventHandler(menuItem_Click);
contextMenu.MenuItems.Add(menuItem);
browser.ContextMenu = contextMenu;
// is firing
browser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
// is firing
browser.HandleCreated += new System.EventHandler(browser_HandleCreated);
windowsFormsHost.Child = browser;
browser.DocumentText = "Test";
}
void browser_HandleCreated(object sender, EventArgs e)
{
throw new NotImplementedException();
}
void browser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
throw new NotImplementedException();
}
void menuItem_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
What am I missing?
I know this question is old, but I just ran into the same problem. I fixed it by using a ContextMenuStrip instead of a ContextMenu.
System.Windows.Forms.ContextMenuStrip webBrowserMenu = new System.Windows.Forms.ContextMenuStrip();
System.Windows.Forms.ToolStripMenuItem refreshItem = new System.Windows.Forms.ToolStripMenuItem("Refresh");
refreshItem.Click += new System.EventHandler(refreshMenuItem_Click);
webBrowserMenu.Items.Add(refreshItem);
webBrowser1.ContextMenuStrip = webBrowserMenu;
Related
I make RichTextBox in TabControl:
private void newModuleToolStripMenuItem_Click(object sender, EventArgs e) {
TabPage tab = new TabPage();
RichTextBox richText = new RichTextBox();
string promptValue = ShowDialog("Input File Name", "File name");
tab.Text = promptValue;
tabControl1.Controls.Add(tab);
tabControl1.SelectTab(tabControl1.TabCount - 1);
richText.Parent = tabControl1.SelectedTab;
richText.Dock = DockStyle.Fill;
}
and I would to make event TextChange to this RichTextBox.
You can just add the following to newModuleToolStripMenuItem_Click code:
richText.TextChanged += RichText_TextChanged;
Then define the event handler:
private void RichText_TextChanged(object sender, EventArgs e)
{
// add your handling code here ...
}
Or you can make your event handler in lambda expression:
richText.TextChanged += (sender, e) =>
{
// add your handling code here ...
};
I am using Tabcontrol and I created a class for all new Tabpages.
For examle when I open a new Tabpage the class creates controls and places them.
bttn1 = new Button();
bttn1.Name = "button1";
bttn1.Text = "Start";
bttn1.Location = new Point(3, 405);
bttn1.Size = new Size(75, 23);
tp.Controls.Add(bttn1);
So my question is how can I check if this button is clicked?
Also my other question is the same with a Timer tick event.
You can easily attach to the button's Click event from the code:
bttn1.Click += new EventHandler(butt1_Click);
And here's the handler:
void button1_Click(object sender, EventArgs e)
{
// ...
}
Visual Studio will help you when you type the Click +=. After typing +=, hit the Tab key twice to get the handler.
I hope that you have created a UserControl for this or have sub-classed the TabPage class to create your controls. You should expose the Click event of the button from this newly created class through some new event you create:
public class MyTabPage : TabPage
{
private Button bttn1;
public event EventHandler Button1Clicked;
public MyTabPage()
{
bttn1 = new Button();
bttn1.Name = "button1";
bttn1.Text = "Start";
bttn1.Location = new Point(3, 405);
bttn1.Size = new Size(75, 23);
bttn1.Click += bttn1_Click;
this.Controls.Add(bttn1);
}
void bttn1_Click(object sender, EventArgs e)
{
OnButton1Clicked();
}
protected virtual void OnButton1Clicked()
{
var h = Button1Clicked;
if (h != null)
h(this, EventArgs.Empty);
}
}
Now when you create an instance of MyTabPage, you can attach a handler to the Button1Clicked event:
MyTabPage page = new MyTabPage();
page.Button1Clicked += page_Button1Clicked;
tabControl.TabPages.Add(page);
...
void page_Button1Clicked(object sender, EventArgs e)
{
}
I have a form with a tabControl and inside of each tab is a flowLayoutPanel where I can drag and drop files and a button is created for each dropped file. Afterwards when I click on a button, the file that i dropped should open. I have managed to do this for one file only.. My problem is how can I tell which button was clicked and to open the file/app stored in the path for each button.. How can I differentiate in the button_click event the clicked button and the path of the app to open?
Code for this part so far:
Process myProcess = new Process();
string path_app;
public Form1()
{
InitializeComponent();
this.DragEnter += new DragEventHandler(Form1_DragEnter);
this.DragDrop += new DragEventHandler(Form1_DragDrop);
}
void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
e.Effect = DragDropEffects.All;
}
void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
foreach (string s in fileList)
{
Button button = new Button();
button.Click += new EventHandler(this.button_Click);
flowLayoutPanel1.Controls.Add(button);
path_app = String.Format("{0}", s);
}
}
private void button_Click(object sender, System.EventArgs e)
{
myProcess.StartInfo.FileName =path_app;
myProcess.Start();
}
Also my tabControl has the possibility to add new tabs but how can I get the selected tab and the inside flowLayoutPanel to know where to create the button?
And by the way, is there a problem of how I open the files? I understood that i have to take into consideration the working directory..
Thank you for your help!
You can utilize Tag property of the Button:
void Form1_DragDrop(object sender, DragEventArgs e)
{
foreach (String s e.Data.GetData(DataFormats.FileDrop))
{
Button button = new Button();
button.Click += new EventHandler(this.button_Click);
flowLayoutPanel1.Controls.Add(button);
path_app = String.Format("{0}", s);
// Add to Tag any data you want to pin to the button
button.Tag = path_app;
}
}
private void button_Click(object sender, System.EventArgs e)
{
// Obtain via Tag
String path_app = ((sender as Button).Tag as String);
myProcess.StartInfo.FileName = path_app;
myProcess.Start();
}
You could use button.Tag = "theFancyPath" and in the EventHandler cast the object sender as Button to access the Tag property.
If you need more then you could inherit from Button:
public class ButtonWithPathProperty : Button
{
public FileInfo PathToOpen { get; private set; }
public ButtonWithPathProperty(FileInfo path)
{
PathToOpen = path;
this.Click += new EventHandler(this.button_Click);
}
private void button_Click(object sender, System.EventArgs e)
{
var yourPath = this.PathToOpen;
}
}
This is not tested btw :)
I have the following code but it is not working can anyone suggest me what changes i need to do in order to making it working .
Mainscroll.ManipulationCompleted += new EventHandler<System.Windows.Input.ManipulationCompletedEventArgs>(Mainscroll_completed);
private void Mainscroll_completed(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
{
UIElement target = sender as UIElement;
target.AddHandler(UIElement.ManipulationCompletedEvent, new EventHandler(layoutroot), true);
// throw new NotImplementedException();
}
private void layoutroot(object sender, EventArgs e)
{
MessageBox.Show("done");
}
Replace this line:
Mainscroll.ManipulationCompleted +=
new EventHandler<System.Windows.Input.ManipulationCompletedEventArgs>(Mainscroll_completed);
with this line:
this.Mainscroll.AddHandler(
Pivot.ManipulationCompletedEvent,
new EventHandler<ManipulationCompletedEventArgs>(Mainscroll_completed), true);
and remove:
target.AddHandler(UIElement.ManipulationCompletedEvent......
I am trying to combine several ContextMenu items into one. Currently, I am using separate MenuItem for it to work. Is there a way to combine all these MenuItems into one WHILE being able to control each of these event triggers when the users click on the different MenuItems?
NotifyIcon notifyIcon = new NotifyIcon();
System.Windows.Forms.ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem ChangeDetailsMenu = new System.Windows.Forms.MenuItem();
ChangeDetailsMenu.Text = "Change Contact Details";
ChangeDetailsMenu.Click += ChangeContactDetails;
System.Windows.Forms.MenuItem ChangeKinectAngleMenu = new System.Windows.Forms.MenuItem();
ChangeKinectAngleMenu.Text = "Change Kinect Angle";
ChangeKinectAngleMenu.Click += ChangeKinectAngle;
System.Windows.Forms.MenuItem exitMenu = new System.Windows.Forms.MenuItem();
exitMenu.Text = "Exit";
exitMenu.Click += ExitHandler;
contextMenu.MenuItems.Add(exitMenu);
contextMenu.MenuItems.Add(ChangeDetailsMenu);
contextMenu.MenuItems.Add(ChangeKinectAngleMenu);
Icon icon = new Icon("kse.ico");
notifyIcon.ContextMenu = contextMenu;
notifyIcon.Icon = icon;
notifyIcon.Visible = true;
private void ExitHandler(object sender, EventArgs e)
{
notifyIcon.Visible = false;
System.Windows.Application.Current.Shutdown();
}
private void ChangeContactDetails(object sender, EventArgs e)
{
}
private void ChangeKinectAngle(object sender, EventArgs e)
{
}