Results 1 to 13 of 13

Thread: Remind Window pos in VB.Net [Resolved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    Question Remind Window pos in VB.Net [Resolved]

    Greetings,

    how can I get the "last" windows possition in vb.net.
    I tried the example below but I get a error

    "E:\...\...\frmMain.vb(95): 'Option Strict On' lässt keine impliziten
    Konvertierungen von System.Object in System.Drawing.Point zu."

    in the line

    Dim pt As Point = xs.Deserialize(fs)

    I don't know why.
    (Where a the good old days of VB6)

    BTW I actually like to safe the Window Pos in the registry but
    what I read so far this is not recommendet.

    Thanks in advance
    Last edited by Bongo; Mar 29th, 2004 at 02:45 PM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Do it this way . Location encapsulate the "X" and "Y" of the form .
    VB Code:
    1. Dim position As Point = Me.Location

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395
    Thanks Pirate it works

    The code snipets looking now - but as you can see in the outputfile

    <?xml version="1.0"?>
    <Point xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <X>0</X>
    <Y>0</Y>
    </Point>>

    where is the size of the window????



    VB Code:
    1. Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    2.         'Prüfen ob App wirklich beendet werden soll
    3.         If MessageBox.Show(Application.ProductName & " schliessen?", Application.ProductName & " - Info", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.No Then
    4.             e.Cancel = True
    5.         End If
    6.  
    7.         Dim xs As New Xml.Serialization.XmlSerializer(GetType(Point))
    8.         Dim fs As New IO.FileStream("form.config", IO.FileMode.OpenOrCreate)
    9.         xs.Serialize(fs, Me.Location)
    10.         fs.Close()
    11.     End Sub
    12.  
    13.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    14.         mstrAppTitle = Application.ProductName & " " & Application.ProductVersion & " - (" & Application.StartupPath & ")"
    15.         Me.Text = mstrAppTitle
    16.  
    17.         If IO.File.Exists("form.config") Then
    18.             Dim xs As New Xml.Serialization.XmlSerializer(GetType(Point))
    19.             Dim fs As New IO.FileStream("form.config", IO.FileMode.OpenOrCreate)
    20.             Dim pt As Point = Me.Location
    21.             fs.Close()
    22.             Me.Location = pt
    23.         End If
    24.     End Sub

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I thought you're wanting to save the location of the form . Here's how to get the size of the form in a Size variable .
    VB Code:
    1. Dim siz As Size = Me.Size

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395
    Size and Location

    tried the t hing with the Me.Size but got error

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Bongo
    Size and Location

    tried the t hing with the Me.Size but got error
    What does the error say and where did you put that code ?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395
    the closing method looks like to work (see below) but
    the loading seems not to work.

    <?xml version="1.0"?>
    <Point xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <X>14</X>
    <Y>-1</Y>
    </Point><?xml version="1.0"?>
    <Size xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Width>996</Width>
    <Height>715</Height>
    </Size>

    I tried xsLocation.de
    VB Code:
    1. Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    2.            Dim xsLocation As New Xml.Serialization.XmlSerializer(GetType(Point))
    3.         Dim xsSize As New Xml.Serialization.XmlSerializer(GetType(Size))
    4.         Dim fs As New IO.FileStream("form.config", IO.FileMode.OpenOrCreate)
    5.         xsLocation.Serialize(fs, Me.Location)
    6.         xsSize.Serialize(fs, Me.Size)
    7.         fs.Close()
    8.     End Sub
    9.  
    10.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    11.            If IO.File.Exists("form.config") Then
    12.             Dim xsLocation As New Xml.Serialization.XmlSerializer(GetType(Point))
    13.             Dim xsSize As New Xml.Serialization.XmlSerializer(GetType(Size))
    14.             Dim fs As New IO.FileStream("form.config", IO.FileMode.OpenOrCreate)
    15.             Dim ptLocation As Point = xsLocation.Deserialize(fs) ' Me.Location
    16.             Dim ptSize As Size = xsSize.Deserialize(fs) ' Me.Size
    17.             fs.Close()
    18.             Me.Location = ptLocation
    19.             Me.Size = ptSize
    20.         End If
    21.     End Sub

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Try to put the load event code in the constructor if still doesn't work , try putting it in the OnLoad method of the form . btw , are you sure you're getting the correct values from the file ? You maybe need to check them using messagebox .

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395
    The values are correct writen to the file.

    the prob is in the load meth. see comments in code.

    The meaning of the msg in english is:
    Option Strict on does not allow a impliziert convercion of
    System.Objects in System.Drawing.point.

    VB Code:
    1. Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         If IO.File.Exists("formLocation.config") Then
    3.             Dim xsLocation As New Xml.Serialization.XmlSerializer(GetType(Point))
    4.             Dim fsLocation As New IO.FileStream("formLocation.config", IO.FileMode.OpenOrCreate)
    5. 'the next line gives the error
    6. 'E:\...\frmMain.vb(95): 'Option Strict On' lässt keine impliziten Konvertierungen von System.Object in System.Drawing.Point zu.
    7.             Dim ptLocation As Point = xsLocation.Deserialize(fsLocation) ' Me.Location
    8.             Me.Location = ptLocation
    9.             fsLocation.Close()
    10.         End If
    11.         If IO.File.Exists("formSize.config") Then
    12.             Dim xsSize As New Xml.Serialization.XmlSerializer(GetType(Size))
    13.             Dim fsSize As New IO.FileStream("FormSize.config", IO.FileMode.OpenOrCreate)
    14. 'the next line gives the error
    15. 'E:\...\frmMain.vb(95): 'Option Strict On' lässt keine impliziten Konvertierungen von System.Object in System.Drawing.Point zu.
    16.             Dim ptSize As Size = xsSize.Deserialize(fsSize) ' Me.Size
    17.             Me.Size = ptSize
    18.         End If

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Use DirectCast function that forces the conversion .
    VB Code:
    1. DirectCast(xsLocation.Deserialize(fsLocation), Point)
    Check file names , I think the chunk code inside the if structure isn't executed .

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395
    It works
    Thanks Pirate,

    I put the to working procedures _Closing and _Load below for
    the fellow developers with the same prob.

    Thanks again.

    BTW would it work if I put all in one file? and if yes how?

    VB Code:
    1. Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    2.         'Prüfen ob App wirklich beendet werden soll
    3.         If MessageBox.Show(Application.ProductName & " schliessen?", Application.ProductName & " - Info", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.No Then
    4.             e.Cancel = True
    5.         End If
    6.  
    7.         Dim xsLocation As New Xml.Serialization.XmlSerializer(GetType(Point))
    8.         Dim fsLocation As New IO.FileStream("fLocation.config", IO.FileMode.OpenOrCreate)
    9.         xsLocation.Serialize(fsLocation, Me.Location)
    10.         fsLocation.Close()
    11.  
    12.         Dim xsSize As New Xml.Serialization.XmlSerializer(GetType(Size))
    13.         Dim fsSize As New IO.FileStream("fSize.config", IO.FileMode.OpenOrCreate)
    14.         xsSize.Serialize(fsSize, Me.Size)
    15.         fsSize.Close()
    16.  
    17.     End Sub
    18.  
    19.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    20.         mstrAppTitle = Application.ProductName & " " & Application.ProductVersion & " - (" & Application.StartupPath & ")"
    21.         Me.Text = mstrAppTitle
    22.  
    23.         If IO.File.Exists("fLocation.config") Then
    24.             Dim xsLocation As New Xml.Serialization.XmlSerializer(GetType(Point))
    25.             Dim fsLocation As New IO.FileStream("fLocation.config", IO.FileMode.OpenOrCreate)
    26.             Dim ptLocation As New System.Xml.XmlTextReader(fsLocation)
    27.             Me.Location = DirectCast(xsLocation.Deserialize(ptLocation), Point)
    28.             fsLocation.Close()
    29.         End If
    30.         If IO.File.Exists("fSize.config") Then
    31.             Dim xsSize As New Xml.Serialization.XmlSerializer(GetType(Size))
    32.             Dim fsSize As New IO.FileStream("FSize.config", IO.FileMode.OpenOrCreate)
    33.             Dim ptSize As New System.Xml.XmlTextReader(fsSize)
    34.             Me.Size = DirectCast(xsSize.Deserialize(ptSize), Size)
    35.             fsSize.Close()
    36.         End If
    37.     End Sub

  12. #12
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I think you can write all these stuff in one file but you maybe need more code to handle xml nodes and such stuff(messy work ) . Well , if you're just saving the location and the size of the form , it's much better to save them in the registry . Notepad uses it too . 99% of the applications write their settings in the reg . I don't know why it's not recommended though ....

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395
    I think so as well, as i mentioned before I saw this now in a few
    postings. It is verry bad to write with VB.Net to the registry.
    (I was writing all the years to the reg and I and my clients
    been happy about this)

    But this is a new topic and I dont want to be guilty
    starting a holly war between the followers of this message and the others.

    What i think is a good approch is to write a methode and passing
    the values to this funktion.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width