반응형

* C# API 이용 인터넷 연결 체크 예제... (Internet Connect Check)

 

메인화면

전체 소스 코드

Form1.cs

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace CSharp_InternetConnectCheck
{
    public partial class Form1 : Form
    {
        [DllImport("wininet.dll", SetLastError = true)]
        private extern static bool InternetGetConnectedState(out int lpdwFlags, int dwReserved);

        [Flags]
        enum ConnectionStates
        {
            Modem = 0x1,
            LAN = 0x2,
            Proxy = 0x4,
            RasInstalled = 0x10,
            Offline = 0x20,
            Configured = 0x40,
        }

        System.Threading.Thread thMain;
        bool bCheck = false;

        public Form1()
        {
            InitializeComponent();

            CheckForIllegalCrossThreadCalls = false;
            thMain = new System.Threading.Thread(new System.Threading.ThreadStart (Thread_Tick));
            
            //백그라운드 스레드로 지정...
            thMain.IsBackground = true;
            button1.Text = "Internet Connect Check Start";
            bCheck = true;
            //스레드 시작...
            thMain.Start();
           


        }

        protected override void OnClosed(EventArgs e)
        {
      
            if (thMain != null)
            {
                //스레드가 돌고 있으면...
                if (bCheck )
                {
                    //강제종료...
                    thMain.Abort ();
                }
                //스레드가 일시 정지 상태이면...
                else
                {
                    //대기중인 스레드 종료...
                    thMain.Interrupt();
                }
                thMain = null;
            }

            GC.Collect();

            base.OnClosed(e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!bCheck)
            {
                bCheck = true;
                //일시정지된 스레드 다시 시작
                thMain.Resume();
                button1.Text = "Internet Connect Check Start";
                
                
            }
            else
            {
                bCheck = false;
                //스레드 일시정지
                thMain.Suspend();
                button1.Text = "Internet Connect Check Stop";
                
            }
        }

        /// <summary>
        /// 인터넷 연결 상태
        /// </summary>
        /// <returns></returns>
        public static bool Get_InternetConnectedState()
        {
            int flage = 0;
            return InternetGetConnectedState(out flage, 0);
        }

        private void Thread_Tick()
        {
            while (true )
            {
                if (Get_InternetConnectedState())
                {
                    label1.Text = "인터넷 연결이 되어 있습니다.";
                }
                else
                {
                    label1.Text = "인터넷 연결이 끊어 졌습니다.";
                }

                System.Threading.Thread.Sleep(1000);
            }
        }

    }
}

API 선언

 

 

 

* 예제 결과

 

 

https://kdsoft-zeros.tistory.com/22

 

[C#] 크로스 스레드 (Cross Thread) 예제

* 크로스 스레드 - 자신의 스레드가 아닌 다른 스레드가 그 컨트롤에 접근했었을 때 발생하는 오류 * 해결 방법 Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using..

kdsoft-zeros.tistory.com

 

반응형
반응형

* VBNET 텍스트 파일 읽기 (txt File Read) 한글 깨짐 방지 예제...

 

메인화면

전체 소스코드

Form1.vb

 

Public Class Form1

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

        Dim ofd As OpenFileDialog = New OpenFileDialog

        ofd.Filter = "텍스트 파일(*.txt) | *.txt"

        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            label1.Text = ofd.FileName

            '한글 깨짐
            'Using sr As System.IO.StreamReader = New System.IO.StreamReader(label1.Text)
            '1 첫번째 방법...
            'Using sr As System.IO.StreamReader = New System.IO.StreamReader(label1.Text, System.Text.Encoding.Default)
            '2 두번째 방법...
            Using sr As System.IO.StreamReader = New System.IO.StreamReader(label1.Text, System.Text.Encoding.GetEncoding(949))
                textBox1.Text = sr.ReadToEnd()
            End Using

        End If

    End Sub
End Class

 

 

*예제 결과

 

기본 파일 위치만 지정 했을 경우

 

파일 기본 위치 및 엔코딩 까지 지정했을 경우

 

 


https://kdsoft-zeros.tistory.com/34

 

[VBNET] File Create Delete Read Write Ex

* VBNET 파일 예제 Form1.vb Public Class Form1 Dim strCheckFolder As String = "" Dim strFileName As String = "Test.txt" Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventA..

kdsoft-zeros.tistory.com

 

반응형
반응형

* C# Text File Read 시 한글 깨짐 방지 예제...

 

메인화면

전체 소스코드

Form1.cs

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CSharp_File
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "TXT File(*.txt) | *.txt";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                label1.Text = ofd.FileName;

                //File Read...
                //한글 깨짐
                //using (System.IO.StreamReader sr = new System.IO.StreamReader(label1.Text))
                //1. 첫번째 방법
                //using (System.IO.StreamReader sr = new System.IO.StreamReader(label1.Text, System.Text.Encoding.Default ))
                //2. 두번째 방법
                using (System.IO.StreamReader sr = new System.IO.StreamReader(label1.Text, System.Text.Encoding.GetEncoding(949)))
                {
                    textBox1.Text = sr.ReadToEnd();
                }


            }


        }
    }
}

 

*예제 결과

 

기본 파일 위치만 지정 했을 경우
파일 기본 위치 및 엔코딩 까지 지정했을 경우

 

 

 

https://kdsoft-zeros.tistory.com/32

 

[C#] File Create & Delete & Read & Write 예제

* C# 파일 예제 Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Wind..

kdsoft-zeros.tistory.com

 

반응형
반응형

* VBNET API 를 이용한 컨트롤 (Control ) 모서리 둥글게 하기 예제...

 

메인화면

 

전체 소스 코드

Form1.vb

Public Class Form1

    Public Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
    Public Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long


    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

        '라벨의 크기를 전달 하고 15 정도로 모서리를 둥글게 한다.
        Dim ip As IntPtr = CreateRoundRectRgn(0, 0, label1.Width, label1.Height, 15, 15)
        Dim i As Integer = SetWindowRgn(label1.Handle, ip, True)

        '텍스트박스 크기를 전달 15 정도로 모서리를 둥글게 한다.
        Dim ip2 As IntPtr = CreateRoundRectRgn(0, 0, textBox1.Width, textBox1.Height, 15, 15)
        Dim i2 As Integer = SetWindowRgn(textBox1.Handle, ip2, True)

    End Sub
End Class

* 예제 결과

 

결과 화면

 

Button 클릭 시 위 그림의 빨간 테두리 안에 컨트롤들의 모서리가 둥글게 변하는 모습을 보실 수 있습니다.

 

https://kdsoft-zeros.tistory.com/124

 

[C#] [API] 컨트롤 (Control) 모서리 둥글게 만들기

* C# API 이용 컨트롤 (Control) 모서리 둥글게 만들기 예제... 전체 소스 코드 Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Draw..

kdsoft-zeros.tistory.com

 

반응형
반응형

* C# API 이용 컨트롤 (Control) 모서리 둥글게 만들기 예제...

 

메인화면

전체 소스 코드

Form1.cs

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace CSharp_RoundControl
{
    public partial class Form1 : Form
    {

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateRoundRectRgn(int x1, int y1, int x2, int y2,
                                                int cx, int cy);
        [DllImport("user32.dll")]
        private static extern int SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw);


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //15 로 전달 되어 있는 인자 -> 실제 모서리 둥글게 표현 하는 인자
            IntPtr ip = CreateRoundRectRgn(0, 0, label1.Width, label1.Height, 15, 15);
            int i = SetWindowRgn(label1.Handle, ip, true);

            IntPtr ip2 = CreateRoundRectRgn(0, 0, textBox1.Width, textBox1.Height, 15, 15);
            int i2 = SetWindowRgn(textBox1.Handle, ip2, true);

            this.Refresh();

        }
    }
}

 

* 예제 결과 

결과 화면

Button 클릭 시 위 그림의 빨간 테두리 안에 컨트롤들의 모서리가 둥글게 변하는 모습을 보실 수 있습니다.

반응형
반응형

* VBNET 숫자 (금액) 을 한글로 변화 ...

 

메인화면

 

전체 소스 코드

Form1.vb

 

Public Class Form1

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        label1.Text = CalToHan(textBox1.Text)
    End Sub

    Private Function NumberHan(ByVal iTmp As Integer) As String
        Dim sTmp As String = ""

        Select Case iTmp
            Case 0
                sTmp = ""
            Case 1
                sTmp = "일"
            Case 2
                sTmp = "이"
            Case 3
                sTmp = "삼"
            Case 4
                sTmp = "사"
            Case 5
                sTmp = "오"
            Case 6
                sTmp = "육"
            Case 7
                sTmp = "칠"
            Case 8
                sTmp = "팔"
            Case 9
                sTmp = "구"
        End Select
        Return sTmp
    End Function


    Private Function NumberUnit(ByVal iTmp As Integer) As String

        Dim sTmp As String = ""
        Select Case iTmp
            Case 0
                sTmp = ""
            Case 1
                sTmp = ""
            Case 2
                sTmp = "십"
            Case 3
                sTmp = "백"
            Case 4
                sTmp = "천"
            Case 5
                sTmp = "만"
            Case 6
                sTmp = "십"
            Case 7
                sTmp = "백"
            Case 8
                sTmp = "천"
            Case 9
                sTmp = "억"
            Case 10
                sTmp = "십"
            Case 11
                sTmp = "백"
            Case 12
                sTmp = "천"
        End Select

        Return sTmp

    End Function

    Private Function CalToHan(ByVal strTmp As String) As String
        Dim iTotalLength As Integer = strTmp.Length
        Dim iLength As Integer = iTotalLength
        Dim cTmp() As Byte = System.Text.Encoding.ASCII.GetBytes(strTmp)
        Dim sTmp As String = ""

        Dim iCount As Integer
        For iCount = 0 To iTotalLength - 1 Step iCount + 1

            If cTmp(iCount) - 48 <> 0 Then
                sTmp += NumberHan(cTmp(iCount) - 48) + NumberUnit(iLength)
            End If
            iLength -= 1
        Next
        sTmp += "원"
        Return sTmp

    End Function


End Class

*예제 결과

 

 

반응형
반응형

* C# 숫자(금액) 을 한글로 변환 하기 예제...

 

메인화면

전체 소스 코드

Form1.cs

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CSharp_숫자를한글로
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = CalToHan(textBox1.Text);
        }

        #region 숫자를 한글로 읽기 변환 ex) 1542500 => 일백오십사만이천오백원
        public  string CalToHan(string strTmp)
        {
            int iTotalLength = strTmp.Length;
            int iLength = iTotalLength;
            byte[] cTmp = Encoding.ASCII.GetBytes(strTmp);
            string sTmp = "";

            for (int iCount = 0; iCount < iTotalLength; iCount++)
            {
                if (cTmp[iCount] - 48 != 0)
                {
                    sTmp += NumberHan(cTmp[iCount] - 48) + NumberUnit(iLength);
                }
                iLength -= 1;
            }
            sTmp += "원";
            return sTmp;
        }
        private  string NumberUnit(int iTmp)
        {
            string sTmp = "";
            switch (iTmp)
            {
                case 0:
                    {
                        sTmp = "";
                        break;
                    }
                case 1:
                    {
                        sTmp = "";
                        break;
                    }
                case 2:
                    {
                        sTmp = "십";
                        break;
                    }
                case 3:
                    {
                        sTmp = "백";
                        break;
                    }
                case 4:
                    {
                        sTmp = "천";
                        break;
                    }
                case 5:
                    {
                        sTmp = "만";
                        break;
                    }
                case 6:
                    {
                        sTmp = "십";
                        break;
                    }
                case 7:
                    {
                        sTmp = "백";
                        break;
                    }
                case 8:
                    {
                        sTmp = "천";
                        break;
                    }
                case 9:
                    {
                        sTmp = "억";
                        break;
                    }
                case 10:
                    {
                        sTmp = "십";
                        break;
                    }
                case 11:
                    {
                        sTmp = "백";
                        break;
                    }
                case 12:
                    {
                        sTmp = "천";
                        break;
                    }
            }
            return sTmp;
        }
        private  string NumberHan(int iTmp)
        {
            string sTmp = "";

            switch (iTmp)
            {
                case 0:
                    {
                        sTmp = "";
                        break;
                    }
                case 1:
                    {
                        sTmp = "일";
                        break;
                    }
                case 2:
                    {
                        sTmp = "이";
                        break;
                    }
                case 3:
                    {
                        sTmp = "삼";
                        break;
                    }
                case 4:
                    {
                        sTmp = "사";
                        break;
                    }
                case 5:
                    {
                        sTmp = "오";
                        break;
                    }
                case 6:
                    {
                        sTmp = "육";
                        break;
                    }
                case 7:
                    {
                        sTmp = "칠";
                        break;
                    }
                case 8:
                    {
                        sTmp = "팔";
                        break;
                    }
                case 9:
                    {
                        sTmp = "구";
                        break;
                    }
            }
            return sTmp;
        }
        #endregion

    }
}

 

-> Ascii 코드 값 숫자 0 -> 48 , 1->49 ~ 

 

 

*예제 결과

반응형
반응형

* C# IP Ping Check 예제...

전체 소스 코드

Form1.cs

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CSharp_PingCheck
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            try
            {

                System.Net.NetworkInformation.Ping Pping = new System.Net.NetworkInformation.Ping();
                System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();

                options.DontFragment = true;

                //전송할 데이터를 입력
                string strData = "123123123";
                byte[] buffer = ASCIIEncoding.ASCII.GetBytes(strData);

                int iTimeout = 120;
                //IP 주소를 입력
                System.Net.NetworkInformation.PingReply PRreply = Pping.Send(System.Net.IPAddress.Parse(TextBox1.Text), iTimeout, buffer, options);

                if (PRreply.Status == System.Net.NetworkInformation.IPStatus.Success)
                {
                    MessageBox.Show("Ping Check Success...", "확 인", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }

                else
                {
                    MessageBox.Show("Ping Check Failed...", "확 인", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }

            catch(Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
    }
}

 

*예제 결과

 

 

 

반응형

+ Recent posts