Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie to Visual C++ - Simple Program Help

Status
Not open for further replies.

neoborn

Technical User
Nov 1, 2002
6
CA
Hello,

I am new to the whole world of C++ and have been studying a few samples / examples off the web of C++ coding etc. I made a short code / program called "The Namorizer" which is a console program that asks for your first name, then your last and finally adds them together and displays the result i.e your full name.

What I am trying to accomplish here is to do the same thing but with a windows form. I have designed the form in VS2k3 and now would like to add the code to the various textboxes etc for the events. I am kind of lost at this point of what to do and would like someone with patience and skills in communication just to spend a few minutes explaining where I would put this and also perhaps providing me with links to good sites and tutorials to learn from.

Thank you in advance for any help you can give.

neo.
 
My friend, I would venture to say that learning Visual C++ programming (especially MFC) will probably be one of the toughest eggs you will ever have to crack. Don't feel bad, once it does click it will make perfect sense, but that learning curve is ROUGH!

Good luck!

"I would rather have a free bottle in front of me, than a pre-frontal lobotomy..."

-Shrubble
 
Nah, Visual C++ programming and MFC are a piece of cake, compared to COM, ActiveX, DirectX, and .NET! ;)
 
add variables to your text boxes

i.e. first name box, second name box and finally I assume you have a full name box?

do you want a button to process the details? (i.e. make the full name appear?)

or you could put event handlers into your firstname and surname text boxes - i.e. On_EN_CHANGE this will create a routine that will be activated every time you enter text in that box - then the program will update the full name box when you type in the first name or the surname box?

Use UpdateData ( true ) to retrieve the firstname and surname text strings, then set the fullname string and finally UpdateData ( false ) - this will put the fullname string into your box.

Hope this helps???

 
In Visual C++ 2003 if you're using .NET forms, you should be able to do something like textBox1.Text = "blah blah"; // or whatever your text box/es is/are named. This should also work if you assign it a variable of appropriate type (string, character array). Well, at least it does in C#.
 
1. I am using vsnet 2003 for this project.

2. I have three boxes. First Name, Last Name and Full Name text boxes.

3. I was planning on using a button to update the third text box so when the user clicks on "ok" or whatever it says " Your full name is:.......". I wouldn't mind trying your auto update feature as they type but programatically I'm not there yet :)

4. I looked for this UpdateData but could not find it in the textbox properties area(I'm such a noob).


 
Ok so I have been reading "SAMS Teach Yourself Visual C++ in 21 Days" and have learn't some more, of which I am going to try in my program :).

I think I now get the "updatedata" function or whatever :) I'll keep hacking away and see what I come up with.
 
look into the class wizard, you need to create a member variable. (Assuming MFC here)
 
I am also trying to learn c++ and am using "Beginning C++"

by Ivor Horton


It looks like a good starting text


 
This is what I have so far. I'm such a newb it's not funny. The third box (Results) is not updating when I type in to the first name box and second name box. The program is also exiting when I press enter / return after inputting my first name, why is this?

==============================================================
// TheNamorizerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TheNamorizer.h"
#include "TheNamorizerDlg.h"
#include ".\thenamorizerdlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
enum { IDD = IDD_ABOUTBOX };

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

// Implementation
protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CTheNamorizerDlg dialog



CTheNamorizerDlg::CTheNamorizerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTheNamorizerDlg::IDD, pParent)

, m_strFirstMsg(_T(""))
, m_strSecondMsg(_T(""))
, m_strResults(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTheNamorizerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);

DDX_Text(pDX, IDC_FIRSTMSG, m_strFirstMsg);
DDX_Text(pDX, IDC_SECONDMSG, m_strSecondMsg);
DDX_Text(pDX, IDC_RESULTS, m_strResults);
}

BEGIN_MESSAGE_MAP(CTheNamorizerDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_EN_CHANGE(IDC_FIRSTMSG, OnEnChangeFirstmsg)
ON_EN_CHANGE(IDC_SECONDMSG, OnEnChangeSecondmsg)
ON_EN_SETFOCUS(IDC_RESULTS, OnEnSetfocusResults)
END_MESSAGE_MAP()


// CTheNamorizerDlg message handlers

BOOL CTheNamorizerDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Clear text boxes before starting or make sure they are clear

//m_strFirstMsg = "";
//m_strSecondMsg = "";

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

void CTheNamorizerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CTheNamorizerDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTheNamorizerDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}


void CTheNamorizerDlg::OnEnChangeFirstmsg()
{
//On the box content changing from being empty to having some
//content save the value and store it in a defined variable

//Get value from input box
UpdateData(TRUE);
}

void CTheNamorizerDlg::OnEnChangeSecondmsg()
{
//On the box content changing from being empty to having some
//content save the value and store it in a defined variable

//Get value from input box
UpdateData(TRUE);
}

void CTheNamorizerDlg::OnEnSetfocusResults()
{
Not sure what to do here but something like: m_Results = ("Your full name is ")m_strFirstMsg + m_strSecondMsg;
}
=-=-=-=-=-=-=-=--=-=-=-==-===-=-===-==-=-==-=-=-=-=-=-=-=-=-
 
I misunderstood. You said Visual C++ 2003 and "windows form" and so I thought of WinForms. :) I don't use MFC, sorry.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top