Combo Box Error

LostNativ

Member
Joined
May 7, 2021
Messages
5
Programming Experience
Beginner
I am trying to create a WPF Watts Calculator. The user is to select the type of calculation from the combo box and then fill in the values click calculate and receive the result. However, I am getting a combo box error. I am not sure how to handle it. the error reads CS1061: 'type' does not contain a definition for 'name' and no accessible extension method 'name' accepting a first argument of type 'type' could be found (are you missing a using directive or an assembly reference?). The ms docs aren't to clear I am not quite following what is being said.

XAML
Xaml Cod:
<Window x:Class="S1P2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:S1P2"
        mc:Ignorable="d"
        Title="Watt's Law Calculator" Height="450" Width="624.82"
        WindowStartupLocation="CenterScreen">


    <Grid Margin="10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="326*"/>
            <ColumnDefinition Width="271*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="170*"/>
            <RowDefinition Height="0*"/>
            <RowDefinition Height="229*"/>
        </Grid.RowDefinitions>
       
        <Label x:Name="lblCalcType"
               Content="Choose calculation type"
               HorizontalAlignment="Left" Margin="49,50,0,0" VerticalAlignment="Top"
               Width="138" Height="26"/>
        <Label x:Name="lblFirstValue"
               Content="First Value" HorizontalAlignment="Left" Margin="49,134,0,0"
               VerticalAlignment="Top" RenderTransformOrigin="0.77,0.683" Width="103"/>
        <Label x:Name="lblSecondValue"
               Content="Second Value" HorizontalAlignment="Left" Margin="49,10,0,0"
               Grid.Row="2" VerticalAlignment="Top" Width="103"/>

        <Button x:Name="btnCalculate" Click="btnCalculate_Click" Content="Calculate" HorizontalAlignment="Left" Margin="49,117,0,0" VerticalAlignment="Top" Width="138" Height="36" Grid.Row="2"/>
        <Button x:Name="btnClear" Click="btnClear_Click" Content="Clear" HorizontalAlignment="Left" Margin="120.072,117,0,0" VerticalAlignment="Top" Width="100" Height="36" RenderTransformOrigin="0.952,0.432" Grid.Row="2" Grid.Column="1"/>
        <Label  x:Name="lblResult" Content="Result"  HorizontalAlignment="Left" Margin="49,55,0,0" Grid.Row="2" VerticalAlignment="Top" RenderTransformOrigin="-0.137,0.267" Width="66"/>
        <TextBox Name="txtFirstValue" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="120,138,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" RenderTransformOrigin="-0.016,1.503" TextChanged="txtFirstValue_TextChanged"/>
        <TextBox Name="txtSecondValue" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="120,22,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" TextChanged="txtSecondValue_TextChanged"/>
        <TextBox Name="txtResult" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="120,67,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" TextChanged="txtResult_TextChanged"/>

        <StackPanel Margin="0,0,112,0">
            <Menu Height="25" Margin="0,0,-358,0">
                <MenuItem Header="File" Height="25" Width="140">
                    <MenuItem Header="Close"/>
                </MenuItem>
            </Menu>
        </StackPanel>
        <ComboBox x:Name="cboCalcType" SelectedIndex="0" HorizontalAlignment="Left" Margin="68,54,0,0" VerticalAlignment="Top" Width="152" RenderTransformOrigin="0.065,1.304" Height="22" Grid.Column="1" SelectionChanged="CboCalcType_SelectionChanged">
            <ComboBoxItem Content="Power" HorizontalAlignment="Left" Width="150" Selected="ComboBoxItem_Selected"/>
            <ComboBoxItem  Content="Voltage" HorizontalAlignment="Left" Width="150" Selected="ComboBoxItem_Selected"/>
            <ComboBoxItem  Content="Current" HorizontalAlignment="Left" Width="150" Selected="ComboBoxItem_Selected"/>
        </ComboBox>


    </Grid>
</Window>


Code Behind:
Code Behind



namespace S1P2

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        public delegate float WattsLawDelegate(float val1, float val2);



        public MainWindow()

        {

            InitializeComponent();

            ComboBox cboCalcType = new ComboBox();

            cboCalcType.SelectedIndex = 0;

          

        

        }//end main window

    



        public float CalcPower(float current, float voltage)

        {

            //

            //Name                : float CalcPower(float current, float resistance)

            //Purpose            : Calculate the Power using Watt's Law

            //Re-use            : None

            //Method Parameters    : - float current

            //                      - current value in amps

            //                    - float voltage

            //                      - voltage value in volts

            //Output Type       : float

            //                    - the calculated Power value

            //

            float power = 0;

            try

            {

                power = current * voltage;

            } // end try

            catch (Exception)

            {

            

            } // end catch 

            return power;

        } // end method



        public float CalcVoltage(float power, float current)

        {

            //

            //Name                : float CalcVoltage(float power, float current)

            //Purpose            : Calculate the Voltage using Watt's Law

            //Re-use            : None

            //Method Parameters    : - float power

            //                      - power value in watts

            //                    - float current

            //                      - current value in amps

            //Output Type       : float

            //                    - the calculated Voltage value

            //

            float voltage = 0;

            try

            {

                voltage = power / current;

            } // end try

            catch (Exception)

            {

              

            } // end catch

            return voltage;

        } // end method



        public float CalcCurrent(float power, float voltage)

        {

            //

            //Name                : float CalcCurrent(float power, float voltage)

            //Purpose            : Calculate the Current using Watt's Law

            //Re-use            : None

            //Method Parameters    : - float power

            //                      - power value in watts

            //                    - float voltage

            //                      - voltage value in volts

            //Output Type       : float

            //                    - the calculated Current value

            //

            float current = 0;

            try

            {

                current = power / voltage;

            } // end try

            catch (Exception)

            {

            

            } // end catch

            return current;

        } // end method



      

        private void btnCalculate_Click(object sender, RoutedEventArgs e)

        {

            WattsLawDelegate wattsLawDel = null;

            float txtFirstValue = 0;

            float txtSecondValue = 0;

            float txtResult = 0;



            float option = 10;

            float result = 0;



            option = cboCalcType.SelectedIndex;

          

                switch (option)

                {

                    case 0:

                        txtFirstValue = float.Parse(txtFirstValue + "");

                    txtSecondValue = float.Parse(txtSecondValue + "");

                        wattsLawDel = new WattsLawDelegate(CalcPower);

                        lblResult.ContentStringFormat = "The calculated power is:";

                    // valid = true;

                       break;



                case 1:

                    txtFirstValue = float.Parse(txtFirstValue + "");

                    txtSecondValue = float.Parse(txtSecondValue + "");

                    wattsLawDel = new WattsLawDelegate(CalcCurrent);

                    lblResult.ContentStringFormat = "The calculated current is:";

                    // valid = true;

                    break;



                case 2:

                    txtFirstValue = float.Parse(txtFirstValue + "");

                    txtSecondValue = float.Parse(txtSecondValue + "") ;

                    wattsLawDel = new WattsLawDelegate(CalcVoltage);

                    lblResult.ContentStringFormat = "The calculated voltage is:";

                    //valid = true;

                    break;





                default:

                    break;

                  

            } // end switch

            result = wattsLawDel(txtFirstValue, txtFirstValue);

            txtResult = float.Parse(result + " ");



        }



        private void btnClear_Click(object sender, RoutedEventArgs e)

        {

            txtFirstValue.Clear();

            txtSecondValue.Clear();

            txtResult.Clear();

            cboCalcType.SelectedIndex = 0;

        }

      



      

        private void CboCalcType_SelectionChanged(object sender, SelectionChangedEventArgs e)

        {

            switch (cboCalcType.SelectedIndex)

            {

                case 0:



                    lblFirstValue.ContentStringFormat = "current";

                    lblSecondValue.ContentStringFormat = "voltage";





                    break;

                case 1:

                    lblFirstValue.ContentStringFormat = "power";

                    lblSecondValue.ContentStringFormat = "voltage";



                    break;

                case 2:

                    lblFirstValue.ContentStringFormat = "power";

                    lblSecondValue.ContentStringFormat = "currrent";



                    break;



                default:



                    break;



            }//end switch



        }



        private void txtFirstValue_TextChanged(object sender, TextChangedEventArgs e)

        {



        }



        private void txtSecondValue_TextChanged(object sender, TextChangedEventArgs e)

        {



        }



        private void txtResult_TextChanged(object sender, TextChangedEventArgs e)

        {



        }

        private void CommandCanExecute(object sender, CanExecuteRoutedEventArgs e)

        {



        }



        private void CommandExecuted(object sender, ExecutedRoutedEventArgs e)

        {



        }



    



      

    }





}//end namespace
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,025
Location
Chesapeake, VA
Programming Experience
10+
On lines 37-39, you dropped the namespace prefix for "Name". Notice how you have the "x:" namespace prefix for "Name" on lines 34-36.

As an aside, if you are using WPF properly (e.g. using MVVM), you should rarely need to explicitly give controls any names. Simply using WPF's binding support should handle about 95% of your needs. Now, on the other hand, if you are using WPF as if it were WinForms or HTML+CSS+JavaScript, then yes would need to explicitly name the controls.
 
Top Bottom