Microcrew
Microcrew

Reputation: 11

How do I get the value from an numericUpDown in c++?

I've been trying out the windows forms for a small project of mine where the program calculates the contents of different amounts of food.

I figured the numericUpDowns would be a good way to get inputs on the amount of food.

I tried following Microsofts tutorials on how to get the value from an numericupdown for the calculations and ended up with this:

Double testVariable = numericSportfit.Value * 14.4; 
    


private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
}

//Button Räkna
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) 
{
    MessageBox::Show(testVariable.ToString(), "Resultat");
}

However I keep getting the error message "E0153 expression must have class type" and "C2228 left of '.Value' must have class/struct/union"

I am running VS 2019. I have tried searching the entire web for on how to get the value in C++ but all I can find is C#. Originally I just wanted to program in C as that is what I'm most familiar with but seems VS only has C++ and C#.

Here is the entire block of code:

namespace Foderkalkylator {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
    MyForm(void)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~MyForm()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::NumericUpDown^ numericSportfit;
protected:

private:
    /// <summary>
    /// Required designer variable.
    /// </summary>
    System::ComponentModel::Container ^components;


    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    void InitializeComponent(void)
    {
        this->button1 = (gcnew System::Windows::Forms::Button());
        this->numericSportfit = (gcnew System::Windows::Forms::NumericUpDown());
        (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericSportfit))->BeginInit();
        this->SuspendLayout();
        // 
        // button1
        // 
        this->button1->BackColor = System::Drawing::SystemColors::ActiveBorder;
        this->button1->Location = System::Drawing::Point(656, 387);
        this->button1->Name = L"button1";
        this->button1->Size = System::Drawing::Size(116, 62);
        this->button1->TabIndex = 0;
        this->button1->Text = L"Räkna";
        this->button1->UseVisualStyleBackColor = false;
        this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
        // 
        // numericSportfit
        // 
        this->numericSportfit->DecimalPlaces = 2;
        this->numericSportfit->Location = System::Drawing::Point(94, 55);
        this->numericSportfit->Name = L"numericSportfit";
        this->numericSportfit->Size = System::Drawing::Size(120, 20);
        this->numericSportfit->TabIndex = 1;
        this->numericSportfit->ValueChanged += gcnew System::EventHandler(this, &MyForm::numericSportfit_ValueChanged);
        // 
        // MyForm
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(784, 461);
        this->Controls->Add(this->numericSportfit);
        this->Controls->Add(this->button1);
        this->Name = L"MyForm";
        this->Text = L"Foderkalkylator";
        this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
        (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericSportfit))->EndInit();
        this->ResumeLayout(false);

    }

    
Double testVariable = numericSportfit.Value * 14.4; 
    


private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
}

//Button Räkna
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) 
{
    MessageBox::Show(testVariable.ToString(), "Resultat");
}


//sportfit numericUpDown
private: System::Void numericSportfit_ValueChanged(System::Object^ sender, System::EventArgs^ e) 
{

}
};

}

Thanks in advance!

Upvotes: 1

Views: 898

Answers (2)

got6108
got6108

Reputation: 1

Load the text, just like this:

System::String^ StringValue = numericUpDown -> Text;
const char* char_vale = (const char*)(System::Runtime::InteropServices::Mershal::StringToHGlobalAnsi(StringValue)).ToPointer();
std::string string_value = char_value;
double Value = std::stod(string_value);

Upvotes: -1

Code Gorilla
Code Gorilla

Reputation: 980

If I understand correctly your problem is with this line:

double testVariable = numericSportfit.Value * 14.4; 

So looking at the online help for System::Windows::Forms::NumericUpDown give a page that shows

public decimal Value { get; set; }

Which as you say is only in C#. I can't remember the proper name for that call, but it replaces the getValue() and setValue() equivalents in C++. So I would guess it could be getValue() or Value() that you want.

Rather than guessing you should be able to drill down into the code and find out. In your source code,Control click on NumericUpDown and that should take you to the file that defines the object and you might be able to work out what you need from there.

Also - You can compile C in VS2019 I think you need to define the file as a .c file, but to be honest you will get much better apps if you learn C#, IMO

Upvotes: -1

Related Questions