fstream and c_str() : c_str is not working - winforms

I have been recently working on a program and i tried to use System::String^ to use it for an ofstream file...
I always get an Error...
Note This is a Form so it is a Header File.
This is my Code...(one part)
System::String^ txtPath;
#pragma endregion
private: System::Void btn1_Click(System::Object^ sender, System::EventArgs^ e) {
using namespace std;
ofstream fout (txtPath->c_str());
}
private: System::Void txtBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
txtPath=(Convert::ToString(txtBox1->Text))+"\\weapon_deagle.txt";
}
The Error I get is :
Error 1 error C2039: 'c_str' : is not a member of 'System::String' c:\users\a\documents\visual studio 2010\projects\d1s1k formed\d1s1k formed\Form1.h
101

Related

A non-static member reference must be relative to a specific object

I'm trying to get the text in my textbox tb_key to write to my std::string Key Variable by doing this:
std::string Key = TRIPRECOILWARE::LoginForm::tb_key->Text;
I get an error saying :
A non-static member reference must be relative to a specific
object
I tried to search but I couldn't find anything really that fixed it for me.
Minimal Reproducible Example:
LoginForm.h
namespace TRIPRECOILWARE {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
private: System::Void tb_key_TextChanged(System::Object^ sender, System::EventArgs^ e)
{
}
}
LoginForm.cpp
std::string Key = TRIPRECOILWARE::LoginForm::tb_key->Text;
I'm trying to use this in LoginForm.h
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
if (Authenticate(StringToChar(Key), (StringToChar(hwid)))) // Authenticates key & hwid
{
this->Hide();
Software^ soft = gcnew Software();
soft->Show();
}
Basically, I want to get Key from Textbox called tb_key and write
it to my Key variable defined above. Then use that key to
authenticate and perform code
Your real problem is a duplicate of How to turn System::String^ into std::string?
Corrected code:
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>
using namespace System;
using namespace msclr::interop;
void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
std::string Key = marshal_as<std::string>(tb_key->Text);
if (Authenticate(Key.c_str(), hwid.c_str())) // Authenticates key & hwid
{
Hide();
Software^ soft = gcnew Software();
soft->Show();
}
}

Declare VideoCapture as a global variable

I am trying to stream webcam at C++/CLI winform application with using picture-box.
With the help of timer,i can stream webcam on picture-box but VideoCapture cap(0); Mat frame; are declare continuously. How can i transfer these two to a button click and use inside this timer.
private: System::Void Timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
VideoCapture cap(0);
Mat frame;
cap.read(frame);
System::Drawing::Graphics^ graphics2 = pictureBox1->CreateGraphics();
System::IntPtr ptr2(frame.ptr());
System::Drawing::Bitmap^ b2 = gcnew System::Drawing::Bitmap(frame.cols,
frame.rows, frame.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr2);
System::Drawing::RectangleF rect2(0, 0, pictureBox1->Width, pictureBox1->Height);
graphics2->DrawImage(b2, rect2);
}

how can I solve this loop?

I am currently building a windows form application and I have got the next problem,
I can't declare a variable global because the syntax im using won't allow me to do that, also, i need to declare the variable in the method it self and at last, it must loop so its able to count. This is what I have got so far:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
int i;
m_bIsTimerOn = true;
while (m_bIsTimerOn)
{
i++;
label1->Text = (i.ToString());
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
m_bIsTimerOn = false;
timer1->Enabled = false;
}
m_bIsTimerOn is a globally boolean. As you might see here my problem is if i press button1 the program is just stuck in the while loop. I would like to know that the moment you press button2 the while loop stops I also would like to know if this is even possible. If you could response in c++ that would also be fine.
Thank you in advance.
I suppose you want to increment i while timer is on. If you need to update i for each timer tick then it's pretty easy, remove that loop (and that variable BTW) and simply do update inside timer Tick event:
private:
int _i;
void button1_Click(Object^ sender, EventArgs^ e) {
_i = 0;
timer1->Enabled = true;
}
void _timer1_Tick(Object^ sender, EventArgs^ e) {
label1->Text = (i++).ToString();
}
void button2_Click(Object^ sender, EventArgs^ e) {
timer1->Enabled = false;
}
If i must be updated independently from timer ticks then you have to move it to a BackgroundWorker or simply in event handler for Application::Idle:
void OnIdle(Object^ sender, EventArgs^ e) {
label1->Text = (_i++).ToString();
}
void button1_Click(Object^ sender, EventArgs^ e) {
_i = 0;
Application::Idle += gcnew EventHandler(this, Form1::OnIdle);
timer1->Enabled = true;
}
void button2_Click(Object^ sender, EventArgs^ e) {
Application::Idle -= gcnew EventHandler(this, Form1::OnIdle);
timer1->Enabled = false;
}
As final note: you may even keep your loop as is and put a call to Application::DoEvents() just after your label1->Text = (i.ToString()); but this will probably consumes a lot of CPU, slow down your application and open your code to reentrancy, I'd really avoid something like that...
Your program is in endless loop because the while loop prevents the messages from getting processed and hence your button2_Click does not get invoked. To make you application capable of processing the messages that occurs, add Application.DoEvents() in your loop as:
while (m_bIsTimerOn)
{
i++;
label1->Text = (i.ToString());
Application.DoEvents(); // causes the application to handle pending events
}
So now, if you press the second button, m_bIsTimerOn will become false and your loop will terminate.

How can i run hidef XNA from winforms?

I have winforms application and i have texture SimpleTexture.xnb compiled as HiDef. I need to run XNA 3D visualisation from my winforms application in separate window. I try this:
private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread thStartGame = new System.Threading.Thread(StartGame);
thStartGame.Start();
}
private void StartGame()
{
using (Game1 game = new Game1())
{
game.Run();
}
}
But i get error:
Error loading "SimpleTexture". This file was compiled for the HiDef profile, and cannot be loaded into a Reach GraphicsDevice.
What can i do to run this??
Change reach to hidef:
YourGraphicsDeviceManager.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs (graphics_PreparingDeviceSettings);
void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
{
e.GraphicsDeviceInformation.GraphicsProfile = GraphicsProfile.HiDef;
}

How to cancel an event in Microsoft Visual C++ 2010?

I like to cancel an event if there is an error, so the rest of the code won't run.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {if(textBox1->Text == "")//cancel event
}
If the text of the textBox1 is empty the event exit so the rest of the code wont run and doesn't create an error.
I do it like this, but is there a better solution to this?
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { if(textBox1->Text == "") Error = 1;
if(Error == 0)//Rest of the code);
}
Thank you.
Imi007
Just 'return;' that would work

Resources