Why can't I get control Handle as a "Int" ??

D

dan2009temp

Hi all,

I need to get the handle of a PictureBox, as a Int value, I tried the
following -

int vHandle = System.Windows.Forms.PictureBox.Handle.ToInt32();

OR

int vHandle = Convert.ToInt32
(System.Windows.Forms.PictureBox.Handle);


But for the 2 of them I'm getting the Error message:

"An object reference is required for the nonstatic field, method, or
property"

What is the problem? why can't I get the value? I need to get the same
int value that I see if my mouse is standing/hovering over the
"Handle" word (if I set it manualy through the watch window then the
next command that uses this int handle is working just fine).

Thanks!
 
J

Jeff Gaines

On 27/01/2009 in message
<8a5773f9-a60d-4094-b888-de552bd39917@q30g2000prq.googlegroups.com>
Hi all,

I need to get the handle of a PictureBox, as a Int value, I tried the
following -

int vHandle = System.Windows.Forms.PictureBox.Handle.ToInt32();

OR

int vHandle = Convert.ToInt32
(System.Windows.Forms.PictureBox.Handle);


But for the 2 of them I'm getting the Error message:

"An object reference is required for the nonstatic field, method, or
property"

What is the problem? why can't I get the value? I need to get the same
int value that I see if my mouse is standing/hovering over the
"Handle" word (if I set it manualy through the watch window then the
next command that uses this int handle is working just fine).

Thanks!

You need to replace 'PictureBox' with the instance of the PictureBox you
use in your application.
 
A

Alberto Poblacion

I need to get the handle of a PictureBox, as a Int value, I tried the
following -

int vHandle = System.Windows.Forms.PictureBox.Handle.ToInt32();

OR

int vHandle = Convert.ToInt32
(System.Windows.Forms.PictureBox.Handle);


But for the 2 of them I'm getting the Error message:

"An object reference is required for the nonstatic field, method, or
property"

The problem is in the "System.Windows.Forms.PictureBox.Handle" part.
Handle is an instance property of the PictureBox class, and you are trying
to use it directly on the PictureBox class, like if it were a static
property, instead of invoking it on an _instance_ of PictureBox.

If you want the handle of a specific PictureBox, you need to use
something like "this.PictureBox1.Handle", where PictureBox1 is the name of
one of the PictureBoxes that you added to your Form.

Once you have gotten the Handle in this way, you can use its ToInt32()
method to convert it to an integer.
 
D

dan2009temp

I tried the following -

private void button1_Click( object sender, EventArgs e )
{
int vHandle = 0;

vHandle = Convert.ToInt32( this.PictureBox1.Handle );
MessageBox.Show( vHandle.ToString() );
}


but I'm getting an error message saying: " 'WindowsApplication1.Form1'
does not contain a definition for 'PictureBox1' "

What next ?

Thanks.
 
M

Mel Weaver

private void button1_Click( object sender, EventArgs e )
{
int vHandle = 0;
PictureBox pictureBox = new PictureBox();
vHandle = Convert.ToInt32( pictureBox.Handle );
MessageBox.Show( vHandle.ToString() );
}





I tried the following -

private void button1_Click( object sender, EventArgs e )
{
int vHandle = 0;

vHandle = Convert.ToInt32( this.PictureBox1.Handle );
MessageBox.Show( vHandle.ToString() );
}


but I'm getting an error message saying: " 'WindowsApplication1.Form1'
does not contain a definition for 'PictureBox1' "

What next ?

Thanks.
 
A

Alberto Poblacion

I tried the following -
[...]
vHandle = Convert.ToInt32( this.PictureBox1.Handle );
but I'm getting an error message saying: " 'WindowsApplication1.Form1'
does not contain a definition for 'PictureBox1' "

PictureBox1 was just an example. You have to replace that with the
ACTUAL NAME of the PictureBox that you previously added to your form (you DO
have a PictureBox whose handle you want to obtain, don't you?)

Also, note that instead of Convert.ToInt32, which does not accept a
Handle, you need to use
vHandle=this.TheNameOfYourPictureBox.Handle.ToInt32().
 
D

dan2009temp

Thanks all,

It was such a stupid mistake, I wrote -

"PictureBox1" ( big 'P' )

instead of -

"pictureBox1"


Thanks again.
Dan.
 
G

Göran Andersson

Jeff said:
Ctrl+Space is your friend.

Sometimes...

I have found myseld typing something like:

TextBox1.tex

and press ctrl+space to complete the name of the Text property. If you
type less than three letters it won't identify it uniquely and you get
the dropdown where you have to key down a few times to get to the right
one...

:p
 
J

Jeff Johnson

Sometimes...

I have found myseld typing something like:

TextBox1.tex

and press ctrl+space to complete the name of the Text property. If you
type less than three letters it won't identify it uniquely and you get the
dropdown where you have to key down a few times to get to the right one...

Well, yeah. Ctrl+Space = "Complete word IF POSSIBLE, otherwise display
Intellisense." (Ctrl+J, on the other hand, ALWAYS displays Intellisense.
Most people want the Ctrl+Space functionality most of the time.)
 
S

Stefan L

Not sure here, but I think Jeff's coming from VB... ;-)
Ctrl-J is Intellisense-shortcut for VB developers, in C# it's
Ctrl-Shift-Space.
 
J

Jeff Johnson

Not sure here, but I think Jeff's coming from VB... ;-)

Yes, originally, but...
Ctrl-J is Intellisense-shortcut for VB developers, in C# it's
Ctrl-Shift-Space.

Ctrl+Shift+Space is mapped to Edit.ParameterInfo (basically "Show method
overloads") for me. Remember, there are several VS settings. You might be
running under "General Developer" while I'm running under "C# Developer," or
maybe vice-versa. All I know is that I originally used the VB settings, but
I switched to a different set probably more than 2 years ago.

Of course, I could have also remapped a few keystrokes to their VB6
equivalents.... Edit.ListMembers is mapped to several keystrokes for me (in
the Text Editor scope), like Ctrl+K, L and Ctrl+K, Ctrl+L as well as the
aforementioned Ctrl+J.

I'll be rebuilding a system soon. If I remember, I'll use a macro I have
which dumps all the VS commands and their keyboard shortcuts to see what a
virgin setup looks like. Then I'll know what I've manually changed.
 
S

Stefan L

Hi Jeff,

that was of course just a little joke. I mean, that's one of the best
aspects using VS that you can change all your keyb setting.
For myself, I also started with the VB settings and then switched some
this and that, decided on the next computer to go and try out the C#
settings, went nuts, switched back to VB which didn't work and then
remapped the most important settings manually. Eventually I have mixed
them all up now and thus am always unsure about what to press. And at a
colleague's pc there are of course diffrent settings, which brings you
back to point-and-click...

Why am I writing this: Your macro extracting the shortcuts caught my
attention. Where did you get it? I found one at
http://www.codinghorror.com/blog/archives/000412.html which is working
fine, but the output is missing some kind of readability (icons too
large). Is yours better?

Thanks,
Stefan
 
J

Jeff Johnson

Why am I writing this: Your macro extracting the shortcuts caught my
attention. Where did you get it? I found one at
http://www.codinghorror.com/blog/archives/000412.html which is working
fine, but the output is missing some kind of readability (icons too
large). Is yours better?

I wrote it myself, and it's probably uglier. It's pretty much a dump only a
program could love. After it writes to the Output window I copy and paste it
into Excel and pretty it up from there.

Here (pardon the crappy formatting; blame OE):

Imports System.Collections.Generic

Friend Class KeyboardBinding
Private _chord As String = String.Empty
Public Property Chord() As String
Get
Return _chord
End Get
Set(ByVal value As String)
If value Is Nothing Then
_chord = String.Empty
Else
_chord = value
End If
End Set
End Property

Private _keystroke As String
Public Property Keystroke() As String
Get
Return _keystroke
End Get
Set(ByVal value As String)
If value Is Nothing Then
_keystroke = String.Empty
Else
_keystroke = value
End If
End Set
End Property

Private _scope As String = String.Empty
Public Property Scope() As String
Get
Return _scope
End Get
Set(ByVal value As String)
If value Is Nothing Then
_scope = String.Empty
Else
_scope = value
End If
End Set
End Property

Public Sub New()
End Sub

Public Sub New(ByVal source As String)
If source Is Nothing Then
Throw New ArgumentNullException("source")
End If
Dim scopePos As Integer = source.IndexOf("::")
If scopePos > 0 Then
_scope = source.Substring(0, scopePos)
Dim chordPos As Integer = source.IndexOf(", ", scopePos + 2)
If chordPos > 0 Then
' Yes, this is a chord
_chord = source.Substring(scopePos + 2, chordPos - scopePos - 2)
_keystroke = source.Substring(chordPos + 2)
Else
' No, this is not a chord, just a bare keystroke
_keystroke = source.Substring(scopePos + 2)
End If
End If
End Sub
End Class

Function GetOutputWindowPane(ByVal Name As String, Optional ByVal show As
Boolean = True) As OutputWindowPane

' I stole this from the Samples.Utilities module because I don't know how to
reference other modules

Dim window As Window
Dim outputWindow As OutputWindow
Dim outputWindowPane As OutputWindowPane

window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)

If show Then window.Visible = True

outputWindow = window.Object

Try

outputWindowPane = outputWindow.OutputWindowPanes.Item(Name)

Catch e As System.Exception

outputWindowPane = outputWindow.OutputWindowPanes.Add(Name)

End Try

outputWindowPane.Activate()

Return outputWindowPane

End Function



Sub ShowCommands()

Dim out As OutputWindowPane = GetOutputWindowPane("Commands", True)

out.Clear()

Dim cmdList As New SortedDictionary(Of String, Command)

' This counter is needed to make duplicate command names unique in the
dictionary

Dim counter As Integer = 1

For Each cmd As Command In DTE.Commands

If cmd.Name.Length > 0 Then

cmdList.Add(cmd.Name & counter.ToString("00000"), cmd)

counter += 1

End If

Next

out.OutputString(String.Format("Name{0}Scope{0}Chord{0}Keystroke{1}", vbTab,
Environment.NewLine))

For Each cmd As Command In cmdList.Values

Dim shortcuts As New List(Of KeyboardBinding)

Dim bindingObj As Object() = CType(cmd.Bindings, System.Array)

If bindingObj.Length > 0 Then

For Each sc As Object In bindingObj

Dim kb As New KeyboardBinding(sc.ToString())

out.OutputString(String.Format("{0}{5}{1}{5}{2}{5}{3}{4}", _

cmd.Name, kb.Scope, kb.Chord, kb.Keystroke, Environment.NewLine, vbTab))

Next

Else

out.OutputString(String.Format("{0}{1}", cmd.Name, Environment.NewLine))

End If

Next

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top