C# Tips

Code/Mobile 2017.11.02 05:15


* 형변환
    - 숫자 -> 문자
        int num = 100;
        string str = num.ToString();//Exception 발생
        str = Convert.ToString( num );//Exception 발생
        int.TryParse(str, out num);//Exception 없음, 추천
       

    - 문자 -> 숫자   
        string str = "100";
        int num = int.Parse( str );
        num = Convert.ToInt32( str );
       


//===========
* 문자열 배열
    List<string> as1 = new List<string>();

as1.Clear();//RemoveAll 대신 사용

    as1.Add("qwe");



//===========

* UI 다루기

            //탭 변경
            //tabMain.SelectTab(1);

            //폼 보이기
            //frmCalc form2 = new frmCalc();            form2.Visible = true;



//================
* 이벤트 수동 추가

    - 폼 Load 함수에 추가
private void frmMain_Load(object sender, EventArgs e)
{
    ...
    textAnswer.KeyDown += new KeyEventHandler(textAnswer_KeyDown);
    ...
}


private void textAnswer_KeyDown(object sender, KeyEventArgs e)
{
     if (e.KeyCode == Keys.Enter) {
        //enter key is down
        ...
     }
}


//===============================
* 창 크기 설정 저장, 부르기(복원)
    Solution Explorer -> Properties -> Settings.settings
    - 이름 추가, 형식 지정



    - 폼 이벤트 추가: Load,  FormClosing


private void Form1_Load(object sender, EventArgs e)
{
    //설정 부르기
    if (Properties.Settings.Default.Size.Width > 0) {
        Location = Properties.Settings.Default.Location;
        Size = Properties.Settings.Default.Size;
    }
}


private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    //설정 저장      
    if (this.WindowState == FormWindowState.Normal) {
        Properties.Settings.Default.Location = this.Location;
        Properties.Settings.Default.Size = this.Size;
        Properties.Settings.Default.Save();
     }   
}


//===============================
* 글자 읽기(TTS)


using System.Speech.Synthesis;


SpeechSynthesizer m_synth;

       private int VoiceInit(int nMode)
        {
            //음성 초기화
            m_synth = new SpeechSynthesizer();
            m_synth.Volume = 100;  // 0...100
            m_synth.Rate = 0;     // -10...10


//목소리 국적(국가 언어) 선택

            VoiceInfo info = null;
            int i = 0;
            foreach (InstalledVoice voice in m_synth.GetInstalledVoices()) {
                info = voice.VoiceInfo;
                i++;
                if (info.Culture.ToString() == "en-US") { //"ko-KR"
                    break;
                }
            }

            if (info != null) {
                m_synth.SelectVoice(info.Name);
            }

            ////읽기
            //m_synth.Speak("Hello World");

            // Asynchronous
            //m_synth.SpeakAsync("Hello World");

            return 1;
        }



Posted by 코덴스

댓글을 달아 주세요