(C++ / CLI) Set contents of textbox to content of a .txt - c++-cli

I am looking to set the contents of a textbox to that of a .txt file,
however I cannot get it to work. I have a button which would "refresh" the
content of the textbox to that of the .txt file, here is the code I am using:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
std::ifstream dat1("DataStore.txt");
String^ textHolder;
if (dat1.is_open())
{
while ( getline(dat1, line) )
{
textHolder += line.c_str;
}
textBox1->Text = textHolder;
dat1.close();
}
else textBox1->Text = "Unable to open file";
}
I get these 2 errors when compiling the program:
error C3867: 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str': function call missing argument list; use '&std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str' to create a pointer to member
error C2297: '+=' : illegal, right operand has type 'const char *(__thiscall std::basic_string<char,std::char_traits<char>,std::allocator<char>>::* )(void) throw() const'
How do I make this work?
NOTE: I am trying to display the whole .txt file's contents in the textbox

If you are going to use C++/CLI you may as well take advantage of .net. You can use the File class to read the file for you:
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
textBox1->Text = System::IO::File::ReadAllText("DataStore.txt");
}
You'll need to hope that the process working directory contains that file. In a GUI app you are better served by specifying full paths to files, since the working directory is typically ill-defined.
If you are trying to learn C++, then perhaps a .net C++/CLI WinForms application is not the place to start.

You forgot to end method c_str, change it to c_str()

Related

Hooking to empty C++ implementation with C++/CLI events

I am using an open source toolkit (VTK) that has an object with a method that is triggered by pressing any key. The idea is to overwrite the implementation in your own class but I can't figure out how to do it in C++/CLI.
This is what I have, but I can't figure out how to hook it to an object.
delegate void myEventHandler(vtkObject * sender, EventArgs ^ e);
event myEventHandler^ LeftButtonPressEvt;
LeftButtonPressEvt += gcnew myEventHandler(&MyClass::MyModifiedOnKeyPress);
void MyModifiedOnKeyPress(vtkObject * sender, EventArgs ^ e)
{
//this should be called whenever the button is pressed
}
Hooking it to the vtk object would look something like this:
vtkObject->OnKeyPress += gcnew myEventHandler(&MyClass::Pick);
This returns the error that a function is the left operand which makes sense, but I cant figure out how this would be hooked to the event.
For non-static methods, you need to specify which object the delegate should point to.
vtkObject->OnKeyPress += gcnew myEventHandler(this, &MyClass::Pick);
// ^^^^
(If that doesn't solve the problem, then I don't understand exactly what problem you're having. If you're getting an error message, don't describe the error, copy & paste exactly what the error message is.)

C++ will change it last input

I don't know if anyone had the same problem.
I use Microsoft visual C++ 2010 I am new to it but I know a lot of Java program but for me trying to go from one program to another is confusing.
Anyway I will make a basic program "Hello World" and run it it works fine but when I go and try to type something else or add something to the text it remains the same code when I run it. It does not happen all the time but it will do it a lot and it seam like it is becoming more common.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
label1 -> Text = "hello world";
}
This is what I put.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
label1 -> Text = "This is a test";
}
when I run the first code it works, but when I change to the code and put in the 2nd code it will remain the first code when I run it, but I change what the code say so it should change as well but it does not.
There is a setting that controls whether Visual Studio forces a build before you run, likely you set it to false.
Here is a similar question
Tools + Options, Projects and Solutions, Build and Run. "On Run, when projects are out of date" = Always build.

VS2010 C++ math support (sin identifier not found)

I have a Windows Form on a new C++ project, a Button1, and inside the Button1 code am using some trig functions. I also have #include <cmath> in the resource.h file next to Form1.h file. (Below is the contents of resource.h):
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by app.rc
#include <cmath>
Why is the code not seeing the trig function?
The Button1 code is as follows:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
double x[1000];
double y[1000];
double hifac;
double px[1000];
double py[1000];
int nout, jmax;
double prob;
int i,period;
period=300;
for (i=0; i<1000;i++){
x[i]=i;
y[i]=sin(2 * 3.14 * i / period);
}
}
Just put appropriate code in its appropriate place. That #include does not belong in resource.h, that header file should be reserved strictly for resource identifier definitions for unmanaged resources. Just about anywhere else is appropriate, obvious choices are source file where this code appears and the stdafx.h precompiled header file.
And use the appropriate math function. It is std::sin(), the missing namespace name is surely why you get the compiler complaint. But calling that function from managed code is inefficient, extra work is needed to run native code from managed code. It isn't much extra work but it is unnecessary work. Use Math::Sin() instead.

Going from a parent form to a child form without creating a gcnew form

Hello I have a main form (Form1) and a child form (Form5). If I click a button for the first time I want it to open a new form. However if it is being clicked for the second time I want it to open the form I opened the first time, holding that forms data I will have filled in (textboxes etc). This is what I have:
.cpp file:
System::Void Form1::button5_Click(System::Object^ sender, System::EventArgs^ e){
formclick++;
if (formclick == 1)
{
Form5 ^dos1 = gcnew Form5(this, MyArray, MyArray1);
dos1->Show();
}
if (formclick==2)
{
otherform->Show();
}
Form1.h file:
> Form1(System::Windows::Forms::Form ^ Form5)
> {
>
>
> otherform = Form5;
> InitializeComponent();
> } public: System::Windows::Forms::Form ^ otherform;
However I get the error:
An unhandled exception of type 'System.NullReferenceException' occurred in System.Windows.Forms.dll
Additional information: Object reference not set to an instance of an object.
TIA
You are storing the newly created form in the local scope of the button5_click function (in the dos1 variable) and that means it is free to be removed from memory when the function exists.
The code excerpts you've given is a little messy, but it could be enough for you to change the click funtion to the following:
System::Void Form1::button5_Click(System::Object^ sender, System::EventArgs^ e){
if (!otherForm)
otherForm = gcnew Form5(this, MyArray, MyArray1);
else
otherform->Show();
}
Code not tested, but the principal thing is that you need to store the newly created form in a class member, and not only locally to the click funtion!
Regards
Even

Brother Label Printer c# program with visual studio 2012 for windows8

I want to make a simple program which prints sth (Just wnt it to write sth )
I ve added Interop.bpac.dll (Found it from samples bin folder)
I ve wrote this code
private void buttonTry_Tapped_1(object sender, TappedRoutedEventArgs e)
{
bpac.DocumentClass doc = new DocumentClass();
if(doc.Open("templateFile.lbx"))
{
doc.GetObject("field1").Text = "Hello";
doc.GetObject("field2").Text = "World";
doc.StartPrint("", PrintOptionConstants.bpoDefault);
doc.PrintOut(1, PrintOptionConstants.bpoDefault);
doc.EndPrint();
doc.Close();
}
}
And it gives an error "Interop type 'bpac.DocumentClass' can not be embedded.Use the applicable interface instead." For now Im trying to print QL700 I ll try some other thermal receipt printers later
And also I couldnt get the templateFile.lbx whats that and where does the program search this file?
Thanks :)
Change Embed Interop Types to False