|
I'm creating an application tht detects when USB drives are inserted and removed. I've overridden the WndProc method in a hidden form to trap the WM_DEVICECHANGE messages and then inspect the WParam for DBT_DEVICEARRIVAL, DBT_DEVICEQUERYREMOVE, and DBT_DEVICEREMOVECOMPLETE values.
However if the Drive leter is removed for a drive, the WM_DEVICECHANGE message is pumped but only with the DBT_DEVNODES_CHANGED parameter, which isn't unique or a reliable way to indentify this event.
Can anyone comment or make a suggestion please?
|
|
|
|
|
Hi,
I need to determine the parent class from which a particular subclass is derived. Could someone kindly explain to me how I can access that information please?
Regards,
Dave
Regards,
Dave
|
|
|
|
|
Well this is something you should know at design time, not in code. Why would you need to check in code when a class in C# can only have one parent class ? Can you better explain what you're trying to do ?
The is and as keywords let you do stuff like
if ( x is mybase)
which returns true if the object x is a mybase, either directly or as a parent.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
One way to do it is to examine the BaseType . For example:
myObject.GetType().BaseType...
|
|
|
|
|
if you have an instance of that class:
object unknown = ?;
Type unknownType = unknown.GetType();
Type toFind = unknownType.BaseType;
The variable Type will hold the type your class directly inherits from.
I hope this answers your question.
|
|
|
|
|
I'm working on some homework and need to create an application that draws 3 shapes when the form is clicked. The objects are suppose to be drawn on specified surface area within the form. I have the surface area being created but cannot seem to get a shape to be drawn onto it when clicked. I'm thinking that there is a problem with my event handling of the mouse click but I cannot seem to figure out how to fix it. I have included my code and would appreciate any help or direction that you guys could provide me.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace William_Cappoli_IT466_Unit_6_Project
{
public partial class frmMain : Form
{
private DrawImage imgDraw;
public frmMain()
{
imgDraw = new DrawImage();
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs paintEvnt)
{
Graphics gfx = paintEvnt.Graphics;
imgDraw.DrawBoundingBox(gfx);
}
private void frmMain_Click(PaintEventArgs e)
{
Graphics imgObj = e.Graphics;
imgDraw.Draw(imgObj);
}
}
}
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class DrawImage
{
private float xCoordinate = 0;
private float yCoordinate = 60;
private float width = 521;
private float height = 560;
private Color backcolor = Color.White;
public void DrawBoundingBox(Graphics g)
{
Pen myPen = new Pen(Color.Black);
SolidBrush myBrush = new SolidBrush(backcolor);
g.DrawRectangle(myPen, xCoordinate, yCoordinate, width, height);
g.FillRectangle(myBrush, xCoordinate, yCoordinate, width, height);
}
public void Draw(Graphics o)
{
Pen imgPen = new Pen(Color.Black);
o.DrawRectangle(imgPen, 90, 100, 45, 75);
}
}
Thanks in advance
|
|
|
|
|
SilentCodingOne wrote: private void frmMain_Click(PaintEventArgs e)
{
Graphics imgObj = e.Graphics;
imgDraw.Draw(imgObj);
}
Well, a click event does not recieve painteventargs, so I don't know how this is supposed to fit in. The way to force a paint event is to call Invalidate() on the form. If you do this in an actual click event, after setting up the box that needs to be drawn, then the paint event should be set up to draw the box or whatever you want.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Thanks I'm pretty new to C# and this is my first try at event handlers. I will make the update and see what happens.
|
|
|
|
|
The core thing to know is, if you change the parameters to an event handler, it ceases to be an event handler. It also won't get called just because you write it, it needs to be hooked up to the event. There is no way you can hook the code you posted to a click event.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Hello all!
My block of code in question is this:
while (WB.IsBusy)
System.Threading.Thread.Sleep(1);
WB is a System.Windows.Forms.WebBrowser.
My concern is that the WB object was created by my class and is in the same thread, so by sleeping I wonder if it will stop the WebBrowser from processing whatever its busy doing. So if the while ends up getting run 100 times did I just Sleep for 100 extra, unnecessary, milliseconds?
My reason for using the Sleep is so the processor usage doesn't peg out while waiting for the WebBrowser.
I imagine that most of the IsBusy time is spent waiting for content to download and that while the thread is sleeping Windows will keep loading the data into the stream, but I'm guessing and I have no idea how to check.
So, am I way off base here? Is there a better way to wait for a WebBrowser object to finish what its doing?
Thanks all!
|
|
|
|
|
I would look for an event fired by the web browser, I am sure there's one for when a page finishes loading. Then you can just catch that event and start processing from there.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Indeed. Thank you for the response!
For the automation I'm making I need it to be able to handle a site that may stop responding =/
My full loop code is:
DateTime start = DateTime.Now;
DateTime stop = start.AddSeconds(_SecondsToWait);
while (!DoneLoading() && DateTime.Now < stop)
{
System.Windows.Forms.Application.DoEvents();
System.Threading.Thread.Sleep(1);
}
DoneLoading is a delegate that returns true once the page has sufficently loaded.
So, the reason I keep running the loop is that I need to check if the page is done loading or if _SecondsToWait has already passed. I don't want it to wait for an event that may never fire. If there is another way to do this, I'd love to learn about it!
|
|
|
|
|
Anticast wrote: or if _SecondsToWait has already passed
What about a timer then?
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Anticast wrote: or the automation I'm making I need it to be able to handle a site that may stop responding =/
OK, so use a timer as well then, set to a nice long timeout, for pages that time out. This code is not what I was suggesting, it's still creating endless loops of wait events. Sleep(1) sleeps for a microsecond, 1/1000 of a second. This code will just call DoEvents with barely a pause.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Christian Graus wrote: Sleep(1) sleeps for a microsecond, 1/1000 of a second
1/1000 of a second is one millisecond. A microsecond is one millionth of a second I believe.
The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo
|
|
|
|
|
Oh, whatever. I doubt it would make much difference.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Hi All;;;
I want To make textbox accept only character and not number,
(Active only character key ,backspace key, and apace key)
and disactive all other keys.
please help me,,,
thanks all
|
|
|
|
|
|
Try this code:
private void textBox1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyValue >= 48 && e.KeyValue <= 57)
e.SuppressKeyPress = true;
}
Regards,
Lev
|
|
|
|
|
I wrote this code it is close to what you are trying to do.
I am not a C# programmer however so here it is in VB.NET
''' <summary>
''' This routine will not let the user enter anything except numbers and dashes into the SSN textbox
''' </summary>
''' <param name="e">This is the Keypressevent from the calling form</param>
''' <remarks></remarks>
Public Sub sSocialKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
' allow backspace
If Asc(e.KeyChar) = 8 Then
Exit Sub
End If
'Dont catch control
If Char.IsControl(e.KeyChar) Then
Exit Sub
End If
'numbers Only
If Char.IsNumber(e.KeyChar) Then
e.Handled = False ' and cancel this keystroke
Else
e.Handled = True
End If
End Sub
Private Sub txtSocialSecurityNumber_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSocialSecurityNumber.KeyPress
sSocialKeyPress(e)
End Sub
Use the C# equivalent of this for the text box you are wanting to validate.
I hope this helps, BTW I know I am not the best programmer so please noone laugh very hard. LOL
Humble Programmer
|
|
|
|
|
I wrote an article[^] that does exactly the reverse - only numeric. It's not difficult to adapt it. There are a couple of comments at the bottom of the article on other approaches that you might find usefult too.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
string[] arFiles = Directory.GetFiles(System.Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache));
int x = 0;
foreach (string f in arFiles)
MessageBox.Show(arFiles[x]);
The above code will only get "desktop.ini", I'm not surprised though, I know this's a special folder but do you have an idea how to reach a specific file and read it's content in this folder??
Thank you guys!
All generalizations are wrong, including this one!
(\ /)
(O.o)
(><)
|
|
|
|
|
Muammar© wrote: int x = 0;
foreach (string f in arFiles)
MessageBox.Show(arFiles[x]);
Huh? what is the purpose of f in the above snippet?
|
|
|
|
|
Okay okay I was so sleepy when I wrote this, I got it now, thanks Luc
All generalizations are wrong, including this one!
(\ /)
(O.o)
(><)
|
|
|
|
|
Muammar© wrote: foreach (string f in arFiles) MessageBox.Show(arFiles[x]);
What the ...??
How about looking at the file names and not the first element of an array over and over again.
foreach (string f in arFiles)
Debug.WriteLine(f);
|
|
|
|