Click here to Skip to main content
15,900,110 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: CtypeDynamic : How can i detect if an expression can be converted to a specific Date format Pin
desanti25-Nov-18 3:05
desanti25-Nov-18 3:05 
GeneralRe: CtypeDynamic : How can i detect if an expression can be converted to a specific Date format Pin
Richard MacCutchan25-Nov-18 3:49
mveRichard MacCutchan25-Nov-18 3:49 
GeneralRe: CtypeDynamic : How can i detect if an expression can be converted to a specific Date format Pin
desanti25-Nov-18 3:53
desanti25-Nov-18 3:53 
GeneralRe: CtypeDynamic : How can i detect if an expression can be converted to a specific Date format Pin
Richard MacCutchan25-Nov-18 4:03
mveRichard MacCutchan25-Nov-18 4:03 
GeneralRe: CtypeDynamic : How can i detect if an expression can be converted to a specific Date format Pin
Eddy Vluggen25-Nov-18 3:55
professionalEddy Vluggen25-Nov-18 3:55 
GeneralRe: CtypeDynamic : How can i detect if an expression can be converted to a specific Date format Pin
desanti25-Nov-18 4:39
desanti25-Nov-18 4:39 
GeneralRe: CtypeDynamic : How can i detect if an expression can be converted to a specific Date format Pin
Eddy Vluggen25-Nov-18 5:54
professionalEddy Vluggen25-Nov-18 5:54 
QuestionMdiParent property reverts to Nothing after assigned to other MDIParent Pin
FedericoMui23-Nov-18 22:25
FedericoMui23-Nov-18 22:25 
Hello everybody. Very silly problem but it's driving me a bit crazy exactly because of its simplicity... Back to VB after 9 years so my apologies in advance if this will turn out being something very dumb. I'm blind to it... shame on me!

In short, the target is to manage multiple screens by placing an MDI container on each and allowing the user to move the mdiChildren from one Parent to another, so that they can be moved on different screens without losing MDI structure.
For the moment it's just academic, so we won't focus on how odd this could seem (it's a control application, not a document editor) or on the fact that one of the mdiParentForms will be a different class because it will be the main one with different controls on it.
As I said this is just a very rough test to check the basic principle.

I built a test project in which I create one instance per each screen in the Startup application event. Reference to all parent created is held in a list declared at a global level

Public Module Module1
    Public mps As New List(Of frmMdiParent)
End Module


Partial Friend Class MyApplication
        Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup

            For i As Integer = 0 To Screen.AllScreens.Count - 1
                Dim p As New frmMdiParent
                p.Location = Screen.AllScreens(i).Bounds.Location

                p.StartPosition = FormStartPosition.Manual
                p.WindowState = FormWindowState.Maximized
                p.Show()
                mps.Add(p)
            Next i

        End Sub 
End Class


Just for short, each instantiated mdiParent form creates one child of its own in the Load event.

Public Class frmMdiParent

    Private Sub frmMdiParent_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            mps.Remove(Me)
        Catch : End Try
    End Sub

    Private Sub frmMdiParent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim f As New frmChild
        f.MdiParent = Me
        f.Show()
    End Sub
End Class


Each child's caption is set to tickcount for visual disambiguation. Each child has a button named btnSwitch to cycle through parents (again, not refined... just base test).
On click I look for the very next mdiParent form in the list and change the form's mdiParent to that object (variable index is declared private at form level):


Private Sub btnSwitch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSwitch.Click
    'not very refined, first shot will be a dumb shot. 
    'Known, just a very rough test for testing the method
    If Index<= mps.Count - 2 Then
        Index += 1
    Else
        Index = 0
    End If

    Me.MdiParent = mps(Index)
End Sub


Till now everything looks working fine. I have two screens, on both a full-screen mdi parent is displayed and inside each an mdiChild is created, which actually belongs to it.
When I click the btnSwitch command, the child is moved to the other mdiParent, then if I click again it's moved back, and it's switching between the two parents correctly.
Both children originally created in each parent actually do work properly.

- BUT -

I then need to access the mdiParent from the child (e.g. for managing toolstrip items etc). It seems obvious to me to access me.MDIParent property. And... there is the surprise: if I reference from inside the child window class
me.MDIParent property, it returns NOTHING.
I checked every single step of the few lines, and the references returned by the list are valid.
Never nothing.
I added some code and some visual information to try to get a better view of the problem.
It turns out, when the child turns to another parent (which is successfully made by assigning me.MDIParent), then querying the same property return nothing! I changed the code to dumb-proof (I thought) debug, as follows (what I display on screen has been checked tens of times in the debug, but I just wanted to put it the most clear way)
The funny thing is:
I have an assignation instrucion Me.MdiParent = mps(index) and I checked a lto of times the reference mps(index) is valid, correct and not nothing. So I'm setting a NON NOTHING reference. By the way, the effect on screen is the expected one.
In the very next instruction I read the same property just assigned and it's NOTHING. WTF?!??
Public Class frmChild
    Dim index As Integer = 0
    Private Sub child_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        For i As Integer = 0 To mps.Count - 1
            If mps(i) Is Me.MdiParent Then index = i
        Next i
        Me.Text = Environment.TickCount  'Just for visual discrimination

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        'Added a second button to do something which references the parent.
        'as long as the form has just been created on startup in its original parent, 
        'everything works. Then the parent is alway snothing (even if returning to original one)
        '
        Me.MdiParent.WindowState = FormWindowState.Normal   '<-NullReferenceException 

    End Sub

    Private Sub btnSwitch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSwitch.Click

        'not very refined, first shot will be a dumb shot. 
        'Known, just a very rough test for testing the method, not a refined app
        If index <= mps.Count - 2 Then
            index += 1
        Else
            index = 0
        End If

        'Checked in debug, it's NEVER nothing. 
        'Just put a stop clause in case I was blind! Never Stopping, as expected
        If mps(index) Is Nothing Then Stop
        Me.MdiParent = mps(index)

        'Application.DoEvents()  //brought no benefits!

        'Just do prove myself I'm not getting confused. 
        'Yes, I just set the parent to something NOT noting and 
        'the mdiParent IS nothing! WTF??!!
        If Me.MdiParent Is Nothing Then Stop

        'AND THE FUN PART IS: the form actually keeps moving from one MDIContainer to another!!!

        'Show me visually whether it is nothing or not!
        Me.Label1.Text = If(Me.MdiParent Is Nothing, "NOTHING", "OK") 'After first click always displays NOTHING
        Me.Label2.Text = index   'always displaying correct index 

    End Sub
End Class

Tried in both VB2008/framework 3.5 and VS2017/Framework 4.6.1 under Win7/64/Pro
I can't believe I'm struggling with something that stupid! I'm sure it is something stupid, but I'm too blind to see it.Confused | :confused: Confused | :confused: Confused | :confused: (OK, there might be some workarounds, but I really want to understand what I'm doing wrong!) Am I missing something? Could anyone help please? Thanks a lot in advance.

modified 24-Nov-18 4:45am.

QuestionProblems verifying if an object exist on entity framework Pin
desanti19-Nov-18 6:09
desanti19-Nov-18 6:09 
AnswerRe: Problems verifying if an object exist on entity framework Pin
Richard Deeming19-Nov-18 6:15
mveRichard Deeming19-Nov-18 6:15 
AnswerRe: Problems verifying if an object exist on entity framework Pin
Eddy Vluggen19-Nov-18 6:21
professionalEddy Vluggen19-Nov-18 6:21 
GeneralRe: Problems verifying if an object exist on entity framework Pin
Mycroft Holmes19-Nov-18 10:59
professionalMycroft Holmes19-Nov-18 10:59 
GeneralRe: Problems verifying if an object exist on entity framework Pin
Eddy Vluggen19-Nov-18 13:26
professionalEddy Vluggen19-Nov-18 13:26 
GeneralRe: Problems verifying if an object exist on entity framework Pin
Mycroft Holmes20-Nov-18 11:05
professionalMycroft Holmes20-Nov-18 11:05 
GeneralRe: Problems verifying if an object exist on entity framework Pin
Eddy Vluggen20-Nov-18 11:09
professionalEddy Vluggen20-Nov-18 11:09 
QuestionEntity framework 6 : When to call Savechanges ? Pin
desanti17-Nov-18 1:10
desanti17-Nov-18 1:10 
AnswerRe: Entity framework 6 : When to call Savechanges ? Pin
Eddy Vluggen17-Nov-18 1:20
professionalEddy Vluggen17-Nov-18 1:20 
GeneralRe: Entity framework 6 : When to call Savechanges ? Pin
desanti17-Nov-18 7:33
desanti17-Nov-18 7:33 
GeneralRe: Entity framework 6 : When to call Savechanges ? Pin
Eddy Vluggen17-Nov-18 12:08
professionalEddy Vluggen17-Nov-18 12:08 
AnswerRe: Entity framework 6 : When to call Savechanges ? Pin
Dave Kreskowiak17-Nov-18 5:02
mveDave Kreskowiak17-Nov-18 5:02 
GeneralRe: Entity framework 6 : When to call Savechanges ? Pin
desanti17-Nov-18 7:41
desanti17-Nov-18 7:41 
AnswerRe: Entity framework 6 : When to call Savechanges ? Pin
Richard Deeming19-Nov-18 2:43
mveRichard Deeming19-Nov-18 2:43 
QuestionVb.net and Crystal Report Pin
Bj Molo15-Nov-18 1:38
Bj Molo15-Nov-18 1:38 
AnswerRe: Vb.net and Crystal Report Pin
Eddy Vluggen15-Nov-18 1:45
professionalEddy Vluggen15-Nov-18 1:45 
GeneralRe: Vb.net and Crystal Report Pin
Bj Molo15-Nov-18 1:52
Bj Molo15-Nov-18 1:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.