MouseClick event handler doesn't work in ChromiumWebBrowser - c#

On other objects like the Forms itself MouseClick event works but when it comes to ChromiumWebBrowser the event handler simply doesn't listen any mouse clicks.
The browser is in a tab page. I tried to listen mouse clicks from there but that also didn't work.
private void ChromeBrowser_MouseClick(object sender, MouseEventArgs e)
{
Log("Click");
}
Designer.cs:
//
// chromeBrowser
//
this.chromeBrowser.ActivateBrowserOnCreation = false;
this.chromeBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
this.chromeBrowser.Location = new System.Drawing.Point(3, 3);
this.chromeBrowser.Name = "chromeBrowser";
this.chromeBrowser.Size = new System.Drawing.Size(1003, 539);
this.chromeBrowser.TabIndex = 0;
this.chromeBrowser.MouseClick += new System.Windows.Forms.MouseEventHandler(this.ChromeBrowser_MouseClick);
//

Related

WPF Raise DoubleClick Event

I have a class derived from WindowsFormsHost that listens to WinForms mouse events. It works fine for single clicks but is there any way to trigger double client events? ClickCount is read-only so I can't set it, and raising Control.MouseDoubleClickEvent doesn't propagate it. Any other idea?
private void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
MouseButton? wpfButton = ConvertToWpf(e.Button);
if (!wpfButton.HasValue)
return;
RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, wpfButton.Value) {
RoutedEvent = Mouse.MouseDownEvent,
Source = this
//ClickCount = 2 // read-only
});
//RaiseEvent(new RoutedEventArgs() { // won't propagate
// RoutedEvent = System.Windows.Controls.Control.MouseDoubleClickEvent,
// Source = this
//});
}
Here is you would raise a MouseDoubleClickEvent programmatically in WPF:
MouseButtonEventArgs doubleClickEvent = new MouseButtonEventArgs(Mouse.PrimaryDevice, (int)DateTime.Now.Ticks, MouseButton.Left);
doubleClickEvent.RoutedEvent = Control.MouseDoubleClickEvent;
doubleClickEvent.Source = this;
RaiseEvent(doubleClickEvent);

How to create click event for dynamically created user controls in C#?

I'm creating a Windows Universal App which contains a ListView filled with User-Controls. User-Controls are added to ListView dynamically during runtime, based on the elements from the database.
public void ShowFavorites()
{
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), (Application.Current as App).DBPath))
{
var Favorites = conn.Table<Favorites>();
lvFavorites.Items.Clear();
foreach (var fav in Favorites)
{
FavoriteItem favItem = new FavoriteItem();
favItem.Favorite = fav;
lvFavorites.Items.Add(favItem);
}
}
}
So how can i create an event that that triggers when the user-control is pressed?
When you create the control, all you need to do is simply link the control to a new event:
// Dynamically set the properties of the control
btn.Location = new Point((lbl.Width + cmb.Width + 17), 5);
btn.Size = new System.Drawing.Size(90, 23);
btn.Text = "Add to Table";
// Create the control
this.Controls.Add(btn);
// Link it to an Event
btn.Click += new EventHandler(btn_Click);
Then, when you (in this case) click on the newly added button, it will call your btn_Click method:
private void btn_Click(object sender, EventArgs e)
{
//do stuff...
}
I got it working even for items with diffrent text etc.
I gonna explain it based on a button beeing added to a panel.
You need to have a List<> in which you store the items, in this case buttons.
List<Button> BtList = new List<Button>();
and I also have a Panel in this case.
Panel PanelForButtons = new Panel();
Here is my code I hope it helps you:
void AddItemToPanel()
{
//Creating a new temporary item.
Button TempBt = new Button();
TempBt.Text = "Hello world!";
//Adding the button to our itemlist.
BtList.Add(TempBt);
//Adding the event to our button.
//Because the added item is always the last we use:
PanelForButtons.Controls.Add(BtList.Last());
BtList.Last().Click += MyButtonClicked;
}
And here is the event:
void MyButtonClicked(object sender, EventArgs e)
{
//First we need to convert our object to a button.
Button ClickedButton = sender as Button;
//And there we have our item.
//We can change the text for example:
ClickedButton.Text = "The world says: \"Hello!\"";
}

Change dynamic buttons text with textbox

Let me tell you what I want to do.
1.there are two button in my form.One of them let button when I click it.Another button finish to create button.
bool active = false;
private void button1_Click(object sender, EventArgs e)
{
active = true;
}
private void button2_Click(object sender, EventArgs e)
{
active = false;
}
2.I will create button with mousedown event I want to set location of this buttons with coordinate like this.
Button button_create;
private void frm_tr_MouseDown(object sender, MouseEventArgs e)
{
if (active)
{
button_create = new Button();
button_create.Location = new Point(e.X + 5, e.Y - 15);
button_create.Size = new Size(75, 30);
button_create.Text = "Button";
this.Controls.Add(button_create);
button_create.MouseClick += new MouseEventHandler(button_create_MouseClick);
}
}
3.I started mouseclickevent of my created button.When I click this created button I will create new form textbox and button.
TextBox button_text;
void button_create_MouseClick(object sender, MouseEventArgs e)
{
Form frm = new Form();
frm.Show();
button_text = new TextBox();
Button accept = new Button();
accept.Location = new Point(frm.Width / 2, frm.Height / 2);
frm.Controls.Add(button_text);
frm.Controls.Add(accept);
accept.MouseClick += new MouseEventHandler(accept_MouseClick);
}
4.I started mouseclickevent of my last created button.I want to change text of my first created button.
void accept_MouseClick(object sender, MouseEventArgs e)
{
button_create.Text = button_text.Text;
}
5.I will click button1 and I click anywhere of this form new button will create and I also click button2 to finish create new button.I can change my created button with textbox but
If I click button1 and I create a new one button I cant change text of previous created button How can I do that?

How to check click method when button is created from a class?

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)
{
}

Handle parents VisibleChanged event c#

I have created a C# usercontrol.
This usercontrol is hosted in a panel like so:
UserControlQuestion1 question1 = new UserControlQuestion1();
panel1.Controls.Add(question1);
panel1.Visible = true;
I want to add an event handler in my usercontrol to handle the panels VisibleChanged Event.
I have tried this which compiles correctly:
private void InitializeComponent()
{
this.Parent.VisibleChanged += new System.EventHandler(this.Parent_VisibleChanged);
But when I run my program the this.Parent is null because it hasn't been added to the parent panel yet I guess
How can I do this?
Making use of what you have so far, you could create a "Register Event" function in your user control...
void RegisterEvent()
{
this.Parent.VisibleChanged += new System.EventHandler(this.Parent_VisibleChanged);
}
which you can call after it has been added to the parent:
UserControlQuestion1 question1 = new UserControlQuestion1();
panel1.Controls.Add(question1);
question1.RegisterEvent();
panel1.Visible = true;
Set your VisibleChanged event handler after you create the control
UserControlQuestion1 question1 = new UserControlQuestion1();
panel1.Controls.Add(question1);
question1.Parent.VisibleChanged += new System.EventHandler(question1.Parent_VisibleChanged);
panel1.Visible = true;
OR
UserControlQuestion1 question1 = new UserControlQuestion1();
panel1.Controls.Add(question1);
panel1.VisibleChanged += new System.EventHandler(question1.Parent_VisibleChanged);
panel1.Visible = true;
You can try handling the ParentChanged event or override the OnParentChanged event raiser:
Control previousParent;
protected override void OnParentChanged(object sender, EventArgs e){
if(Parent != previousParent){
if(Parent != null) Parent.VisibleChanged += Parent_VisibleChanged;
if(previousParent != null) previousParent.VisibleChanged -= Parent_VisibleChanged;
previousParent = Parent;
}
}
Note that with the code above, you don't need code to register the Parent_VisibleChanged in the InitializeComponent.

Categories

Resources