TryParse 메서드

입력한 문자열이 숫자로 변환할 수 있는 문자열인지 체크하여 
숫자로 변환이 가능하다면 True, 문자등이 포함되어 있어서 숫자로 변환이 불가능한다면 False를 반환한다.

 

  • 아래는 가단하게 테스트 해볼 수 있는 코드
using System;

namespace StringConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("숫자를 입력해주세요: ");
            if (int.TryParse(Console.ReadLine(), out int intInput))
                Console.WriteLine(intInput);
            else
                Console.WriteLine("{0} : 입력한 값을 다시 확인해주세요.");

            // 프로그램이 종료되지 않도록 추가
            Console.WriteLine("프로그램을 종료하려면 아무 키나 누르세요...");
            Console.ReadLine(); //엔터 키를 누를 때까지 대기.
        }
    }
}

'C#' 카테고리의 다른 글

[C#] 메인폼에서 다른폼 열기  (0) 2024.04.26
[C#] 파일 다운로드기능  (0) 2024.01.05
[C#] 버튼 일괄 숨기기 / 보이기  (0) 2023.11.23
Posted by 샌츠
,

메인폼의 메뉴에서 다른 서브폼을 열었을때, 가장 기본적인 코드로 폼을 열어보면 서브창만 움직일 수 있고

메인폼은 움직일 수가 없다.

using System;
using System.Windows.Forms;

namespace Namespace
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void menuButton_Click(object sender, EventArgs e)
        {
            //폼을 생성하고 보여줍니다.
            frmSub subForm = new frmSub();
            subForm.ShowDialog(); // 모달 다이얼로그로 표시합니다.
        }
    }

    public partial class frmSub : Form
    {
        public frmSub()
        {
            InitializeComponent();
        }
    }
}

 

아래의 코드는 메뉴의 버튼을 눌렀을때, 서브폼이 팝업되고 메인폼도 제어가 가능한 코드이다.

그리고 sub폼이 열린 상태에서 메뉴의 버튼을 다시 한번 누르면 에러가 발생한다.

이것을 방지하기 위해서는 subForm이 실행되엇는지 체크하면 된다.

using System;
using System.Windows.Forms;

namespace Namespace
{
    public partial class MainForm : Form
    {
        private frmSub subForm; // frmSub 폼의 인스턴스를 보관할 멤버 변수 선언

        public MainForm()
        {
            InitializeComponent();
        }

        private void menuButton_Click(object sender, EventArgs e)
        {
            if (subForm == null || subForm.IsDisposed)
            {
                // frmSub 폼이 없거나 이미 닫혔다면 새로 생성.
                subForm = new frmSub();
                subForm.FormClosed += (s, args) => subForm = null; // 폼이 닫힐 때 참조를 제거.
            }

            if (!subForm.Visible)
            {
                subForm.Show(this); // MainForm의 소유자로 frmSub 폼을 표시.
            }
        }
    }

    public partial class frmSub : Form
    {
        public frmSub()
        {
            InitializeComponent();
        }
    }
}

'C#' 카테고리의 다른 글

[C#] 문자열을 숫자로 변환  (0) 2024.05.23
[C#] 파일 다운로드기능  (0) 2024.01.05
[C#] 버튼 일괄 숨기기 / 보이기  (0) 2023.11.23
Posted by 샌츠
,

매뉴얼 다운로드 기능을 구현하기 위해 프로젝트 폴더에 매뉴얼 파일을 넣어두었다.

그리고 프로그램에서 매뉴얼 다운로드를 할 경우 프로젝트 폴더 안에 있는 매뉴얼을 여는 것이 아닌 

다운로드 폴더에 다운로드를 한 뒤에 열고자 한다.

다운로드를 하기 위해 사용해야 할 것은 ? File.Copy

try
    {
        string sourcePath = "path/to/your/Comprehensive Economic Index_Manual.pdf"; // Replace with the actual path
        string downloadsFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Downloads";
        string destPath = Path.Combine(downloadsFolder, "Comprehensive Economic Index_Manual.pdf");

        File.Copy(sourcePath, destPath, true); // Set to 'true' to overwrite if the file already exists

        // Optionally, if you want to inform the user
        MessageBox.Show("File downloaded to " + destPath);
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
        MessageBox.Show("Error in downloading the file: " + ex.Message);
    }

'C#' 카테고리의 다른 글

[C#] 문자열을 숫자로 변환  (0) 2024.05.23
[C#] 메인폼에서 다른폼 열기  (0) 2024.04.26
[C#] 버튼 일괄 숨기기 / 보이기  (0) 2023.11.23
Posted by 샌츠
,

여러 폼에 초기화 등의 버튼을 만들었을 경우.

메뉴에서 버튼을 클릭시 여러 폼에 있는 버튼들을 일괄로 숨기거나 보이는 기능을 만들어 보았다.

활용은 각자 알아서 .


클래스를 정의하고 각 폼에서 해당 버튼들을 클래스의 리스트변수에 담아두고 

메뉴버튼을 클릭시 해당 리스트에 담겨져 있는 버튼들의 Visible 상태값을 변경하는 코드이다.


  • 클래스정의
using System.Collections.Generic;
using System.Windows.Forms;

public class ButtonManager
{
    private static List<Button> controlButtons = new List<Button>();

    public static void RegisterButton(Button ctlButton)
    {
        controlButtons.Add(ctlButton);
    }

    public static void ToggleButtonsVisibility(bool showHideButtons)
    {
        foreach (Button ctlButton in controlButtons)
        {
            ctlButton.Visible = showHideButtons;
        }
    }
}

 

  • 각 파일 메인 함수
{
	InitializeComponent();
	BtnControlClass.ButtonManager.RegisterButton(ctlButton);
}

'C#' 카테고리의 다른 글

[C#] 문자열을 숫자로 변환  (0) 2024.05.23
[C#] 메인폼에서 다른폼 열기  (0) 2024.04.26
[C#] 파일 다운로드기능  (0) 2024.01.05
Posted by 샌츠
,