Propper way to find out if I'm in DesignMode?????

M

M O J O

Hi,

I can't figure out how to use the DesigMode property. I've searched Google
and not found a soilid solution. Therefor I ask you - what is the propper
way to find out if I'm in DesignMode???

Here's my test code - I'm trying to inheriting the textbox, so when I place
it on my form, I want the backcolor of the texbbox to change to yellow....


Public Class MyTextBox
Inherits System.Windows.Forms.TextBox

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

If Me.DesignMode Then
Me.BackColor = System.Drawing.Color.Yellow
Else
Me.BackColor = System.Drawing.Color.Red
End If
End Sub

'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container()
End Sub

#End Region

End Class




The only solution I found was to use the function instead of DesignMode:

Private Function MeInDesignMode() As Boolean
MsgBox(Process.GetCurrentProcess.ProcessName)
Return Process.GetCurrentProcess.ProcessName.ToLower = "devenv"
End Function

..... but I don't think this is a solid solution.


Any ideas?????

Thanks!

M O J O
 
P

Peter Huang

Hi,

I think the DesignMode will work when we are in the design view of the IDE,
e.g. when we drag a usercontrol onto a form.
You may try as below.
1. Create a new usercontrol and handle the load event as below
Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Me.DesignMode Then
MsgBox("Now the control is in design mode")
End If
End Sub

2. Build the usercontrol
3. Create a new winform application and drag the usercontrol onto the form
4. at this time, the messagebox will be pop up which shows "Now the control
is in design mode".

But in runtime, i.e. when we run the winform application, the dialog will
not pop up.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

M O J O

Hi Peter,

No, the DesignMode always returns false.

Please look at my MyTextBox example.

Since I'm inheriting from a TextBox, I do not have a Load-event.

Kind regards,
M O J O
 
P

Peter Huang

Hi,

Yes, the DesignMode did not work in the contructor. Because the textbox did
not have onload method, we can detect the designmode in other function
after contructor.
You may try the code below in your userdefined textbox.
Protected Overrides Sub OnCreateControl()
MyBase.OnCreateControl()
If DesignMode Then
Me.BackColor = System.Drawing.Color.Yellow
Else
Me.BackColor = System.Drawing.Color.Red
End If
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang

Hi,

I am glad that that worked for you.
Cheers.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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