1

I am really struggling here being a newbie programmer... This has all been done in WPF by the way

I have three textboxes in my MainWindow.

The method which returns the values to these textboxes has been made public (and is within MainWindow). Below is one of the three textboxes.

 public float GetLEL()
    {
        bool LEL = false;
        float parsedLELValue = 0;
        LEL = float.TryParse(LEL_TextBox.Text, out parsedLELValue);
        return parsedLELValue;
    }

How can I return this value into my usercontrol class as is, even if it changes?

I have tried all sorts such as creating an instance within the Usercontrol (which haven't worked) -

Application app = new Application();


private float GetNewLEL()
        {
            float parsedNewLELValue = 0.00F;
            bool NewLEL = false;
            if (HolidayPay_CheckBox.IsChecked == false)
            {
                NewLEL = float.TryParse(app.GetHPR().ToString, out parsedNewLELValue);
            }

            else if (HolidayPay_CheckBox.IsChecked == true)
            {
                parsedNewLELValue = 0.00F;
            }

            return parsedNewLELValue;

        }

However, the instance in the Usercontrol is not finding the GetLEL() Method from within the MainWindow. Can someone please help. Someone else has suggested Get and Set but I am not sure of how to do this.

Ibrahiem Rafiq
  • 73
  • 2
  • 6
  • 16
  • "How can I return this value into my usercontrol class as is, even if it changes?" : can you clarify what you want to achieve exactly ? Whet do you mean by "my user control class" ? Bascilly you are going in the wrong direction, to retrieve the value of the your TextBox, you should use DataBinding : http://msdn.microsoft.com/en-us/library/ms752347.aspx – franssu Aug 28 '12 at 13:45
  • My usercontrol does some calculations based on the values in these textboxes which are on the mainwindow. The values of the textboxes can be changed by the user therefore even if they have been changed, they should be the ones to be used in the usercontrol. Databinding, how can I accomplish this? the information is far too complex for me, is there an easier way? – Ibrahiem Rafiq Aug 28 '12 at 13:49
  • "therefore even if they have been changed, they should be the ones to be used in the usercontrol". Can you explain one more time ? And yes, there is a lot of information to process, but practically it's not that hard and it is the way to go if you want a well designed application. Try googling "wpf simple databinding" for example. http://stackoverflow.com/questions/1725554/wpf-simple-textbox-data-binding – franssu Aug 28 '12 at 14:01
  • Right, I'll try and explain, at one point i am setting the values of these boxes to 0.00 based on a tickbox(in the usercontrol, however, when the tickbox is unticked I want it to revert to the textbox values, but it doesn't, it remains at zero. – Ibrahiem Rafiq Aug 28 '12 at 14:07
  • You definitely have to DataBind ;) – franssu Aug 28 '12 at 14:11
  • Thats's fine, can I databind a value from a textbox to a string / float within the other class? – Ibrahiem Rafiq Aug 28 '12 at 14:13

1 Answers1

0

I tried to reproduce what I understood you want to achieve ; if you have any question about this code I'd be pleased to answer.

MainWindow.xaml :

<Window x:Class="WpfApplication16.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <CheckBox IsChecked="{Binding MySuperCheckBoxIsChecked}" />

            <TextBox Text="{Binding MySuperFloatValue}" />

            <Button Click="Button_Click">Click me !</Button>

            <TextBlock x:Name="MySuperTextBlock"/>
        </StackPanel>
    </Grid>
</Window>

MainWindows.xaml.cs :

using System.Windows;

namespace WpfApplication16
{
    public partial class MainWindow : Window
    {
        private MySuperDataContextClass _mySuperDataContextClass = 
                                           new MySuperDataContextClass();

        public MainWindow()
        {
            InitializeComponent();

            this.DataContext =_mySuperDataContextClass;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (_mySuperDataContextClass.MySuperCheckBoxIsChecked)
            {
                MySuperTextBlock.Text = 
                   _mySuperDataContextClass.MySuperFloatValue.ToString();
            }
            else
            {
                MySuperTextBlock.Text = 0.0f.ToString();
            }
        }
    }
}

MySuperDataContextClass.cs :

using System.ComponentModel;

namespace WpfApplication16
{
    public class MySuperDataContextClass : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private bool _mySuperCheckBoxIsChecked;
        private float _mySuperFloatValue;

        public bool MySuperCheckBoxIsChecked
        {
            get { return _mySuperCheckBoxIsChecked; }

            set 
            {
                _mySuperCheckBoxIsChecked = value;

                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this,
                     new PropertyChangedEventArgs("MySuperCheckBoxIsChecked"));
                }
            }
        }

        public float MySuperFloatValue
        {
            get { return _mySuperFloatValue; }

            set 
            { 
                _mySuperFloatValue = value;

                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this,
                     new PropertyChangedEventArgs("MySuperFloatValue"));
                }
            }
        }
    }
}
franssu
  • 2,422
  • 1
  • 20
  • 29