• 窗体间传值练习


       最近在论坛上看到不少关于两个窗体之间传值的问题。也看过一篇博客上关于两个窗体传值的最佳方案。自己做了一个类似记事本搜索功能,大概使用了两种较为常用的窗体间传值方法。一是将类及其中的控件设置成public,在另已窗体中使用控件的属性。二是通过代理,出发事件,让另一窗体接受数据。

    效果图如下:


        下面介绍文本搜索工具。新建两个Form,分别命名为MyText和searchForm,当然修饰符用public。MyText窗体中一个TextBox,将TextBox设置为public static型,Multiline为true,一个按钮。searchForm需要一个按钮,一个TextBox,一个按钮。下面开始贴代码。

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

    namespace  SearchText

    {
        public partial class MyText : Form
        {
            public MyText()
            {
                InitializeComponent();
            }

            public searchForm seach;//申明变量
            bool closed = true;//closed判断是否已存在searchForm的实例
            private void btnSearch_Click(object sender, EventArgs e)
            {
                if (closed)//实例化searchForm
                {
                    closed = false;
                    seach = new searchForm();
                    seach.TopMost = true;
                    //注册事件,在searchFormclosed之后通知MyText已经没有searchForm实例了
                    seach.FormClosed += new FormClosedEventHandler(seach_FormClosed);
                    seach.StartPosition = FormStartPosition.Manual;
                    seach.DesktopLocation = new Point(this.Right - 200, this.Top - 20);
                    //SearchPhrase为searchForm类中的属性,用于接受MyText中选中要搜索的文本
                    seach.SearchPhrase = txtDocument.SelectedText;
                    seach.Show();
                }
                else//已存在searchForm的实例就激活并赋予焦点
                {
                    seach.SearchPhrase = txtDocument.SelectedText;
                    seach.Activate();
                }

            }

            void seach_FormClosed(object sender, FormClosedEventArgs e)
            {
                closed = true;
            }
        }
    }

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

    namespace SearchText

    {
        public partial class searchForm : Form
        {
            public searchForm()
            {
                InitializeComponent();
            }

            //用于接受需要搜索的文本
            public string SearchPhrase
            {
                set
                {
                    txtSearch.Text = value;
                }
            }

            private void btnSearch_Click(object sender, EventArgs e)
            {
                //定位选中文本的开始位置
                int textStart=0;
                string textDoc = MyText.txtDocument.Text;
                string searchText = this.txtSearch.Text;
                int ln = searchText.Length;
                if (ln > 0)
                {
                    int selectStart = MyText.txtDocument.SelectionStart;
                    if (selectStart >= MyText.txtDocument.Text.Length)//需搜索的文本超出全部文本长度
                    {
                        textStart = 0;
                    }
                    else//否则搜索下次出现需要搜索文本的位置
                    {
                        textStart = selectStart + ln;
                        textStart = textDoc.IndexOf(searchText,textStart);
                    }
                    if (textStart >= 0)
                    {
                        //设置MyText中的选中文本,起始位置
                        MyText.txtDocument.SelectionStart = textStart;
                        //设置MyText中选中文本的长度
                        MyText.txtDocument.SelectionLength = txtSearch.Text.Length;
                        //让MyText的TextBox获得焦点,否则焦点在searchForm中,文本无法被选中
                        MyText.txtDocument.Focus();
                    }
                }
            }
        }
    }

    总结:通过次文本搜索工具熟悉了窗体间传值的两种方法,当然相信多实践还会有更多中方式。另外特别需要注意的是最后把焦点给MyText的TextBox。本文参考《C#和.Net核心技术》

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    ElasticSearch Java API 增删改查操作
    ElasticSearch Java API连接报错NoNodeAvailableException解决方案
    Elasticsearch 使用Kibana实现基本的增删改查+mget批量查询+bulk批量操作
    Elasticsearch CentOS6.5下安装ElasticSearch6.2.4+elasticsearch-head+Kibana
    Elasticsearch 核心概念:NRT+索引+分片+副本
    Solr solrj4与solrj-7.7.2操作solrcloud
    Solr Centos6.5下搭建solr-7.7.2集群solrcloud+DIH操作
    分层图【p2939】[USACO09FEB]改造路Revamping Trails
    NOIP2018提高组模拟题(二)
    树链剖分【p3178】[HAOI2015]树上操作
  • 原文地址:https://www.cnblogs.com/gxwang/p/4940887.html
Copyright © 2020-2023  润新知