C# UI +Treeview+dataGridView

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
//创建委托
public delegate string MyDelegate(string s);

public partial class Form1 : Form
{
//声明委托
public MyDelegate UpDataDelegate = null;

//声明窗体变量
public static Form1 form1;

public Form1()
{
InitializeComponent();


treeViewInitialize();
dataGridViewInitialize();
//传递窗体变量
form1 = this;
}

/// <summary>
/// tree格式
/// </summary>
private void treeViewInitialize()
{
treeView1.AllowDrop = true;
treeView1.ItemDrag += TreeView1_ItemDrag;
}

/// <summary>
/// dataGridView格式
/// </summary>
private void dataGridViewInitialize()
{

dataGridView1.AllowDrop = true;
dataGridView1.RowHeadersWidth = 25;
dataGridView1.ReadOnly = true;
for (int i = 1; i < 10; i++)
{
this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[0].Cells[0].Value = "";
}

}

[DllImport("user32.dll")]
public static extern bool ReleaseCapture();

[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;

/// <summary>
/// dataGridView将数据复制到指定单元格
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView2_DragDrop(object sender, DragEventArgs e)
{
string cellvalue = e.Data.GetData(typeof(string)) as string;
Point cursorLocation = dataGridView1.PointToClient(new Point(e.X, e.Y));

System.Windows.Forms.DataGridView.HitTestInfo hittest = dataGridView1.HitTest(cursorLocation.X, cursorLocation.Y);
if (hittest.ColumnIndex != -1
&& hittest.RowIndex != -1)
{
dataGridView1[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue;
}
}

/// <summary>
/// dataGridView接收数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}

/// <summary>
/// treeview控件拖动数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TreeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
TreeNode moveNode = (TreeNode)e.Item;
if(moveNode.Text=="输入"|| moveNode.Text == "输出")
{

}
else
{
DoDragDrop(moveNode.Text, DragDropEffects.Copy);
}

}
}


/// <summary>
/// 为了是主界面能够移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}

/// <summary>
/// 最小化窗体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}

/// <summary>
/// 关闭窗体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}

/// <summary>
/// tabControl 格式设置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
SolidBrush back = new SolidBrush(Color.FromArgb(45, 45, 48));
SolidBrush white = new SolidBrush(Color.FromArgb(122, 193, 255));
Rectangle rec = tabControl1.GetTabRect(0);
e.Graphics.FillRectangle(back, rec);
Rectangle rec1 = tabControl1.GetTabRect(1);
e.Graphics.FillRectangle(back, rec1);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
for (int i = 0; i < tabControl1.TabPages.Count; i++)
{
Rectangle rec2 = tabControl1.GetTabRect(i);
e.Graphics.DrawString(tabControl1.TabPages[i].Text, new Font("微软雅黑", 9), white, rec2, sf);
}
}

/// <summary>
/// 添加新行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{

this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[rowIndex].Cells[0].Value = "";
}

/// <summary>
/// 插入指定行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_Click(object sender, EventArgs e)
{

if (rowIndex >= 0)
{
this.dataGridView1.Rows.Insert(rowIndex);
this.dataGridView1.Rows[rowIndex].Cells[0].Value = "";
}
}

int rowIndex = 0;
/// <summary>
/// 删除指定行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{

if (!this.dataGridView1.Rows[rowIndex].IsNewRow)
{
this.dataGridView1.Rows.RemoveAt(rowIndex);
}
}

/// <summary>
/// 点击获取选择行索引
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
rowIndex = e.RowIndex;
}


private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
if (progressBar1.Value < progressBar1.Maximum)
{
progressBar1.Value += 4;
}
}

/// <summary>
/// dataGridView格式刷
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
dataGridView1.RowHeadersWidth - 4,
e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
dataGridView1.RowHeadersDefaultCellStyle.Font,
rectangle,
dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}

/// <summary>
/// treeView双击绑定数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (treeView1.Nodes.Count > 0)
{
Point point = treeView1.PointToClient(Control.MousePosition);

if (point.X < 55 && point.X > 44)
{
if (treeView1.SelectedNode.Text == "输入" || treeView1.SelectedNode.Text == "输出")
{

}
else
{
DataBindingForm dataBindingForm = new DataBindingForm();
dataBindingForm.ShowDialog();
}
}
}
}

/// <summary>
/// 传递数据方法
/// </summary>
public void method()
{
treeView1.SelectedNode.Text = UpDataDelegate(treeView1.SelectedNode.Text+":");
}

}

}

 

 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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

namespace WindowsFormsApp2
{
public partial class DataBindingForm : Form
{
public DataBindingForm()
{
InitializeComponent();
}

public string UPData(string s)
{

return s + treeView1.SelectedNode.Text;
}

private void button1_Click(object sender, EventArgs e)

{

label1.Text = string.Empty;

#region 递归
//1.获取TreeView的所有根节点
foreach (TreeNode tn in treeView1.Nodes)
{
DiGui(tn);
}
#endregion
}

private void DiGui(TreeNode tn)
{
//1.将当前节点显示到lable上
label1.Text += "数据节点:" + tn.Text + "\r\n";
foreach (TreeNode tnSub in tn.Nodes)
{
DiGui(tnSub);
}
}

private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (treeView1.Nodes.Count > 0)
{
if (treeView1.SelectedNode.Text == "输入" || treeView1.SelectedNode.Text == "输出"|| treeView1.SelectedNode.Text.Contains("工具:"))
{

}
else
{
Form1.form1.UpDataDelegate = UPData;
Form1.form1.method();
this.Close();
}
}
}
}
}

 

posted @   薛定谔的箱子  阅读(493)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示