C# 问题 ??

ILOVEYOUMYWIFERE 2008-12-11 05:15:01
winfrom中 窗体form1上我添加了一个label1,我想达到 鼠标单击label1,
在label1旁边显示出可选的下级目录(下级目录也用lable来显示),
在点击某一个下级目录时执行Click事件 怎么做??望高手指教!!
...全文
115 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
gislqy 2008-12-11
  • 打赏
  • 举报
回复

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

namespace WindowsApplication2
{
public partial class Form1 : Form
{
Label[] lblMenus;
bool blnClick;

public Form1()
{
InitializeComponent();
}

private void label1_Click(object sender, EventArgs e)
{
Rectangle rect;
Label lblMenu;

if (lblMenus == null)
{
lblMenus = new Label[8];
for (int i = 0; i < 8; i++)
{
lblMenu = new Label();
lblMenu.Text = "AAAA" + i.ToString();
if (i == 0)
{
rect = label1.Bounds;
}
else
{
rect = ((Label)lblMenus.GetValue(i - 1)).Bounds;
}
lblMenu.Left = rect.Left;
lblMenu.Top = rect.Bottom;
lblMenu.Size = new Size(label1.Width, label1.Height);
this.Controls.Add(lblMenu);
lblMenu.Click += new EventHandler(lblMenu_Click);
lblMenu.Show();
lblMenus.SetValue(lblMenu, i);
}
blnClick = true;
}

if (blnClick == true)
{
for (int i = 0; i < 8; i++)
{
((Label)lblMenus.GetValue(i)).Visible = true;
}
blnClick = false;
return;
}
else
{
for (int i = 0; i < 8; i++)
{
((Label)lblMenus.GetValue(i)).Visible = false;
}
blnClick = true;
}
}

void lblMenu_Click(object sender, EventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
for (int i = 0; i < 8; i++)
{
((Label)lblMenus.GetValue(i)).Visible = false;
}
blnClick = true;
Label lbl = sender as Label;
MessageBox.Show(lbl.Text);
}
}
}
ILOVEYOUMYWIFERE 2008-12-11
  • 打赏
  • 举报
回复
硬要喊用lable的嘛 谢谢了
zgke 2008-12-11
  • 打赏
  • 举报
回复
private void label1_Click(object sender, EventArgs e)
{
for (int i = 0; i != 8; i++)
{
Label Lab = new Label();
Lab.Name = i.ToString();
Lab.Click += new EventHandler(Lab_Click);
Lab.Text = i.ToString();
Lab.Size = label1.Size;
int Height = label1.Location.Y + (label1.Height * i);
Lab.Location = new Point(label1.Location.X + label1.Width, Height);

label1.Parent.Controls.Add(Lab);
}
}

void Lab_Click(object sender, EventArgs e)
{
Label _Label = (Label)sender;
MessageBox.Show(_Label.Name);
}

感觉还是 TreeView好用
swalp 2008-12-11
  • 打赏
  • 举报
回复

private void label1_Click(object sender, System.EventArgs e)
{
this.label2.Text = "2";
this.label3.Text = "3";
this.label4.Text = "4";
this.label5.Text = "5";
this.label6.Text = "6";
this.label7.Text = "7";
this.label8.Text = "8";
this.label9.Text = "9";
}

不是这样吗?
swalp 2008-12-11
  • 打赏
  • 举报
回复
如果你的下级目录的内容不是从数据库中抽出的那就直接负值啊。下级目录的label付对应的值啊。
swalp 2008-12-11
  • 打赏
  • 举报
回复
this.label2.Text = "3";
不是吗?你想要做什么?
ILOVEYOUMYWIFERE 2008-12-11
  • 打赏
  • 举报
回复
我有8个下级目录 用 lable来显示。关键是怎么让下级显示出来呢??
swalp 2008-12-11
  • 打赏
  • 举报
回复
你在第一个label的click事件给第二个label负值不能显示吗?
ILOVEYOUMYWIFERE 2008-12-11
  • 打赏
  • 举报
回复
我有8个lable 关键时怎么显示出下一个呢
特别 2008-12-11
  • 打赏
  • 举报
回复
很繁琐,没有特殊要求,推荐使用TreeView控件
swalp 2008-12-11
  • 打赏
  • 举报
回复
你的下级目录有很多条内容吗?
一条内容的话就点击第一个label的时候给第二个label负值。
再点击再执行click事件啊。

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧