I want to write
int i=4;
textBox1->Text = i;
But it is giving compilation error for type mismatch. How to do box or typecast this?
Sorry for answering the quesition myself. But I just got it while searching.
There is a very easy method
int i=4;
textBox1->Text = Convert::ToString(i);
Instead you could use: textBox1->Text = i.ToString();.
There is a very easy method
int i=4;
textBox1->Text = ""+i;
You need conversion, not a cast. Use itoa() or itow() depending on whether you compile for Unicode.
if you are using CString you can use Format method, or use old c function itoa
example:
CString str;
str.Format("%d",i);
also do not forget to call UpdateData method to update the GUI controls
Convert integer to string and set as value for Text.
CString textVal;
textVal.Format(_T("%d"), i);
textBox1->Text = textVal;
Related
I write in mixed mode (C++/CLI) and I can not resolve this problem:
String ^progID = "Matrikon.OPC.Server";
CLSID clsid;
HRESULT result = CLSIDFromProgID(progID, &clsid);
error C2664: 'CLSIDFromProgID' : cannot convert parameter 1 from 'System::String ^' to 'LPCOLESTR'
How can I convert String^ to LPCOLESTR ?
Thanks!
I made another way:
// 1.
pin_ptr<const WCHAR> str = PtrToStringChars(progID);
LPCOLESTR coleString = (LPWSTR)str;
I have found that pin_ptr will be released if goes out of scope Define the Scope of Pinning Pointers and pin_ptr (C++/CLI)
This code works well for me:
// 2. this is the same like (1.)
String ^progID2 = "Matrikon.OPC.Simulation.1";// This is example of dynamic string
pin_ptr<const WCHAR> PINprogID2 = PtrToStringChars(progID2);
CLSID clsid2;
HRESULT result2 = CLSIDFromProgID(PINprogID2, &clsid2); //(LPCOLESTR, &CLSID)
Another example:
// 3.
pin_ptr<const WCHAR> sclsid3 = PtrToStringChars("{63D5F432-CFE4-11d1-B2C8-0060083BA1FB}");
CLSID clsid3;
CLSIDFromString((WCHAR*)sclsid3, &clsid3); //(LPOLESTR, &CLSID)
I am not much experienced and I am not sure if there are some lack of memory, but I think those codes are correct.
Avoid using the hammer for every nail. C++/CLI lets you just as easily use native types. So it is simply:
LPCOLESTR progid = L"Matrikon.OPC.Server";
// etc..
Non-zero odds (always say why) that you can simply use Type::GetTypeFromProgID().
First, lets convert System::String to char*
IntPtr p = Marshal::StringToHGlobalAnsi(progID);
char *pNewCharStr = static_cast<char*>(p.ToPointer());
second, casting char * to LPCOLESTR using ATL conversion macro:
LPCOLESTR converted_string = A2COLE(pNewCharStr);
I am trying to store some contents into a string variable by passing it as a parameter in various types of Windows API functions which accepts variable like char *.
For example, my code is:-
std::string myString;
GetCurrentDirectoryA( MAX_PATH, myString );
Now how do I convert the string variable to LPSTR in this case.
Please see, this function is not meant for passing the contents of string as input, but the function stores some contents into string variable after execution. So, myString.c_str( ) is ruled out.
Edit: I have a workaround solution of removing the concept of string and replacing it with something like
char myString[ MAX_PATH ];
but that is not my objective. I want to make use of string. Is there any way possible?
Also casting like
GetCurrentDirectoryA( MAX_PATH, ( LPSTR ) myString );
is not working.
Thanks in advance for the help.
Usually, people rewrite the Windows functions they need to be std::string friendly, like this:
std::string GetCurrentDirectoryA()
{
char buffer[MAX_PATH];
GetCurrentDirectoryA( MAX_PATH, buffer );
return std::string(buffer);
}
or this for wide char support:
std::wstring GetCurrentDirectoryW()
{
wchar_t buffer[MAX_PATH];
GetCurrentDirectoryW( MAX_PATH, buffer );
return std::wstring(buffer);
}
LPTSTR is defined as TCHAR*, so actually it is just an ordinary C-string, BUT it depends on whether you are working with ASCII or with Unicode in your code. So do
LPTSTR lpStr = new TCHAR[256];
ZeroMemory(lpStr, 256);
//fill the string using i.e. _tcscpy
const char* cpy = myString.c_str();
_tcscpy (lpStr, cpy);
//use lpStr
See here for a reference on _tcscpy and this thread.
Typically, I would read the data into a TCHAR and then copy it into my std::string. That's the simplest way.
Whenever I try to directly assign a String variable to another String variable I get error found no suitable conversation.
So is there a way to convert String^ pointer to a non-pointer struct String ?
I want :
System::String a = System::String('X',256);
I don't want :
System::String^ a = %System::String('X',256);
No, there is not, because as Hans pointed out in a comment, System::String is immutable. You cannot assign it. You can only associate a handle with an entirely new System::String object.
BTW
System::String^ a = %System::String('X',256);
is incorrect, it should be
System::String^ a = gcnew System::String('X',256);
Use System::String a('X', 256);.
how do I set a value of a textbox in Visual C++?
I have been trying the following but the line strTemp.Format("%d", nNbr); does not compile
thanks
int nNbr = 5;
CString strTemp;
strTemp.Format("%d", nNbr);
textBox.SetWindowTextW(strTemp);
You probably need to use the _T macro in order to support both ANSI and UNICODE builds of your application:
int nNbr = 5;
CString strTemp;
strTemp.Format(_T("%d"), nNbr);
textBox.SetWindowText(strTemp);
The following code is generating warning C6284 when compiled with /analyze on MSVC 2008 : object passed as parameter '%s' when string is required in call to function.
CString strTmp, str;
str = L"aaa.txt"
strTmp.Format (L"File: %s", str);
I'm looking for a nice solution for this that would not require static_cast
Microsoft describes the usage of CString with variable argument functions here:
CString kindOfFruit = "bananas";
int howmany = 25;
printf_s( "You have %d %s\n", howmany, (LPCTSTR)kindOfFruit );
As an alternative you can also use the method PCXSTR CString::GetString() const; to try to fix the warning:
CString strTmp, str;
str = L"aaa.txt"
strTmp.Format (L"File: %s", str.GetString());
One of CString's design flaws, err, features is that it features an implicit conversion to LPCTSTR which makes the warning not that meaningful IMHO. But anyway, if you look at the Microsoft documentation, they actually use casts in their own example. I don't really see the need to avoid a static_cast here, in fact I would welcome it as it does make the implicit conversion more explicit and thus easier to spot.