因为今天是用的家里的电脑,用的不是retena屏的mac,因此稍后截图的时候大家看到的图片可能会有一些模糊,还请大家海涵。。。

兄弟们,我胡汉三又回来啦!



以下開始我们的第一个C#程序。我们之前已经通过命令行实现了一个简单的可运行程序,看起来不复杂。可是在如今这个讲求效率的时代,用命令行简直弱爆了。因此我们今天用上了高大上的VS。

下载的过程就不赘述了,我们直接看怎样开发一个WinForm程序吧。可能大家有点懵。WinForm是神马玩意,为什么要先学这个呢。

我仅仅能这么简单的跟您说一下:假设我们要做C#开发的话,最好的入门就是做一个WinForm程序。

他事实上类似于VC++的那种可运行程序,仅仅是依托了.Net框架而已。

好的,開始我们的WinForm程序吧:

1.创建程序

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvempoMTcx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

创建成功后看到文件夹结构例如以下:

如图所看到的的Program.cs就是程序的入口。让我们看一下里面的代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5.  
  6. namespace WindowsFormsApplication1
  7. {
  8. static class Program
  9. {
  10. /// <summary>
  11. /// 应用程序的主入口点。
  12. /// </summary>
  13. [STAThread]
  14. static void Main()
  15. {
  16. Application.EnableVisualStyles();
  17. Application.SetCompatibleTextRenderingDefault(false);
  18. Application.Run(new Form1());
  19. }
  20. }
  21. }

当中以下这句话指出了要启动的Form类:

  1. Application.Run(new Form1());

然后我们春藤摸瓜,找到了Form1相应的文件:

一共同拥有三个,从命名就能够大概看出他的意思,

Form1.cs用于写逻辑

Form1.Designer.cs用于画界面

Form1.resx用于管理里面的资源

我们看一下Form1.cs里面的代码:

  1. namespace WindowsFormsApplication1
  2. {
  3. public partial class Form1 : Form
  4. {
  5.  
  6. public Form1()
  7. {
  8. InitializeComponent();
  9. }
  10. }
  11. }

非常稀松寻常的代码,接下来我们拖控件到设计器里面

然后我们更改Form1.cs的代码例如以下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13. public partial class Form1 : Form
  14. {
  15.  
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. this.textBox1.Text = "c:\\2.jpg";
  20. }
  21.  
  22. private void button1_Click(object sender, EventArgs e)
  23. {
  24.  
  25. Uri uri = new Uri(this.textBox1.Text);
  26. Bitmap bitmap = new Bitmap(uri.LocalPath.ToString());
  27. this.pictureBox1.Image = bitmap;
  28. //图片uri
  29.  
  30. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://baidu.com");
  31.  
  32. request.Method = "get";
  33.  
  34. WebResponse response = request.GetResponse();
  35. // webBrowser1.Navigate("http://baidu.com");
  36. webBrowser1.DocumentText = response.ToString();
  37.  
  38. }
  39.  
  40. }
  41. }

要实现的功能是:点击button。将TextBox这个路径的图片显示到PictureBox这个控件中。

事实上设计器后台的代码也是能够看的,比方本设计器的代码Form.Designer.cs例如以下

  1. namespace WindowsFormsApplication1
  2. {
  3. partial class Form1
  4. {
  5. /// <summary>
  6. /// 必需的设计器变量。
  7.  
  8. /// </summary>
  9. private System.ComponentModel.IContainer components = null;
  10.  
  11. /// <summary>
  12. /// 清理全部正在使用的资源。
  13. /// </summary>
  14. /// <param name="disposing">假设应释放托管资源。为 true;否则为 false。</param>
  15. protected override void Dispose(bool disposing)
  16. {
  17. if (disposing && (components != null))
  18. {
  19. components.Dispose();
  20. }
  21. base.Dispose(disposing);
  22. }
  23.  
  24. #region Windows 窗口设计器生成的代码
  25.  
  26. /// <summary>
  27. /// 设计器支持所需的方法 - 不要
  28. /// 使用代码编辑器改动此方法的内容。
  29. /// </summary>
  30. private void InitializeComponent()
  31. {
  32. this.button1 = new System.Windows.Forms.Button();
  33. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  34. this.textBox1 = new System.Windows.Forms.TextBox();
  35. this.webBrowser1 = new System.Windows.Forms.WebBrowser();
  36. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
  37. this.SuspendLayout();
  38. //
  39. // button1
  40. //
  41. this.button1.Location = new System.Drawing.Point(640, 25);
  42. this.button1.Name = "button1";
  43. this.button1.Size = new System.Drawing.Size(157, 37);
  44. this.button1.TabIndex = 0;
  45. this.button1.Text = "button1";
  46. this.button1.UseVisualStyleBackColor = true;
  47. this.button1.Click += new System.EventHandler(this.button1_Click);
  48. //
  49. // pictureBox1
  50. //
  51. this.pictureBox1.Location = new System.Drawing.Point(12, 58);
  52. this.pictureBox1.Name = "pictureBox1";
  53. this.pictureBox1.Size = new System.Drawing.Size(508, 286);
  54. this.pictureBox1.TabIndex = 1;
  55. this.pictureBox1.TabStop = false;
  56. //
  57. // textBox1
  58. //
  59. this.textBox1.Location = new System.Drawing.Point(59, 25);
  60. this.textBox1.Name = "textBox1";
  61. this.textBox1.Size = new System.Drawing.Size(298, 21);
  62. this.textBox1.TabIndex = 2;
  63. //
  64. // webBrowser1
  65. //
  66. this.webBrowser1.Location = new System.Drawing.Point(537, 85);
  67. this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
  68. this.webBrowser1.Name = "webBrowser1";
  69. this.webBrowser1.Size = new System.Drawing.Size(250, 250);
  70. this.webBrowser1.TabIndex = 3;
  71. //
  72. // Form1
  73. //
  74. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  75. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  76. this.ClientSize = new System.Drawing.Size(815, 395);
  77. this.Controls.Add(this.webBrowser1);
  78. this.Controls.Add(this.textBox1);
  79. this.Controls.Add(this.pictureBox1);
  80. this.Controls.Add(this.button1);
  81. this.Name = "Form1";
  82. this.Text = "Form1";
  83. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
  84. this.ResumeLayout(false);
  85. this.PerformLayout();
  86.  
  87. }
  88.  
  89. #endregion
  90.  
  91. private System.Windows.Forms.Button button1;
  92. private System.Windows.Forms.PictureBox pictureBox1;
  93. private System.Windows.Forms.TextBox textBox1;
  94. private System.Windows.Forms.WebBrowser webBrowser1;
  95. }
  96. }

大功告成!看一下效果:

完...

C#中的Form,textBox,Bitmap,PictureBox,Button,WebBrowser的更多相关文章

  1. ASP.NET 中通过Form身份验证 来模拟Windows 域服务身份验证的方法

    This step-by-step article demonstrates how an ASP.NET   application can use Forms authentication to ...

  2. jQuery中设置form表单中action值与js有什么不同。。。。

    jQuery中设置form表单中action值与js有什么不同.... HTML代码如下: <form action="" method="post" i ...

  3. Django中的Form

    Form 一.使用Form Django中的Form使用时一般有两种功能: 1.生成html标签 2.验证输入内容 要想使用django提供的form,要在views里导入form模块 from dj ...

  4. Javascript中的Form表单知识点总结

    Javascript中的Form表单知识点总结 在HTML中,表单是由form元素来表示的,但是在javascript中,表单则由HTMLFormElement类型,此元素继承了HTMLElement ...

  5. django中的 form 表单操作

     form组件  1. 能做什么事?   1. 能生成HTML代码  input框   2. 可以校验数据   3. 保留输入的数据   4. 有错误的提示   1. 定义   from django ...

  6. 转 Django中的Form

    https://www.cnblogs.com/chenchao1990/p/5284237.html Form 一.使用Form Django中的Form使用时一般有两种功能: 1.生成html标签 ...

  7. 创建dynamics CRM client-side (十三) - 在HTML Web Resource中获取form elements & 获取外部js文件

    上一节我们讨论到创建HTML Web Resource. 但是纯HTML的页面不能满足我们的需求, 所以今天我们来做在HTML Web Resource中获取form elements Please ...

  8. 【译】.NET 5. 0 中 Windows Form 的新特性

    自从 Windows Form 在 2018 年底开源并移植到 .NET Core 以来,团队和我们的外部贡献者都在忙于修复旧的漏洞和添加新功能.在这篇文章中,我们将讨论 .NET 5.0 中 Win ...

  9. jQuery中设置form表单中action的值的方法

    下面介绍在jQuery中设置form表单中action的值的方法. $("#myFormId").attr("action", "userinfo.s ...

  10. html代码中的form参数是基本一致的

    由于pear的大多数模块仍处于开发当中,因此,这里列举的是随着php4.05一起发布的pear中的模块,需要注意的是,一些抽象类或者是基类(如mail.php,log.php,cache.php)没有 ...

随机推荐

  1. win下写任务提交给集群

    一,复制和删除hdfs中的文件 import org.apache.hadoop.fs.{FileSystem, Path} import org.apache.spark.{SparkConf, S ...

  2. windowsclient开发--使用、屏蔽一些快捷键

    每一个windowsclient都有自己的一些快捷键,有的是windows系统提供的. 今天就要与大家分享一下.在windowsclient开发过程中对按键的处理. ESC按键 Duilib这个库中, ...

  3. python 对比学习

    python和java面向对象的不同 1.属性和方法 java中类的属性(static)除外,对象全部独立拥有: 而python中类的属性,其实例对象一个字段都没有.底层是这么搞的: 对象object ...

  4. Visual studio C++ MFC之树形控件Tree Control

    背景 本篇旨在MSDN帮助文档下总结树形控件Tree Control的使用,并列出碰到的具体问题. 正文 树形控件Tree Control的类则是CTreeCtrl,具体成员对象详见链接,以下则描述一 ...

  5. 自行控制loadrunner的socket协议性能测试 (转)

    一前言 二任务的提出 三实现方案讨论 四技术要点讲解 如何开始录制一个最简单的收发数据包脚本 写日志文件 一行一行读数据包文件 字符串转换为十六进制数据包 发送自己定义的数据包 接收数据包到自定义缓冲 ...

  6. Python中json.loads解析包含\n的字符串会出错

    用python中的json.loads解析字符串,失败了. [解决过程] 1.调试了半天,终于发现,如果把其中的: "呵呵加那么多连接啊\n\n这个标准还是不错的\n\n给大家推荐一个更多的 ...

  7. 自定义ios NSLog

    #ifdef DEBUG #define MyLog(FORMAT, ...) fprintf(stderr,"Time:%s\t File:%s\t Methods:%s\t Line:% ...

  8. 写入文件,创建xunlei批量任务

    [php] <?php $arr = array(); $arr[] = 'http://s1.dwstatic.com/group1/M00/83/4A/834a040953745f52e8a ...

  9. Python技术公众号100天了

    公众号100天了,是个值得一提的日子! 我从2017年10月31日开始做这个公众号,到今天2018年2月7日,差不多100天时间 .虽然公众号很早就申请了,但直到去年10月31日,我才有真正把这个公众 ...

  10. Hive substr 函数截取字符串

    开发中,经常进行模糊查询或者进行截取字符串进行模糊匹配,常用的就是substr函数或者substring函数. 使用语法: substr(string A, int start),substring( ...