change format of C++/CLI stopwatch from System::Diagnostics windows form CLR - c++-cli

private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
this->label3->Text = stopWatch.Elapsed.ToString();}
I need help for changing the format of my stopWatch, how can i put a format like "HH:mm:ss" please ?
because doing this just show the default format that shows too much numbers

Stopwatch.Elapsed is of type TimeSpan.
This type supports various formats when converting to string.
You can see all the info here: Standard TimeSpan format strings, Custom TimeSpan format strings.
In your case I think the following custom format string will work:
System::String ^ text = stopWatch->Elapsed.ToString("hh\\:mm\\:ss");
this->label3->Text = text;

Related

C++-CLI how do I convert String^ (from a textbox) to something that a function which takes void* type variables, will accept?

I have the following function from a dll:
aisgdll_setinfo(int dev, set_field_code field, void *data);
I know how to deal with the first two parameters. I have a textbox the user enters data into and the textbox returns a variable of type String^. I somehow need to get the data from that textbox and do something, so that I can write it to this function to the void *data parameter.
You can use this form.
I used the Thread :: Sleep to prevent abuse in processing. While your thread does not change the status of the screen will be updated.
void solve()
{
using namespace System;
using namespace System::Windows::Forms;
using namespace System::Threading;
Thread^ bot_thread = gcnew Thread(gcnew ThreadStart(bot_run));
bot_thread->Start();
PictureBox^ PB_LoadGIF = gcnew PictureBox();
PB_LoadGIF->Visible = true;
while (bot_thread->ThreadState == ThreadState::Running)
{
Thread::Sleep(1000);
PB_LoadGIF->Parent->Refresh();
}
}
After further research online, I have found some solutions to the problems I was having. The functions I was plugging the retrieved values into needed pointers. I think these solutions can be adapted by others for their own purposes. For converting the Visual Studio Text box String^ to char*:
char* iData = (char*)Marshal::StringToHGlobalAnsi(activeBox->Text).ToPointer();
When I needed to get a floating point number from a Text box, I used the following:
int anInteger = (int)((Convert::ToDouble(activeBox->Text))*10);
int *iData = &anInteger;

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

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()

How to convert a handle string to a std::string

I am trying to convert a handle string to a normal string. I though the method I was using was working, but when I look in the debugger it appears that half of my string has been chopped off on the line that creates the chars variable. Any idea why and what the proper way to convert a handle string to a normal string woudl be?
std::string convert(String^ s) {
const char* chars = (const char*)(System::Runtime::InteropServices::Marshal::
StringToHGlobalAnsi(s)).ToPointer();
string myNewString = std::string(chars);
return myNewString;
}
It's probably the debugger that's cutting off the display of the string. You didn't mention how long a string you're using, but the debugger can't display infinite length, so it has to cut it off at some point.
To verify this, try printing myNewString to the console, or to the debugger via Debug::WriteLine or OutputDebugString.
However, there is a significant issue in your code: After allocating memory with StringToHGlobalAnsi, you must free it using FreeHGlobal.
If you want to continue using StringToHGlobalAnsi, I'd fix it up like this:
std::string convert(String^ s) {
IntPtr ptr = Marshal::StringToHGlobalAnsi(s);
string myNewString = std::string((const char*)ptr.ToPointer());
Marshal::FreeHGlobal(ptr);
return myNewString;
}
However, it's probably easier to use the marshal_as methods. This will take care of everything for you.
std::string output = marshal_as<std::string>(managedString);

SQL stored procedure equivalent to ODS e.InputParamters[] = x?

Morning all, hope everyone is ok.
I have an ODS that uses a combination of query string and Selecting event parameters.
For the ODS, I'd input a paramater in the selection event a la:
protected void oDs_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["memberid"] = memberid;
}
How would I do something along the same lines in a SQL data source?
I have tried the following but without success:
protected void SQL_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.Command.Parameters.Add(memberid);
}
Can anyone point out, the no doubt stupid, error in my ways?
Any help gratefully received.
Sorted - being lazy - sorry.
Stuck this into the page load event instead : )
sqlSPSuppliers.SelectParameters["MemberId"].DefaultValue = memberid;

Trying to write a textfield value to file in c++

I am tearing my hair out over this error.
------ Build started: Project: shotfactorybatchgen, Configuration: Debug Win32 ------
shotfactorybatchgen.cpp
c:\documents and settings\administrator\my documents\visual studio 2010\projects\shotfactorybatchgen\shotfactorybatchgen\Form1.h(307): error C2664: 'fprintf' : cannot convert parameter 2 from 'System::String ^' to 'const char *'
No user-defined-conversion operator available, or
Cannot convert a managed type to an unmanaged type
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I have looked all around the interwebs but I can not find an answer. Here is the code that the error is happening in.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Decimal value;
if(!Decimal::TryParse(textBox4->Text, value)) {
MessageBox::Show("Non-numeric characters detected in 'Wait Time' filed", "Oops", MessageBoxButtons::OK, MessageBoxIcon::Warning);
} else {
if(!Decimal::TryParse(textBox3->Text, value)) {
MessageBox::Show("Non-numeric characters detected in 'Max Upload' filed", "Oops", MessageBoxButtons::OK, MessageBoxIcon::Warning);
} else {
FILE *OutFile = fopen("run.bat","w");
fprintf(OutFile,"#ECHO OFF\r\nC:\\Python26\\python.exe factory.py");
if(factoryname->Text != ""){
fprintf(OutFile," -f ");
fprintf(OutFile,factoryname->Text);
}
fclose(OutFile);
}
}
}
Any ideas? It is a simple windows form application. I am using Visual Studio C++ 2010
Thanks
Colum
If factoryname->Text identifies a property, then its getter may throw a CLR exception, in which case you will leak the file handle OutFile. Consider not using fopen, etc. It's a C library function and there are better alternatives such as std::ofstream.
Alternatively, as you have .NET available, you could use StreamWriter which will let you pass System::String and thus avoid your conversion problem as well:
StreamWriter writer(someFilePath);
writer.WriteLine(someString);
C++/CLI will take care of closing the writer when the scope is exited.
It's simply a conversion error:
cannot convert parameter 2 from 'System::String ^' to 'const char *'
Possible solution is posted here:
What is the best way to convert between char* and System::String in C++/CLI
Opening Files
Convert System::String to const char*