Click here to Skip to main content
15,891,529 members
Articles / Programming Languages / C++/CLI

Using Slider Control in a Good Example

Rate me:
Please Sign up or sign in to vote.
2.21/5 (11 votes)
5 Mar 2008CPOL1 min read 53.2K   2.2K   12   2
Easy line encrypter decrypter

Introduction

Previously we learned how to use a good application that makes sense as a first program, now I'm trying to practice the slider control with the same as what I used in the first example which are the buttons and the Edit controls.

Using the Code

The idea is too simple. Take the ASCII code of the input string, break it into characters and then shift every ASCII code with a certain code obtained from the Key (slider control).

How to do it? It's simple even for me; as a beginner I thought it would be a very big challenge. We are going to create a new MFC project, then add to it two buttons; one edit control and one slider control. Then we add a member variable for the slider control and let’s call it m_ctrlSlide with the type CSliderCtrl. Another member variable we should add to the Edit Control with the type CString and let’s call it m_ctrlEdit. I did limit the max number of character in this string to 80 and you can modify it as you like.

C++
m_CtrlSlider.SetRange(0,13);
m_CtrlSlider.SetTicFreq(13);

After that, the simple programming practice will start when we will assign each button for a function to reverse the other one, which are Encrypt and Decrypt.

The below source code is what I did in every one of those handlers respectively.

C++
//The encrypt function
void CCaesarsEncrypterDlg::OnBnClickedEncrypt()
{
    // TODO: Add your control notification handler code here
    CString strTmp;
    int m_nKey,m_nlength;
    m_nKey=m_CtrlSlider.GetPos();
    UpdateData(TRUE);
    m_nlength=m_strText.GetLength();
    for(int i=0;i<m_nlength;i++)
    {
        char cTemp = m_strText[i];
        cTemp += m_nKey;
        strTmp += cTemp;
    }
    m_strText = strTmp;
    UpdateData(FALSE);
}
//The encrypt function---------------------------------------------
void CCaesarsEncrypterDlg::OnBnClickedDecrypt()
{
    // TODO: Add your control notification handler code here

    CString strTmp;
    int m_nKey,m_nlength;
    m_nKey=m_CtrlSlider.GetPos();
    UpdateData(TRUE);
    m_nlength=m_strText.GetLength();
    for(int i=0;i<m_nlength;i++)
    {
        char cTemp = m_strText[i];
        cTemp -= m_nKey;
        strTmp += cTemp;
    }
    m_strText = strTmp;
    UpdateData(FALSE);
}

One final note: This program is not done for encryption purposes but for learning how to read the sliders' controls and process it. Although this application doesn't rotate the last ASCII character to the first one like it should, you can play with it in the messenger :-).

Have a nice day!

History

  • 5th March, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Iraq Iraq
I had MSC in computer Engineering From IRAQ and i worked with telecommunication field with ZTE and ALCATEL but i look forward to practice programming alot. i had studied C, C++, and VC++. I hope that i will continue my studies to the PhD and to write a book about Computer Engineering in general.

Comments and Discussions

 
Generalinteresting Pin
Matrix is everywhere19-Oct-14 19:02
Matrix is everywhere19-Oct-14 19:02 
GeneralMy vote of 1 Pin
OPOI18-Oct-10 8:07
OPOI18-Oct-10 8:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.