8 Replies - 776 Views - Last Post: 06 February 2013 - 04:31 PM Rate Topic: -----

#1 Bladewing51  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 78
  • Joined: 26-August 11

tracking variables across multiple windows with events inotify

Posted 04 February 2013 - 04:25 PM

This is actually a continuation of an ongoing issue I'm having but I think it should be in a new post as I'm taking a completely new path to get this feature working..

So this is what I have currently and what I'm trying to achieve

Application: A main window and a window that inserts data to the sql server.

Objective: 1) Track window open and closed status with a Boolean variable so that only 1 of that window opens at a time.
2) When Boolean variable changes to false as the window closes it fires an event to refresh my listbox. (Also not sure how I can call a sub from my "home" class so I can run the code to refresh my listbox)

Ive attempted to use a custom event and an implementation of inotify BUT when I use inotify it asks me to create a new instance of the class ive created on each window. This doesn't work for my situation as when I use the property on 1 window it doesn't seem to carry to the 2nd window or back to window 1 from that, I assume because its 2 different instances and not a single one that both objects interact with.

My MAJOR question is, is there a way for both windows to interact in the way I require, I was also thinking about creating a module with a public variable and some how hooking up an inotify class property to the variable but I haven't yet been able to make this work.

Heres my code so far.

Imports System.ComponentModel

Public Class INotifyclass
    Implements INotifyPropertyChanged

    Public Property windowopen As Boolean
        Get
            Return _windowopen
        End Get
        Set(value As Boolean)
            If _windowopen = value Then Exit Property
            _windowopen = value
            Me.onpropertychanged(New PropertyChangedEventArgs("Windowopen"))
            
        End Set
    End Property

    Private _windowopen As Boolean

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Protected Sub onpropertychanged(ByVal e As PropertyChangedEventArgs)
        RaiseEvent PropertyChanged(Me, e)
    End Sub
End Class




Imports System.Data.SqlClient
Imports System.Data
Imports TASKS.INotifyclass

Class Home

    Public WithEvents inote As New INotifyclass

    Private Sub Label_PreviewMouseLeftButtonDown_2(sender As Object, e As MouseButtonEventArgs)
        inote.windowopen = True
        MsgBox(inote.windowopen)
        Dim newwindow As Tasks = New Tasks
        newwindow.Show()




Imports System.Data.SqlClient
Imports System.Data
Imports TASKS.INotifyclass

    Public WithEvents inote As New INotifyclass

    Private Sub Label_PreviewMouseLeftButtonDown_1(sender As Object, e As MouseButtonEventArgs)

        inote.windowopen = True
        MsgBox(inote.windowopen)
        Me.Close()
    End Sub





Is This A Good Question/Topic? 0
  • +

Replies To: tracking variables across multiple windows with events inotify

#2 b2.f2  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 76
  • Joined: 21-May 07

Re: tracking variables across multiple windows with events inotify

Posted 04 February 2013 - 09:11 PM

By windows I hope you mean forms and if I understand it right you want to have access to all controls(listbox,textbox,etc) from another form and manipulate it then you can do like this:

Public Class Form1 'base form


    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim myForm2 As New Form2
        'myForm2.Show(Me) or myForm2.ShowDialog(Me)
        myForm2.Show(Me)

    End Sub

    Public Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        'do somthing
    End Sub
End Class



Another form:

Public Class Form2

    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim owner As Form1 = DirectCast(Me.Owner, Form1)
        '!!You will have access to public or friend declarations only!!  


        'change lable text in base form
        owner.test1.Text = "lol"

        'call event from base form
        owner.ListBox1_SelectedIndexChanged(sender, New System.EventArgs)

    End Sub
End Class


Quote

Objective: 1) Track window open and closed status with a Boolean variable so that only 1 of that window opens at a time.

What do you mean ? if you want to have 1 windows open at the time then use
newwindow. ShowDialog () in place of show.


Quote

When Boolean variable changes to false as the window closes it fires an event to refresh my listbox. (Also not sure how I can call a sub from my "home" class so I can run the code to refresh my listbox)


Now you can use above code in the close form event to refresh list box in base form.

This post has been edited by b2.f2: 04 February 2013 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 Bladewing51  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 78
  • Joined: 26-August 11

Re: tracking variables across multiple windows with events inotify

Posted 05 February 2013 - 02:12 PM

Nope didn't work but that's probably cause I for some reason forgot to put WPF in the title and it looks like your code is winforms if I'm not mistaken...

I get a value of type system.windows.window cannot be coverted to tasks.home on line 4 of your form 2 diagram.

I already do have a line of code for something else where I had to make form 2 own form 1 via the code

me.owner = application.current.mainwindow



With this I can call the sub I'm want but it gives me the ol reference to a non shared member requires an object reference, I'm just not sure what to reference??? ://>

This post has been edited by Bladewing51: 05 February 2013 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#4 b2.f2  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 76
  • Joined: 21-May 07

Re: tracking variables across multiple windows with events inotify

Posted 05 February 2013 - 04:56 PM

Indeed my codes are winforms but i it should work for WPF too, hmm okay try this:

Main Form
Dim newwindow As Tasks = New Tasks     
newwindow.Owner = Me
newwindow. ShowDialog ()



Child Form:

Dim mainWindow As MainWindow = TryCast(Owner, MainWindow)
mainwindow.listbox.clean() 'try if you have access to main form



Codes are not tested.

This post has been edited by b2.f2: 05 February 2013 - 04:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 Bladewing51  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 78
  • Joined: 26-August 11

Re: tracking variables across multiple windows with events inotify

Posted 06 February 2013 - 10:25 AM

I unfortunately got the same error when I wrote out line 2 of main form.

I did however search around and found something promising from someone called Olaf

    Private Sub test()
        Dim winsearch As String = GetType(Home).Name
        Dim win = (From w In Application.Current.Windows Where DirectCast(w, Window).GetType.Name = winsearch Select w).FirstOrDefault
        If win IsNot Nothing Then
            DirectCast(win, Home).loadtasks()

        End If
    End Sub



Now if I hook the gettype on the 2nd line to the window my page is hosted (substitute Home[page] for Mainwindow[window or form] I am able to execute any methods in form 1 from form 2.

The problem is tho that as I'm using a page to host all my controls and methods instead of hosting the controls within the ACTUAL window, it doesn't see the page or anything in it when I use application.current.window.

Anyone have any ideas how I can modify this code to access the page "Home" within the mainwindow.
Was This Post Helpful? 0
  • +
  • -

#6 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: tracking variables across multiple windows with events inotify

Posted 06 February 2013 - 12:20 PM

Whatever you do, I would avoid directly accessing other windows' UI controls. That's never a good idea. Why VB.NET insists on giving UI controls the Friend access modifier by default, rather than Private, I'll never understand (even with VB6 legacy), but that's besides the point...

Also, using a module with a public variable is usually a very bad idea also, so I would avoid that too.

Finally, I would generally also avoid casting the owner window (especially to a concrete type), if at all possible, as that instantly tightly couples your child window to your main window, and creates an unhealthy, semi-hidden dependency in the child window.

If all you need is to refresh the listbox when the second window closes, and prevent another instance of the second window from being opened when one is already open, can't you just do something similar to this:

Class MainWindow 'or Page?
    Private Property CanShowTasks As Boolean
        Get
            Return Me.btnShowTasks.IsEnabled
        End Get
        Set(value As Boolean)
            Me.btnShowTasks.IsEnabled = value
        End Set
    End Property

    Private Sub btnShowTasks_Click(sender As Object, e As RoutedEventArgs) Handles btnShowTasks.Click
        Me.ShowTasks()
        Me.CanShowTasks = False 'May be better to check if window is already open, and if it is, give it focus, rather than disabling the button.
    End Sub

    Private sub ShowTasks()
        Dim window As New Tasks()
        AddHandler window.Closed, AddressOf Me.tasksWindow_Closed
        window.Show()
    End Sub

    Private Sub tasksWindow_Closed(ByVal sender As Object, ByVal e As EventArgs)
        Me.RefreshListBox()
        Me.CanShowTasks = True
    End Sub

    Private Sub RefreshListBox()
        
    End Sub
End Class



You can use a similar technique for actions other than refreshing the listbox also (i.e. raise an event from the child window, and have the parent respond as it sees fit).

I'd also get rid of your INotifyclass, as it doesn't seem to be doing anything of value.

This post has been edited by CodingSup3rnatur@l-360: 07 February 2013 - 11:01 AM

Was This Post Helpful? 1
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: tracking variables across multiple windows with events inotify

Posted 06 February 2013 - 02:25 PM

Form can be potentially inherited from. Eg used as a base for another form.
Friend is accessible to the derived class.
Was This Post Helpful? 0
  • +
  • -

#8 Bladewing51  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 78
  • Joined: 26-August 11

Re: tracking variables across multiple windows with events inotify

Posted 06 February 2013 - 02:36 PM

Holy Crapple it works!

I also already deleted the inotify class earlier as it was a dead end solution I was testing at the time.

Thanks so much def a solution I will be using and playing with in the future
Was This Post Helpful? 0
  • +
  • -

#9 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: tracking variables across multiple windows with events inotify

Posted 06 February 2013 - 04:31 PM

No problem :)

View PostAdamSpeight2008, on 06 February 2013 - 09:25 PM, said:

Form can be potentially inherited from. Eg used as a base for another form.
Friend is accessible to the derived class.


True, but surely protected would be the better access modifier to use in that case. I don't want other classes in the same assembly (or any other assembly, for that matter) directly accessing the controls on my UI.

Further, I wouldn't want derived classes accessing my fields/controls directly either. That would almost be as bad as exposing the fields publicly, in my opinion. If I had to expose fields to derived classes (or, specific properties of the objects held in those fields), I'd want to do so through properties.

This post has been edited by CodingSup3rnatur@l-360: 07 February 2013 - 11:44 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1