㈠ c#用户控件的使用
用户控件里,直接调用即可,参考如下:
usercontrol.ascx.cs中的处理:
1. 定义public的事件委托,如clickeventhandler;
2. 在usercontrol类中声明事件,如click;
3. 在usercontrol类中定义引发事件的方法,如onclick()方法;
4. 在usercontrol类的相关方法中调用引发事件的方法,如在button_click()中调用onclick()。
包含usercontrol的页面cs文件中的处理:
1. initializecomponent()中增加事件处理程序,采用findcontrol方法找到usercontrol;
2. 定义事件处理方法,在该方法中处理usercontrol的事件,如usercontrol_clicked()。
㈡ c#winform 用户控件的制作及在winform中的使用
下面是添加用户控件下面是在winform中的使用,你建立完成后在设计时会刷出来的:
㈢ winform怎么创建用户控件
打开visual studio(我这里用的是VS2010,不同的版本界面可能不同,不过操作大多一样的)
点击 文件 >> 新建 >>项目
在新建项目框中选择 Visual C# 下面的 windows 中的windows窗体控件库
新建的项目就入下图所示了
在上面的界面上 f7 看他的后台代码,如下,可以看出这个用户控件是从UserControl中派生出来的,下面我们来做一个最最简单的用户控件。
我们在左边的工具栏中拖一个Timer和Label控件过来,之后给Timer设置一个Tick的事件,同时在初始化这个用户控件的时候,给Timer设置Enable 为true,和Interval的值为1000,表示1秒中执行一次Tick事件。在Tick中给Label赋上当前时间的字符串。
F5运行,效果如下。这样就做好了一个用户控件,生成后,在bin目录debug中就能得到一个dll,拷贝到别的winform项目中就可以直接拖过来用了,和系统的控件一样,非常的方便。
㈣ C#winform 主窗体上的用户控件怎样调用主窗体的一个方法!
楼上几位的方法估计不行,因为要调用的方法只有一个Int参数,并没有事件驱动,所以在事件列表中看不见该函数。所以需要间接调用,方法如下:
private void yourFunction(int args){
....
}
双击按钮会直接进入该按钮的Click后台事件,在事件中调用你的函数。
选控件,打开属性窗口,选择事件页,在某个事件后,双击生成新的事件
private void Button1_click(object sender eventargs e){
yourFunction(123);
}
㈤ winform 用户控件A调用户控件B的方法(急)
其实很简单...在增加窗体里面声明个事件
public delegate void GetDataList();
public class UserControl1:UserControl{
public event GetDataList OnGetMainFormDataList = null;
public void GetList(){
if(OnGetMainFormDataList != null){
OnGetMainFormDataList();//调用主窗体的函数
}
}
}
在主窗体创建用户控件时.
UserControl1 userControl = new UserControl1();
userControl.OnGetMainFormDataList += new GetDataList(this.GetList);//和主窗体的函数绑定
userControl.Show();
㈥ C# winform如何用代码实现为用户控件添加控件(如lable、textbox)
Lable lable=new Lable();
label//属性设置..
Form1.Controls.Add(label);//添加到窗口容器中,大概这是这么个意思.动态生成控件.
㈦ c#Winform用户控件怎么在别的项目中使用
具体看是什么控件。
比如文本控件,label控件,combox控件有.text可以在属性里设置初始值
然后比如listbox控件可以添加item
㈧ C#Winform窗体如何打开用户控件
假设你有一个form1,里面有一个button1,你还有一个用户控件userControl1
你想通过button1打开userControl1
那你可以在button1的Click事件里面写
var userControl=new userControl1();
userControl.ShowDialog();
㈨ wpf中调用winform用户控件的方法
WPF的MainWindow的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
UserControl1 us = new UserControl1();
this.grid.Children.Add(us.addTextBox()); // 在前台的Grid里 添加Name属性,才可以使用 this.grid.........例如<Grid Name="grid">
}
}
}
winform的userControl的代码
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Controls;
namespace WpfApplication2
{
public partial class UserControl1 : System.Windows.Controls.Control
{
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_Load(object sender, EventArgs e)
{
addTextBox();
}
public System.Windows.Controls.TextBox addTextBox()
{
System.Windows.Controls.TextBox tx = new System.Windows.Controls.TextBox();
tx.Text = "111";
return tx;
}
}
}
UserControl1.Designer.cs 这个不改的话,你执行下,报错的地方删掉.
namespace WpfApplication2
{
partial class UserControl1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
#endregion
}
}
因为WPF和WINFORM的控件类型是不一样的,一个是controls里的,一个是forms里的,你在WPF里添加 的话,类型不同,参数不能转换
其实我这样用,已经用的不是WINFORM的控件了,相当于自己建个类,写个创建控件的方法而已