Click here to Skip to main content
Click here to Skip to main content
Go to top

Using Slider Control in a Good Example

, 5 Mar 2008
Rate this:
Please Sign up or sign in to vote.
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.

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.

//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 Smile | :) .

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)

Share

About the Author

Kusay J. Mohammed
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

 
GeneralMy vote of 1 PinmemberOPOI18-Oct-10 8:07 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 5 Mar 2008
Article Copyright 2008 by Kusay J. Mohammed
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid