I would like to use the drop functionality of the WebBrowser control in C#. Unfortunately it doesn't work although I set the AllowWebBrowserDrop property to true.
For testing I wrote this little programm with just a textbox and a webbrowser control:
public Form1()
{
InitializeComponent();
webBrowser1.AllowWebBrowserDrop = true;
textBox1.Text = "http://www.google.com";
}
private void textBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
DoDragDrop(textBox1.Text, DragDropEffects.Link);
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
MessageBox.Show(e.Url.AbsoluteUri);
}
The DoDragDrop method gets executed correctly, but I never see the MessageBox appearing when dropping the string from the TextBox over the WebControl. Since the WebControl doesn't offer the usual drag & drop events I'm lost.
What do I have to do to make the url drop to the WebBrowser control work?
Use the following approach to initiate FileDrop dragging:
DataObject dObj = new DataObject();
var paths = new System.Collections.Specialized.StringCollection();
paths.Add(textBox1.Text);
dObj.SetFileDropList(paths);
textBox1.DoDragDrop(dObj, DragDropEffects.Link);
Related
so what i want to do is, on Event ButtonClick disable parent Control of this button.
In my Form i have several Panel's in which those button actually are.
I am using following code:
private void button1_Click(object sender, EventArgs e)
{
Control control = button1.Parent;
control.Enabled = false;
}
This xode is working fine, but I wanted to use this.Parent instead of button1.Parent, so that each button would be able to disable its own parent Panel(in this case will disabled Panel of button1).
When i am using this.Parent I get a System.NullReferenceException.
Knows some one why i am getting this error ?
this is your current class you want to use the sender and cast it to Control than get it's parent something like this
private void button1_Click(object sender, EventArgs e)
{
var uc = sender as Control;
uc.Parent.Enabled = false;
}
this represents the Window, which has no Parent.
You can do the following:
private void button_Click(object sender, EventArgs e){
Control control = ((Button) sender).Parent;
control.Enabled = false;
}
Button[] buttons = {button, button2....};
foreach (Button button in buttons)
button.Click += button_Click;
Alternatively, you can create a class inheriting from Button. You can then add a Click event handler that disables it's parent control. You can then use this button instead of the default one.
Learning WPF with MacDonald's "Pro WPF 4.5 in C#," focusing on Ch5, Events.
How would I write a generic event handler that works with both Labels and TextBoxes to process the MouseDown event initializing a drag & drop procedure?
Here is my TextBox handler:
private void tBSource_MouseDown(object sender, EventArgs e) {
TextBox tBox = (TextBox)sender;
DragDrop.DoDragDrop(tBox, tBox.Text, DragDropEffects.Copy);
}
And my Label handler:
private void lblSource_MouseDown(object sender, EventArgs e) {
Label lbl = (Label)sender;
DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Copy);
}
As you can see, I'm using the Content property and the Text property, depending on which object starts the event. If I try to use the same property for both senders, I get build errors (regardless of which I use). If I can avoid duplication, I would be very happy. Should I chunk a conditional out into another function and call that in the handler to determine what property should be used?
you could do something like this:
private void Generic_MouseDown(object sender, EventArgs e)
{
object contentDrop = string.Empty;
//Label inherits from ContentControl so it doesn't require more work to make work for all controls inheriting from ContentControl
if (sender is ContentControl contentControl)
{
//If you don't want to filter other content than string, you can remove this check you make contentDrop an object
if (contentControl.Content is string)
{
contentDrop = contentControl.Content.ToString();
}
else
{
//Content is not a string (there is probably another control inside)
}
}
else if (sender is TextBox textBox)
{
contentDrop = textBox.Text;
}
else
{
throw new NotImplementedException("The only supported controls for this event are ContentControl or TextBox");
}
DragDrop.DoDragDrop((DependencyObject)sender, contentDrop, DragDropEffects.Copy);
}
Let me know if you have any question
Is there a way to prevent users in clicking and typing in WPF WebBrowser? It appears to me that it is only possible to do this in WinForms.
Things that I've tried:
browser.IsEnabled = false; - didn't work, can still click (navigate) and type in text
browser.Focusable = false; - same
having overlay button, which would consume clicks and focus - WebBrowser is a special element, that is always on top of other elements
having another WebBrowser on top of the main one with blank page loaded and opacity set to 0% as an alternative to overlay button (3.) - WPF WebBrowsers do not properly handle opacity, didn't work
browser_MouseDown event with e.Handled = true; - the event is for some reason not called on mouse down
Is there something that I've missed or did wrong in my attempts?
Three events and Boolean did it for me.
bool BrowserIsLoaded = false;
private void Browser_LoadCompleted(object sender, NavigationEventArgs e)
{
BrowserIsLoaded = true;
}
private void Browser_Navigating(object sender, NavigatingCancelEventArgs e)
{
if(BrowserIsLoaded)
e.Cancel = true;
}
private void Browser_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (BrowserIsLoaded)
e.Handled = true;
}
When the browser has finished loading it triggers the LoadCompleted event. Set a Boolean then check that when trying to navigate to a new page when they try to type in a box.
If you don't want to use your own Boolean (I used it for other things so it made sense to me) you can just ask the browser if it's loaded when ever you need it:
private void Browser_Navigating(object sender, NavigatingCancelEventArgs e)
{
WebBrowser wb = (WebBrowser)sender;
if(wb.IsLoaded)
{
e.Cancel = true;
}
}
I want utilize a custom control(ui like a button) dynamic show a tree under it when user click it.And hide tree when custom control lost focus.
how to get it ?
(In addition can't use Form control.)
Create a control(tree) which is hidden from the beginning.
yourControl.Visible = false;
Create your button and your click eventhandler
yourButton.Click += yourButton_Click;
private void yourButton_Click(object sender, EventArgs e)
{
yourControl.Visible = true;
}
To hide when focus is lost, you need to create another method/eventhandler:
yourButton.LostFocus += yourButton_LostFocus;
void yourButton_LostFocus(object sender, EventArgs e)
{
yourControl.Visible = false;
}
I'm making Windows Form App in C# and best control for what I need is ActiveX Control (Calendar). The problem is that I need drag and drop but Control that I use does not have events for it (only positive thing is that it has property "AllowDrop"). (Control is Xtreme Calendar - Codejock)
I did somehow managed to do it. Using ListBox and it's events MouseDown (to get data with IndexFromPoint method) and MouseUp (to call Calendar's DoubleClick event).
private string name = string.Empty;
private void lstNames_MouseDown(object sender, MouseEventArgs e)
{
if (lstNames.Items.Count == 0)
name = string.Empty;
else
{
int index = lstNames.IndexFromPoint(e.X, e.Y);
name = lstNames.Items[index].ToString();
}
}
private void lstNames_MouseUp(object sender, MouseEventArgs e)
{
if (name != string.Empty)
CalendarControl_DblClick(name, null);
}