|
my program is very simple. When you press spacebar it shows next file in directory in webBroser control that fills my whole form. The problem is when I click on the web form or the menu or another window and then comeback and hit spacebar, it doesn't work. I was thinking it lost focus, but after I set focus back to the form it still didn't pick up the event.
Also, how would I set my Form Width to that of the webpage or picture being displayed in the webBroswer control?
Thanks
|
|
|
|
|
The WebBrowser control is an ActiveX control and dispatches messages in its own thread, i.e. .NET will not respond to notification messages for that ActiveX control. If you set the focus back to the Form by clicking in a TextBox or a Button or something, then it actually has the focus and will process the key down and up messages.
To combat this, set Form.KeyPreview to true . This will handle certain notification messages (which the .NET FCL assemblies translate to events) before the control with the focus. This will still not help when the WebBrowser control has the focus.
As far as your last question goes, this really isn't possible. The width of a page or an image displayed in the WebBrowser control doesn't set the size of the WebBrowser control. Rather, the WebBrowser control sets the size.
Now, script in the page can set the size of the WebBrowser control (depending on security settings). You can declare the IDocHostUIHandler and override the ResizeBorder in order for your Form (the hosting control) to resize itself if script resizes the Window. See the article Using MSHTML Advanced Hosting Interfaces[^] for more details on this.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
Hi,
Does anyone know how to lock/unlock files in C#? I'm trying to lock an xml file while its open in the application, and unlock it when closed.
Thank you
Silver Bullet
|
|
|
|
|
See the FileStream class documentation in the .NET Framework. Specifically, the FileStream constructor has several overloads. Some of these require a FileShare enumeration where you can specify how to lock a file for sharing.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I am using a Context Menu on a treeview and needed to get a reference to the treenode that was selected, it is all done in the MouseUp method
Here is my code
<br />
private void TreeView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)<br />
{<br />
if (e.Button.ToString().ToUpper() == "RIGHT")<br />
{<br />
this.TreeView1.ContextMenu.Show(this.TreeView1,new Point(e.X,e.Y));<br />
this.TreeView1.SelectedNode = (this.TreeView1.GetNodeAt(e.X,e.Y));<br />
this.TreeView1.SelectedNode.BackColor = Color.FromName("HighLight");<br />
this.TreeView1.SelectedNode.ForeColor = Color.White;<br />
MessageBox.Show(this.TreeView1.SelectedNode.Text);<br />
<br />
--------------------------------------------------------------------------<br />
ADDED <br />
A better way to do this....<br />
<br />
this.TreeView1.ContextMenu.Show(this.TreeView1,new Point(e.X,e.Y));<br />
this.TreeView1.GetNodeAt(e.X,e.Y).BackColor = Color.FromName("HighLight");<br />
this.TreeView1.GetNodeAt(e.X,e.Y).ForeColor = Color.White;<br />
MessageBox.Show(this.TreeView1.GetNodeAt(e.X,e.Y).Text);<br />
<br />
} <br />
}<br />
--------------------------------------------------------------------------------<br />
<br />
I just ran into a situation where I had to get e reference to a TreeNode when a user right clicked a node.
You do not need to set the ContextMenu property of the treeview to get the ContextMenu to show up.
this.treeViewContextMenu.Show(this.TreeView1,new Point(e.X,e.Y));
Well I just thought that I would post this. Let me know what you think
William O'Malley
|
|
|
|
|
I'd say that if both work well, then choose the one you think works and looks best for your application. That's just my input.
Happy Programming and God Bless!
Internet::WWW::CodeProject::bneacetp
|
|
|
|
|
Sorry for the long decription...
Description:
I have a windows form which contains a ListView control. I'm automatically resizing the whole Form to fit the desktop. Basically, if the user dragged the form to the bottom of the desktop area, the ListView will shrink in size so that the whole window is still on the screen and none of the form extends past the Desktop bounds. To do this I have anchored the ListView control on all side and I change the Form's height during the LocationChanged Event, which in turn causes the ListView to change it's size due to the anchor. When the ListView shrinks the scrollbars are shown automatically which is what I want to occur. So everything works great up to this point.
Problem:
The problem occurs when I release the left mouse button after dragging the Form to the bottom of the desktop, the ListView Scrollbars disappear. It appears that the ListView is then resized past the bounds of the desktop, but I cannot be sure. When I click to drag the form the scroll bars reappear, but again disappear when I release the button.
Also, if I debug and step throught he code the scrollbars remain after the left mouse button is released....very odd
Any seems a similar problem ?
|
|
|
|
|
I *think* I know what it is, the LocationChanged Event is firing when I'm changing the size of the form from within the LocationChanged event handler, which is producing a recursion effect. Is there any way to stop the LocationChanged event from firing when I change the size of the form ?
|
|
|
|
|
Use state variables. If you programmatically change the location or size, set the state variable to some value, like true . In the LocationChanged event (and actually, if you're handler is in your Form class and is for that Form , override OnLocationChanged - calling base.OnLocationChanged - for better performance), if that state variable is set, don't execute your normal code but instead reset the state variable, like to false (if using a boolean).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I've tried this and I don't think its the answer:
protected override void OnLocationChanged(EventArgs e)
{
if(!SettingLocation)
{
if(frmFav!=null && frmFav.Visible)
{
timFavMover.Enabled=true;
MoveIdle=0;
}
IdleTime=0;
SetPlayListHeight();
}
base.OnLocationChanged (e);
}
private void SetPlayListHeight()
{
try
{
if(lvPlayList.Items.Count==0)
return;
SettingLocation=true;
ListViewItem LastItem = lvPlayList.Items[lvPlayList.Items.Count-1];
Rectangle ItemRect = LastItem.GetBounds(ItemBoundsPortion.Entire);
Screen CurScreen = Screen.PrimaryScreen;
Rectangle Desktop = CurScreen.WorkingArea;
if(this.Bottom > Desktop.Bottom)
{
if(this.Height <= PlayerRect.Height)
{
this.Height=PlayerRect.Height;
return;
}
while(this.Bottom > Desktop.Bottom)
{
int BottomDiff = this.Bottom-Desktop.Bottom;
if(BottomDiff==0)
break;
this.Height-= BottomDiff;
if(this.Height <= PlayerRect.Height)
break;
}
}
else if(!IsItemVisible(LastItem) && this.Bottom < Desktop.Bottom)
{
while(!IsItemVisible(LastItem) && this.Bottom < Desktop.Bottom)
{
int BottomDiff=Desktop.Bottom-this.Bottom;
if(BottomDiff==0)
break;
if(ItemRect.Height > BottomDiff)
this.Height+=BottomDiff;
else
this.Height+=ItemRect.Height;
}
}
}
catch(Exception Err)
{
MessageBox.Show("SetPlayListHeight Error: "+Err.Message);
}
finally
{
SettingLocation=false;
}
Correct me if I'm wrong but,
everytime I set the location or size of my form programatically a LocationChanged event is added to the queue, these messages then stack up until I release the mouse button and the form's message pump can process the messages in the queue.
I tried adding Application.DoEvents() in my finally block but that caused the form to not resize at all.
Any suggestions ?
|
|
|
|
|
I have another question. I'm trying to loop through all the textboxes on a
web application. The snippet is below
//foreach(WebControl ctr in Page.Controls)
foreach(Control ctr in Page.Controls)
{
if(ctr is TextBox)
{
TextBox t = (TextBox)ctr;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}
Would you please tell me why it does not work?
|
|
|
|
|
caheo wrote:
Would you please tell me why it does not work?
What error do you get? Does it just not update the colours? or does it throw an exception? or just does not compile?
That information is usually an excellent start to figuring out why something does not work.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
It does not give any error. The reason I ask is there are quite a few TextBoxes on the GUI, after doing comparision at the if statement it skips and do the next iteration. Seems to me the if statement never reaches.
foreach(Control ctr in Page.Controls)
{
if(ctr is TextBox) <- it does comparision but never gets inside the if-statement
{
TextBox t = (TextBox)ctr;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}
Regards
|
|
|
|
|
Personally I would have written it like this:
foreach(Control ctr in Page.Controls)
{
TextBox t = ctr as TextBox;
if(t != null)
{
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
Are you sure your TextBox es aren't in other naming containers (like the Panel )? Just because all the TextBox es appear to be in the page, doesn't mean they're direct children of the page. You need to use a recursive methods like so:
private void SetReadOnly(Control parent, bool readOnly)
{
if (c != null)
{
foreach (Control child in parent.Controls)
{
if (child is TextBox)
{
TextBox tb = (TextBox)tb;
tb.BackColor = Color.AliceBlue;
tb.ReadOnly = readOnly;
}
SetReadOnly(child, readOnly);
}
}
} To call this, just pass the Page reference to this property:
SetReadOnly(Page, false); For more information, see the INamingContainer interface documentation in the .NET Framework SDK.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Since you don't say what happens or what doesn't happen or give any kind of error message (other than "it's broke"), it's difficult to say what you're problem is. I can take a guess and say that you might be having trouble with the if statement, so how about making your if statement look like this:
foreach(Control ctr in Page.Controls)
{
if(ctr.GetType().Equals(GetType(TextBox)))
{
TextBox t = (TextBox)ctr;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
There you go again, Dave!
GetType is a VB.NET operator equivalent to C#'s typeof operator.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
DAMN! I can't keep anything straight nowadays!
Now where did my mouse go...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
See what VB.NET is doing to you? Break away now or be doomed...
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You aren't looping through all the controls on the form, just the direct children of the Page. To get all, you would need to recursively search through the control. Also make sure you are doing this in the PreRender event so you can make sure that all the controls have been created.
The architect has placed his bets,
but the odds are long
-Poster Children
|
|
|
|
|
Hi, I need to know when the user press the period key in the numpad.
In the keys enumeration I have Keys.NumPad1 .... Keys.NumPad9.
Im using the keyup event and its e.Keycod argument.
What Keys enumeration value I shuold to use?
Gracias!
La realidad no es más que impulsos eléctricos del cerebro - Morpheus
|
|
|
|
|
This is documented, though not very clearly - so I offer another suggestion...
If you're not sure about something, a really quick method is to write a small test app. I threw the following together in under a minute:
using System;
using System.Windows.Forms;
public class Test : Form
{
static void Main()
{
Application.Run(new Test());
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
Console.WriteLine(e.KeyCode);
}
} Running it yields that the decimal point on the numpad is Keys.Decimal and that the period is Keys.OemPeriod . Writing simple test apps is very handy - something to keep in mind.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you. Its a good sugestion.
La realidad no es más que impulsos eléctricos del cerebro - Morpheus
|
|
|
|
|
I wanted to create my own Menu type class for use with a drop down button so that when you click the button a window can pop up that will contain some other controls. At first I thought I could do it with a stripped down form, but I realized that when I create the form it will always be activated and take focus from my main window (I don't want this to happen). So what I'm wondering is what are similar things like context menus or the dropdown portion of the combobox based on? Or is it possible to make a form that won't steal the focus?
Thanks,
Jesse
|
|
|
|