当前位置: 首页>>代码示例>>C#>>正文


C# Form.Load事件代码示例

本文整理汇总了C#中System.Windows.Forms.Form.Load事件的典型用法代码示例。如果您正苦于以下问题:C# Form.Load事件的具体用法?C# Form.Load怎么用?C# Form.Load使用的例子?那么恭喜您, 这里精选的事件代码示例或许可以为您提供帮助。您也可以进一步了解该事件所在System.Windows.Forms.Form的用法示例。


在下文中一共展示了Form.Load事件的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Button1_Click

static int x = 200;
static int y = 200;

private void Button1_Click(System.Object sender, 
    System.EventArgs e)
{
    // Create a new Form1 and set its Visible property to true.
    Form1 form2 = new Form1();
    form2.Visible = true;

    // Set the new form's desktop location so it  
    // appears below and to the right of the current form.
    form2.SetDesktopLocation(x, y);
    x += 30;
    y += 30;

    // Keep the current form active by calling the Activate
    // method.
    this.Activate();
    this.Button1.Enabled = false;
}

// Updates the label text to reflect the current values of x 
// and y, which was were incremented in the Button1 control's 
// click event.
private void Form1_Activated(object sender, System.EventArgs e)
{
    Label1.Text = "x: "+x+" y: "+y;
    Label2.Text = "Number of forms currently open: "+count;
}

static int count = 0;

private void Form1_Closed(object sender, System.EventArgs e)
{
    count -= 1;
}

private void Form1_Load(object sender, System.EventArgs e)
{
    count += 1;
}
开发者ID:.NET开发者,项目名称:System.Windows.Forms,代码行数:42,代码来源:Form.Load

示例2: ComboBoxes

//引入命名空间
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

public class ComboBoxes : Form
{
  ComboBox cmb;
  Button btnDisplay;
  Button btnInsert;
  Button btnSelect;
  Label lblEdit;
  TextBox txtDisplay;
  Boolean boolChange = false;
  Boolean boolProcessed = false;

  public ComboBoxes()
  {
    Size = new Size(300,400);
    this.Load += new EventHandler(this_Load);

    cmb = new ComboBox();
    cmb.Parent = this;
    cmb.Location = new Point(10,10);
    cmb.Size = new Size(ClientSize.Width / 2, Height - 200);
    cmb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
    cmb.DropDownStyle = ComboBoxStyle.DropDown;    
    cmb.DropDownStyle = ComboBoxStyle.Simple;
    cmb.DropDownWidth = (int)(cmb.Width * 1.5);
    cmb.MaxDropDownItems = 12;
    cmb.MaxLength = 20;

    cmb.SelectionChangeCommitted += new EventHandler(cmb_SelectionChangeCommitted);
    cmb.Leave += new EventHandler(cmb_Leave);

    btnInsert = new Button();
    btnInsert.Parent = this;
    btnInsert.Text = "&Insert Item";
    btnInsert.Size = new Size((int)(Font.Height * .75) * btnInsert.Text.Length, cmb.Height);
    btnInsert.Location = new Point(cmb.Right + 10, cmb.Top);
    btnInsert.Click += new System.EventHandler(btnInsert_Click);

    lblEdit = new Label();
    lblEdit.Parent = this;
    lblEdit.BorderStyle = BorderStyle.Fixed3D;
    lblEdit.Location = new Point(cmb.Left, cmb.Bottom + 10);
    lblEdit.BackColor = Color.LightGray;
    lblEdit.Text = "";
    lblEdit.Size = new Size(cmb.DropDownWidth, Font.Height * 2);

    btnDisplay = new Button();
    btnDisplay.Parent = this;
    btnDisplay.Text = "&Display Items";
    btnDisplay.Size = new Size((int)(Font.Height * .75) * btnDisplay.Text.Length, cmb.Height);
    btnDisplay.Location = new Point(lblEdit.Left, lblEdit.Bottom + 10);
    btnDisplay.Click += new System.EventHandler(btnDisplay_Click);

    txtDisplay = new TextBox();
    txtDisplay.Parent = this;
    txtDisplay.Location = new Point(btnDisplay.Left, btnDisplay.Bottom + 10);
    txtDisplay.Multiline = true;
    txtDisplay.ReadOnly = true;
    txtDisplay.BackColor = Color.LightGray;
    txtDisplay.ScrollBars = ScrollBars.Vertical;
    txtDisplay.Text = "";
    txtDisplay.Size = new Size(cmb.DropDownWidth, 200);
    
    btnSelect = new Button();
    btnSelect.Parent = this;
    btnSelect.Text = "&Select 4";
    btnSelect.Size = new Size((int)(Font.Height * .75) * btnSelect.Text.Length, cmb.Height);
    btnSelect.Location = new Point(btnDisplay.Right + 10, btnDisplay.Top);
    btnSelect.Click += new System.EventHandler(btnSelect_Click);

      cmb.Items.Add("A");
      cmb.Items.Add("B");
      cmb.Items.Add("C");
      cmb.Items.Add("D");
      cmb.Items.Add("E");                        
    cmb.SelectedIndex = 0;   
  }

  static void Main() 
  {
    Application.Run(new ComboBoxes());
  }

  private void this_Load(object sender, EventArgs e)
  {
    cmb.TextChanged += new EventHandler(cmb_TextChanged);
    cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
  }

  private void cmb_TextChanged(object sender, EventArgs e)
  {
    if (!boolProcessed)
      lblEdit.Text = cmb.Text;
    boolChange = true;
  }    
  private void cmb_SelectedIndexChanged(object sender, EventArgs e)
  {
    if (boolChange)
    {
      boolChange = false;
      boolProcessed = false;
    }
  }

  private void cmb_SelectionChangeCommitted(object sender, EventArgs e)
  {
    if (boolChange)
      ProcessChange();
  }    

  private void cmb_Leave(object sender, EventArgs e)
  {
    if (boolChange)
    {
      ProcessChange();
      boolChange = false;
    }
  }    

  private void ProcessChange()
  {
    lblEdit.Text = "Edited: " + cmb.Text;
    boolProcessed = true;
  }

  private void btnDisplay_Click(object sender, EventArgs e)
  {
    string str = DateTime.Now.ToString() + "\r\n";
    foreach (object item in cmb.Items)
    {
      str += item.ToString() + "\r\n";
    }

    txtDisplay.Text = str;
  }    

  private void btnSelect_Click(object sender, EventArgs e)
  {
    cmb.Select(1,2);
  }    

  private void btnInsert_Click(object sender, EventArgs e)
  {
    if (cmb.FindStringExact(cmb.Text) != -1)
    {
      MessageBox.Show("'" + cmb.Text + "' already exists in the list.\r\n" + 
          "Will not be added again.",
          "Already Exists!");
    }
    else if (cmb.Text == "")
    {
      MessageBox.Show("There is nothing to add.","Nothing There");
    }
    else
    {
      cmb.Items.Add(cmb.Text);
    }
  }
}
开发者ID:C#程序员,项目名称:System.Windows.Forms,代码行数:165,代码来源:Form.Load


注:本文中的System.Windows.Forms.Form.Load事件示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。