how do I set a value of a textbox in Visual C++ - visual-c++

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

Related

Visual MFC 2015 - Having problem with stringstream with a CString japanese

I am struggling with using streamstring for a variable Cstring which contains japanese character. I have tried to use imbue or even setlocate method but it does not work, the result when I dubug "ss" is always ???? chracter. Please help!
CString m_strPath = L"適当";
std::stringstream ss;
ss.imbue(std::locale("Japanese"));
ss << m_strPath;
I want ss contain "適当" too.

Visual C++ run times error

All case run in Visual C++ 2005 environment
Function definition:
char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
Case 1.
CString dd;
dd=(LPCTSTR) PQgetvalue(res,i,0);
when above is compiled with NO ERROR but dd has store garbage data like: 〱〰71〱1 몭몭몭몭몭몭ꮫꮫꮫꮫ
Case 2.
CString dd;
dd= PQgetvalue(res,i,0);
No Compilation error and provide correct output.
Question: How to convert Char* to CString
Case 3.
CString dd;
dd= PQgetvalue(res,i,0);
CString dd = PQgetvalue(res,i,0);
There is NO Difference between above code. But second case generate compilation error like:
error C2440: 'initializing' :
cannot convert from 'char *' to 'ATL::CStringT<BaseType,StringTraits>'
Please clarify anyone
For the first case. I guess you compile your project with Unicode character set (to verify open project settings dialog "Configuration properties" - "General" - "Character set"). Thus converting char* to const wchar_t* would gives you garbage. I.e. TCHAR is wchar_t when you compile with Unicode.
On the C2440. Please clarify whether code in Case 3 is correct (i.e. you supply all 3 lines to compiler as provided here.). Also I'd suggest you to search MSDN on this error.

C++ Convert Sytem::String^ to LPCOLESTR

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

Help, cannot how to run unsafe

How to run csc /unsafe *.cs
using System;
class UnsafeCode
{
unsafe static void Main()
{
int count = 99;
int* p;
p = &count;
Console.WriteLine("Initial value of count is " + *p);
*p = 10;
Console.WriteLine("New value of count is " + *p);
}
}
Error 1 Unsafe code may only appear if compiling with /unsafe C:\Documents and Settings\Eddy Ho\My Documents\Visual Studio 2010\Projects\608-UnsafeCode-Errors\608-UnsafeCode-Errors\Program.cs 5 26 608-UnsafeCode
It's a command line argument.
You use the Visual Studio command prompt and simply type it out.
You can also set this in the IDE, by going to the properties of the project, and in the Build tab select Allow unsafe code checkbox. See here.

Visual C++: displaying integer in a text Box

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;

Resources