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 샌츠
,

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

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

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

다운로드를 하기 위해 사용해야 할 것은 ? 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 샌츠
,

데이터그리드를 테스트해보기 위해 간단한 테스트 코드를 작성해보았다.

목표는 구글 스프레드시트와 연동하는것.

구글 api도 연동해야 하고 해야할 것은 많지만, 한걸음씩 :)

 

프로젝트 환경

  • Visual Studio 2022
  • WPF 애플리케이션
  • 프로젝트이름 : DataGridWPF_231117
  • 프레임워크 : .NET 7.0(표준 용어 지원)

 

MainWindow.xaml

<Window x:Class="DataGridWPF_231117.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DataGridWPF_231117"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <DataGrid x:Name="dataGrid" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <!-- DataGridTextColumn을 사용하여 각 열을 정의합니다.
                Header 속성은 열의 헤더(제목)를 설정하고, Binding 속성을 통해 데이터 속성과 바인딩합니다. -->
                <DataGridTextColumn Header="이름" Binding="{Binding Name}" />
                <DataGridTextColumn Header="나이" Binding="{Binding Age}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

 

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DataGridWPF_231117
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // 데이터 모델 클래스 정의
        private class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
        public MainWindow()
        {
            InitializeComponent();
            Loaded += dataGrid_init;
        }

        private void dataGrid_init(object sender, RoutedEventArgs e)
        {
            // 샘플 데이터 생성
            ObservableCollection<Person> people = new ObservableCollection<Person>
            {
                new Person { Name = "홍길동", Age = 30 },
                new Person { Name = "김철수", Age = 25 },
                new Person { Name = "이영희", Age = 28 },
            };

            // 데이터 그리드에 데이터 바인딩
            dataGrid.ItemsSource = people;
        }
    }
}

 

 

끝.

Posted by 샌츠
,