C#窗體--Label、Picturebox、LinkLabel

Herry-白愛民發表於2018-06-13

label中用來切換圖片來進行切換暫停: 準備開始暫停圖片
用處:label是可以用來書寫文字顯示,圖片顯示

相關屬性:
行為Enabled:控制是否可用
外觀:backcolor:控制背景顏色
字型:font:載入字型
佈局:dock:繫結控制元件邊框
設計:LOcked:移動控制元件調整大小


//思路:利用image屬性調取圖片位置,設定圖片的切換開關或者設定tag的值。

 bool flag = false;
        private void labtext_Click(object sender, EventArgs e)
        {
            if (flag )
            {
                labtext.Image = Image.FromFile(@"絕對位置");
                flag = false;
            }
            else
            {
                labtext.Image = Image.FromFile(@"絕對位置");
                flag = true;
            }
        } 

方法二:首先將tag屬相變成1if (labtext.Tag.ToString()=="1")
            {
                labtext.Image = Image.FromFile(@"絕對位置");
                labtext.Tag = "2";
            }
            else if (labtext.Tag.ToString =="2")
            {
                labtext.Image = Image.FromFile(@"絕對位置");
                labtext.Tag = "1";
            }

顯示下一張圖片和上一張圖片: picturebox控制元件
用處:用來載入圖片

  string[] images;
        private void Form1_Load(object sender, EventArgs e)
        {
            string path = @"E:\我的應用資料\計算機專案\軟體工程專案之CS學習\02 C#\傳智播客基礎實訓3\20121109C#基礎\資料\img";
            images = Directory.GetFiles(path,"*.jpg");
            picImage.Image = Image.FromFile(images[0]);

        }
        int i = 0;
        private void btnRight_Click(object sender, EventArgs e)
        {
            i++;
            if (i==images.Length)
            {
                i = 0;
            }
            picImage.Image = Image.FromFile(images[i]);
        }

        private void btnLift_Click(object sender, EventArgs e)
        {
            i--;
            if (i<0)
            {
                i = images.Length - 1;
            }
            picImage.Image = Image.FromFile(images[i]);
        }

這裡寫圖片描述


超連結文字

//用於顯示網頁,開啟軟體等。
 private void linkname_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http:WWW.aidu.com");
        }


賬號登入練習

這裡寫圖片描述

//登入成功後調出文字框並設定自控換行:

   private void Form1_Load(object sender, EventArgs e)
        {
            btnSave.Visible = false;//隱藏儲存按鈕
            btnWordWarp.Visible = false;//隱藏自動換行按鈕
            txtText.Visible = false;//隱藏文字框;

            txtText.ScrollBars = ScrollBars.Both;//更改文字框屬性為both
            txtText.WordWrap = false;//使文字框不能換行
        }


        //判斷登入
        private void btnOk_Click(object sender, EventArgs e) //登入事件
        {
            if (txtName.Text=="admin"&&txtPwd.Text=="12345")
            {

                //隱藏一些不用控制元件

                labName.Visible = false;
                labPwd.Visible = false;
                txtName.Visible = false;
                txtPwd.Visible = false;
                btnOk.Visible = false;
                linkPwd.Visible = false;
                linkZheCe.Visible = false;


                //顯示需要的控制元件。
                btnSave.Visible = true ;
                btnWordWarp.Visible = true ;
                txtText.Visible = true ;


            }
            else
            {
                MessageBox.Show("賬號密碼錯誤");
                txtName.Text="";
                txtPwd.Text = "";
                txtName.Focus();//設定游標

            }
        }


        //判斷能否換行
        private void btnWordWarp_Click(object sender, EventArgs e)
        {
            if (btnWordWarp.Text=="自動換行")
            {
                txtText.WordWrap = true;
                btnWordWarp.Text = "取消自動換行";
            }
            else if (btnWordWarp.Text=="取消自動換行")
            {
                txtText.WordWrap = false ;
                btnWordWarp.Text = "自動換行";
            }

        }

//儲存路徑
    private void btnSave_Click(object sender, EventArgs e)
    {
        string path = @"絕對地址";  //存放位置
                File.WriteAllText(path, txtText.Text); //複製過去文字 (需要引用io)
                MessageBox.Show("儲存成功");

     }

相關文章