Option selection box to change what appears in main window screen

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2156 Views - Last Post: 05 October 2014 - 09:55 AM Rate Topic: -----

#1 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Option selection box to change what appears in main window screen

Posted 17 September 2014 - 12:37 PM

Hi Guys

Right this is a bit of a confusing question for me but i will try and explain it as best i can.

Basically i have a 'HomeWindow' with a button 'OptionWindow'. My idea is that the 'OptionWindow' button will open up the 'OptionWindow'. Which will give the user three options 'Option1', 'Option2' and 'Option3'. If they pick 'Option1' then in the 'HomeWindow' in a stackpanel i want it to display 'UserControl1'. Where as if they pick 'Option2' then in the 'HomeWindow' in a stackpanel i want it to display 'UserControl2'.

What would be the best way for me to achieve this?

If you need some of my code that i have already then let me know what you need as i have a lot of it so can't paste it all in one go. But the new thing i need is the way of making the user-controls in the 'HomeWindow' change depending on what is selected in the 'OptionWindow'.

Thank You

Is This A Good Question/Topic? 0
  • +

Replies To: Option selection box to change what appears in main window screen

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15310
  • View blog
  • Posts: 61,396
  • Joined: 12-June 08

Re: Option selection box to change what appears in main window screen

Posted 17 September 2014 - 12:51 PM

Quote

Which will give the user three options 'Option1', 'Option2' and 'Option3'. If they pick 'Option1' then in the 'HomeWindow' in a stackpanel i want it to display 'UserControl1'

Do you want this to happen the moment they click the option, or when they click 'okay' (or how ever they leave the optionwindow)?

If it is the former then the 'raise event' would be investigated for that option form.. if the latter then you can have the options be properties on the form and when the option form closes pick them out of the object.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,250
  • Joined: 12-December 12

Re: Option selection box to change what appears in main window screen

Posted 17 September 2014 - 12:56 PM

Read Curtis's tutorial first:

How To Get Values From Form1 To Form2 (Or Any Other Forms).

It refers to WinForms but the principles are the same.
Was This Post Helpful? 0
  • +
  • -

#4 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: Option selection box to change what appears in main window screen

Posted 17 September 2014 - 01:03 PM

View Postmodi123_1, on 17 September 2014 - 08:51 PM, said:

Quote

Which will give the user three options 'Option1', 'Option2' and 'Option3'. If they pick 'Option1' then in the 'HomeWindow' in a stackpanel i want it to display 'UserControl1'

Do you want this to happen the moment they click the option, or when they click 'okay' (or how ever they leave the optionwindow)?

If it is the former then the 'raise event' would be investigated for that option form.. if the latter then you can have the options be properties on the form and when the option form closes pick them out of the object.


It can be either really. Depending on which is easier to be honest.

View Postandrewsw, on 17 September 2014 - 08:56 PM, said:

Read Curtis's tutorial first:

How To Get Values From Form1 To Form2 (Or Any Other Forms).

It refers to WinForms but the principles are the same.


That refers to passing a value though which i already have it doing elsewhere in my program and i tried doing it the same way but it got confusing for me with what i need to pass in the brackets (e.g. public HomeWindow(what goes here?){} and new HomeWindow(what goes here?)) and i had errors coming up everywhere.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,250
  • Joined: 12-December 12

Re: Option selection box to change what appears in main window screen

Posted 17 September 2014 - 01:11 PM

Passing (sharing) an object, or context, in the constructor is not the only approach discussed. You could subscribe to an event, possibly a Close event, of the form window. But the tutorial discusses the options in detail. See post #5 in the tutorial as well.

This post has been edited by andrewsw: 17 September 2014 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15310
  • View blog
  • Posts: 61,396
  • Joined: 12-June 08

Re: Option selection box to change what appears in main window screen

Posted 17 September 2014 - 01:21 PM

I am partial the the latter. It jives more with the nature of object orientated programming.

Keeping the idea of scope in mind, and public/private access option, you can create a form that can have data pulled out of it after it closes.

Example:
Public Class Form13

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim foo As New Form14
        foo.ShowDialog() '-- makes it modal so processing waits until the form14 relequnishes control.

        MessageBox.Show(foo.GetText) '-- the variable 'foo' is still active and in scope.. so you can access public methods/properties like any other object.

    End Sub
End Class



Public Class Form14

    '-- the form has a public way to return the text in the textbox.
    Public ReadOnly Property GetText As String
        Get
            Return TextBox1.Text
        End Get
    End Property

End Class

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,250
  • Joined: 12-December 12

Re: Option selection box to change what appears in main window screen

Posted 17 September 2014 - 01:30 PM

With a modal form, or window, you can check DialogResult.OK as well, in case they didn't actually make a choice, just cancelled or closed the dialog.

From Curtis's tutorial:
private void GetLastNameButton_Click(object sender, EventArgs e)
{
    LastNameForm lnf = new LastNameForm();
    DialogResult res = lnf.ShowDialog();
    if (res == DialogResult.OK)
        lastNameLabel.Text = lnf.LastName;
}


I haven't used a modal dialog in WPF but assume the process is similar.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Option selection box to change what appears in main window screen

Posted 17 September 2014 - 01:55 PM

Another thought...

Have each of the userControls as part of a collection.
Have the options be enumerations that correspond:
Enum 0 = collection[0]
Enum 1 = collection[1]
and so on

Bind the content of the stackpanel to the collection through a custom converter based on the enum property

Now any time the property holding the enum is changed, be it from a button, a menu, a gesture... Any WPF command... The content of the stackPanel will update.
Was This Post Helpful? 1
  • +
  • -

#9 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Option selection box to change what appears in main window screen

Posted 17 September 2014 - 08:43 PM

I believe the DialogResult in WPF returns a nullable bool, but I'm on my phone right now and can't check. Either way, the principle is the same.
Was This Post Helpful? 0
  • +
  • -

#10 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: Option selection box to change what appears in main window screen

Posted 24 September 2014 - 01:32 PM

Hey Guys

Right i have been trying to get this work but having a few issues.

I think my code for the 'Enquiry_OptionselectionWindow' is all fine or at least i'm not getting any errors with it so far.

For my 'HomeWindow' though i am getting: Cannot implicitly convert type 'System.EventHandler<WpfTrackingApplication.Mainwindow.NameUpdatedEventArgs>' to 'System.EventHandler', 'WpfTrackingApplication.Mainwindow.NameUpdatedEventArgs' does not contain a definition for 'Selection' and no extension method 'Selection' accepting a first argument of type 'WpfTrackingApplication.Mainwindow.NameUpdatedEventArgs' could be found (are you missing a using directive or an assembly reference?), 'WpfTrackingApplication.Mainwindow.NameUpdatedEventArgs' does not contain a definition for 'Selection' and no extension method 'Selection' accepting a first argument of type 'WpfTrackingApplication.Mainwindow.NameUpdatedEventArgs' could be found (are you missing a using directive or an assembly reference?)

Also in one of my other windows i am getting: 'WpfTrackingApplication.HomeWindow' does not contain a constructor that takes 1 arguments
For this line: new HomeWindow(this);

Below is my code so far:

Enquiry_OptionselectionWindow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfTrackingApplication
{
    /// <summary>
    /// Interaction logic for Enquiry_Optionselectionwindow.xaml
    /// </summary>
    public partial class Enquiry_OptionselectionWindow : Window
    {
        public Enquiry_OptionselectionWindow(HomeWindow homewindow)
        {
            InitializeComponent();
        }

        #region Variables
        //Declare the variable 'Selection' to store what the user selects.
        public int Selection;
        #endregion

        //Set selection to 1
        public void ShowScreen_CreateClient(object sender, RoutedEventArgs e)
        {
            Selection = 1;
        }

        //
        public class NameUpdatedEventArgs : EventArgs
        {
            public int Selection
            {
                get;
                set;
            }
        }

        public event EventHandler SendSelection;

        protected virtual void OnSendSelection(NameUpdatedEventArgs e)
        {
            if (SendSelection != null)
                SendSelection(this, e);
        }

        public void OnSendSelection(object sender, EventArgs e)
        {
            int newSelection = Selection;
            NameUpdatedEventArgs oss = new NameUpdatedEventArgs();
            oss.Selection = newSelection;
            OnSendSelection(oss);
        }
    }
}



HomeWindow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.ComponentModel;

namespace WpfTrackingApplication
{
    /// <summary>
    /// Interaction logic for Homewindow.xaml
    /// </summary>
    public partial class HomeWindow : Window, INotifyPropertyChanged
    {
        public HomeWindow(MainWindow mainwindow, Enquiry_OptionselectionWindow enquiry_osw)
        {
            InitializeComponent();
            
            //Executes the event to get the current user's name from the 'MainWindow'
            mainwindow.UserNameUpdated += new EventHandler<Mainwindow.NameUpdatedEventArgs>(MW_UserNameUpdated);
            enquiry_osw.SendSelection += new EventHandler<Mainwindow.NameUpdatedEventArgs>(MW_OSWSelectionWindowChanged);
        }

        //Code needed for 'PropoertyChanged' stuff
        public event PropertyChangedEventHandler PropertyChanged;
        void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #region Variables
        //Declare the variable 'CurrentUser' to store the current user's name.
        public string CurrentUser
        {
            get
            {
                return _CurrentUser;
            }

            set
            {
                if (_CurrentUser == value)
                    return;
                _CurrentUser = value;
                RaisePropertyChanged("CurrentUser");
            }
        }
        private string _CurrentUser;

        //Declare the variable 'CurrentUser' to store the current user's name.
        public string connection
        {
            get
            {
                return _connection;
            }

            set
            {
                if (_connection == value)
                    return;
                _connection = value;
                RaisePropertyChanged("connection");
            }
        }
        private string _connection;
        #endregion

        void MW_OSWSelectionWindowChanged(object sender, Mainwindow.NameUpdatedEventArgs e1)
        {
            if (e1 != null && e1.Selection != null)
                CurrentUser = e1.Selection;

        }

        //Code required for getting the current user's name and the SQL connection string from the 'MainWindow'
        void MW_UserNameUpdated(object sender, Mainwindow.NameUpdatedEventArgs e)
        {
            if (e != null && e.UserName != null && e.ConnectionString != null)
                CurrentUser = e.UserName;
                connection = e.ConnectionString;

        }

        //Show 'UserControl_Home'
        private void ShowScreen_Home(object sender, EventArgs e)
        {
            ContentPanel.Children.Clear();
            ContentPanel.Children.Add(new UserControl_Home());
        }

        //Show 'UserControl_CreateClient'
        public void ShowScreen_CreateClient(object sender, RoutedEventArgs e)
        {
            UserControl_CreateClient UC_CC = new UserControl_CreateClient(this);
            SendUserNameUpdated(sender, e);
            ContentPanel.Children.Clear();
            ContentPanel.Children.Add(UC_CC);
        }

        //Show 'UserControl_EditEnquiry'
        private void ShowScreen_EditEnquiry(object sender, RoutedEventArgs e)
        {
            UserControl_EditEnquiry UC_EE = new UserControl_EditEnquiry(this);
            SendUserNameUpdated(sender, e);
            ContentPanel.Children.Clear();
            ContentPanel.Children.Add(UC_EE);
        }

        //Show 'Enquiry_OptionselectionWindow'
        private void ShowScreen_EnquirySelection(object sender, RoutedEventArgs e)
        {
            UserControl_EditEnquiry UC_EE = new UserControl_EditEnquiry(this);
            SendUserNameUpdated(sender, e);
            Enquiry_OptionselectionWindow Enquiry_OSW = new Enquiry_OptionselectionWindow(this);
            Enquiry_OSW.Show();
        }

        //Code required for getting the current users name and the SQL connection string and passing it to the 'user controls'. Currently UserControl_CreateClient and UserControl_EditEnquiry.
        public class NameUpdatedEventArgs : EventArgs
        {
            public string UserName
            {get;set;}
            public string ConnectionString
            {get;set;}
        }

        public event EventHandler<NameUpdatedEventArgs> UserNameUpdated;

        protected virtual void OnUserNameUpdated(NameUpdatedEventArgs e)
        {
            if (UserNameUpdated != null)
                UserNameUpdated(this, e);
        }

        public void SendUserNameUpdated(object sender, EventArgs e)
        {
            string newUserName = CurrentUser;
            string newConnectionString = connection;
            NameUpdatedEventArgs nuea = new NameUpdatedEventArgs();
            nuea.UserName = newUserName;
            nuea.ConnectionString = newConnectionString;
            OnUserNameUpdated(nuea);
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Option selection box to change what appears in main window screen

Posted 26 September 2014 - 11:11 AM

Well, it seems that your problem is in part that you put the event code in the same form as the event handler code. That doesn't make any sense, why would you want something responding to it's own events? It can do that without events.
Was This Post Helpful? 0
  • +
  • -

#12 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: Option selection box to change what appears in main window screen

Posted 26 September 2014 - 11:13 AM

Which event are you referring to?
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Option selection box to change what appears in main window screen

Posted 27 September 2014 - 02:27 AM

View PostCurtis Rutland, on 26 September 2014 - 12:11 PM, said:

Well, it seems that your problem is in part that you put the event code in the same form as the event handler code. That doesn't make any sense, why would you want something responding to it's own events? It can do that without events.


I do that frequently. For example, my classes subscribe to their own INotifyPropertyChanged event. That way if any property value (other than "IsDirty") is changed the one handler can update the "IsDirty" property. Then also subscribe to "Saved" event and "Reverted" event to mark IsDirty as clean/false.

If I were to do this without subscribing to the events I would have to call MarkDirty() method from inside every property - that's sucky.
Was This Post Helpful? 0
  • +
  • -

#14 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Option selection box to change what appears in main window screen

Posted 27 September 2014 - 02:36 AM

Do you notice how you have several ShowScreen_blah(xxxx) methods that are nearly the same? When you see you have several nearly identical methods this is a hint that you could probably have one method replaces them all if you plan it right, and just include a parameter to do the small amount that is different. In other words, don't duplicate code

 //Show 'UserControl_EditEnquiry'
112	        private void ShowScreen_EditEnquiry(object sender, RoutedEventArgs e)
113	        {
114	            ShowScreen(new UserControl_EditEnquiry(this));               
118	        }


 //Show any passed in UserControl
112	        private void ShowScreen(UserControl yogi)
113	        {
115	            SendUserNameUpdated(sender, e);
116	            ContentPanel.Children.Clear();
117	            ContentPanel.Children.Add(yogi);
118	        }


Now when you have to add 15 more steps to show a control, you only have to do it in one place, rather than scrub through 500 lines of code where you copy/pasted it 90 times.
Was This Post Helpful? 0
  • +
  • -

#15 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: Option selection box to change what appears in main window screen

Posted 27 September 2014 - 03:38 AM

That 'showscreen_editenquiry' method is from my 'home window' and I cannot combine then into one method because I have buttons that are subscribed to those methods.

For example I have an 'edit enquiry' button subscribed to the 'showscreen_editenquiry' method.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2