C#循环控制winform中的标签label

C#循环控制winform中的标签label,第1张

private void button1_Click(object sender, EventArgs e)

{

for (int t = 30t <+120t = t + 15)

{

Label lb = GetLabel(t)

if (lb != null)

{

lb.Text = "你好"

lb.ForeColor = Color.Red

}

}

}

private Label GetLabel(int t)

{

int index = (t - 30) / 15 + 1

foreach (Control c in this.Controls)

{

if (c.Name == ("label" + index))

{

return (Label)c

}

}

return null

}

在工具箱输入【navigationPane】,然后拖拽到界面

②navigationPane面板的简要说明

③修改标签名称和设置显示标签图标

点击【Pages】打开面板进行如下 *** 作:

2.1.2、代码添加标签和窗体内容

①新建一个需添加到标签的窗体,如下所示:

②编写代码添加标签和标签对应的窗体

//初始化

private void InitNavigationPane()

{

//指定默认选中第一个标签

navigationPane1.SelectedPage = navigationPage1

//添加一个新标签

NavigationPage NewNavigationPage = new NavigationPage()

NewNavigationPage.Name = "newNavigationPage"

NewNavigationPage.Caption = "添加的标签"

navigationPane1.Pages.Add(NewNavigationPage)

//给该新标签添加界面

NewTagForm newTagForm = new NewTagForm()

//一定要设置(否则会报错:无法将顶级控件添加控件)

newTagForm.TopLevel = false

NewNavigationPage.Controls.Add(newTagForm)

newTagForm.Show()

}

登录后复制

③实现效果

2.2、NavBarControl实现步骤

2.2.1、界面布局

①在工具箱输入navBarControl,然后拖拽到界面

②NavBarControl面板的简要说明

③添加导航栏的标签组、及其组包含的标签

点击设计器【Run Designer】进行如下 *** 作:

2.2.2、代码添加标签组、标签组包含的标签和标签事件

①代码实现给标签添加事件(且点击的标签名称是【01】时创建一个新的组、及其给新组添加一个标签)

//初始化

private void InitNavBarControl()

{

navBarItem1.LinkClicked += Item_Click

navBarItem2.LinkClicked += Item_Click

navBarItem3.LinkClicked += Item_Click

navBarItem4.LinkClicked += Item_Click

navBarItem5.LinkClicked += Item_Click

navBarItem6.LinkClicked += Item_Click

navBarItem7.LinkClicked += Item_Click

navBarItem8.LinkClicked += Item_Click

navBarItem9.LinkClicked += Item_Click

navBarItem10.LinkClicked += Item_Click

}

/// <summary>

/// 导航栏子项目的点击事件

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void Item_Click(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)

{

MessageBox.Show("当前点击的标签是:"+e.Link.Caption)

if (e.Link.Caption=="01")

{

ActiveChangeNavigate()

}

}

//动态更改导航栏

private void ActiveChangeNavigate()

{

//新建一个组

NavBarGroup navBarGroup = new NavBarGroup()

navBarGroup.Name = "NewBarGruop"

navBarGroup.Caption = "动态添加的分组"

navBarGroup.ImageOptions.SmallImageIndex = -1

navBarGroup.ImageOptions.LargeImageIndex = -1

//将该组添加到导航中

navBarControl1.Groups.Add(navBarGroup)

//新建一个标签

NavBarItem navBarItem = new NavBarItem()

navBarItem.Name = "NewBarItem"

navBarItem.Caption = "动态添加的标签"

navBarItem.ImageOptions.SmallImageIndex = -1

navBarItem.ImageOptions.LargeImageIndex = -1

navBarItem.LinkClicked += Item_Click

//添加标签到组中

navBarControl1.Items.Add(navBarItem)

//添加标签到一个分组中

navBarGroup.ItemLinks.AddRange(new NavBarItemLink[] {

new NavBarItemLink(navBarItem)

})

}

登录后复制

②实现效果

三、参考资料

Navigation Pane | WinForms Controls | DevExpress Documentation

https://docs.devexpress.com/WindowsForms/114555/Controls-and-Libraries/Navigation-Controls/Navigation-Pane

Navigation Pane | WinForms Controls | DevExpress Documentation

https://docs.devexpress.com/WindowsForms/115232/controls-and-libraries/pdf-viewer/navigation/navigation-pane?v=18.1&f=BaseControlPainter Navigation Bar | WinForms Controls | DevExpress Documentation

https://docs.devexpress.com/WindowsForms/4870/controls-and-libraries/navigation-controls/navigation-bar

NavBarControl.LinkClicked Event | WinForms Controls | DevExpress Documentation

https://docs.devexpress.com/WindowsForms/DevExpress.XtraNavBar.NavBarControl.LinkClicked

导航栏

NavigationPane

NavBarControl

京东家电年货节来袭,以旧换新,立减10%

京东

广告

DevExpress ExpressPageControl v2.37 for Delphi/BCB (含完整源代码)

15下载·1评论

2008年7月24日

DevExpress中实现动态创建菜单栏和导航栏

1492阅读·0评论·0点赞

2021年6月23日

[DevExpress使用随笔]之NavBarControl控件(一)

6820阅读·0评论·0点赞

2017年11月23日

js基础1图片切换01

85阅读·2评论·0点赞

2020年9月23日

UI控件DevExpress WinForms v21.2 - HTML &CSS模板、支持Windows 11

470阅读·0评论·0点赞

2021年11月11日

Dev控件使用之导航控件NavBarControl

435阅读·0评论·3点赞

2022年9月2日

京东家电年货节来袭,以旧换新,立减10%,超值!

京东

广告

DevExpress 新专案导航画面实现窗体导航

351阅读·0评论·0点赞

2021年7月9日

DevExpress中实现代码动态添加导航栏Tabpage到XtraTabControl

855阅读·1评论·0点赞

2021年6月22日

DevExpress.XtraNavBar:制作软件导航栏

5570阅读·0评论·0点赞

2012年11月6日

配置Windows(Win7)导航面板(Navigation Pane)隐藏/显示收藏夹、库、网络等

1.3W阅读·0评论·1点赞

2014年12月27日

Toolbar popupmenu 样式设置

4016阅读·1评论·1点赞

2017年1月24日

WinForm应用界面开发入门指南 - Office启发式用户界面

269阅读·0评论·0点赞

2021年5月13日

devexpress皮肤设置

174阅读·0评论·0点赞

2015年10月17日

DevExpress(一):主界面设计

7586阅读·0评论·4点赞

2019年2月24日

NavBarControl控件 2015-07-23 16:56 2人阅读 评论(0) 收藏...

4阅读·0评论·0点赞

2015年7月23日

DevExpress中导航栏添加及导航栏事件添加

4162阅读·3评论·0点赞

2018年3月9日

Winform DevExpress控件库(三) 使用NavBarControl控件定制导航栏

5.1W阅读·2评论·4点赞

2017年1月10日

ExpressNavigationPack导航和布局管理控件

244阅读·0评论·0点赞

2022年2月16日

界面控件DevExpress WinForms MVVM入门指南——主视图导航

542阅读·0评论·0点赞

2022年3月24日

干货!DevExpress Winform界面效果实现总结(Part 3)

1172阅读·0评论·0点赞

花了几分钟写的,没有进行代码优化,不过你说的功能都实现了。`

请先在数据库中添加一个名字为test的数据库,一个test的表

表中的字段为:id,name,address

数据库的用户名为sa,密码为sa

Form1.cs的代码:

using System

using System.Collections.Generic

using System.ComponentModel

using System.Data

using System.Drawing

using System.Text

using System.Windows.Forms

using System.Data.SqlClient

namespace WindowsApplication4

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent()

}

private void 数据增删改查_Load(object sender, EventArgs e)

{

//this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect

//this.textBox1.Text = "Provider=SQLOLEDB.1Password=saPersist Security Info=TrueUser ID=saInitial Catalog=testData Source=."

this.textBox1.Text = "Server=.pwd=sauid=sadatabase=test"

}

private void button5_Click(object sender, EventArgs e)

{

SqlConnection con = new SqlConnection(this.textBox1.Text)

con.Open()

SqlDataAdapter sqlda = new SqlDataAdapter("select * from test", con)

DataTable dt = new DataTable()

sqlda.Fill(dt)

this.dataGridView1.DataSource = dt

}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)

{

//if (MessageBox.Show("你是否要更新数据?", "x", MessageBoxButtons.OKCancel) == DialogResult.OK)

//{

//MessageBox.Show("OK")

//SqlConnection con = new SqlConnection(this.textBox1.Text)

//con.Open()

//SqlCommand com = new SqlCommand("update test set address = , con)

//com.ExecuteNonQuery()

// //SqlDataReader dr = com.ExecuteReader()

//}

//else

//{

//MessageBox.Show("NO")

//}

}

private void dataGridView1_SelectionChanged(object sender, EventArgs e)

{

//MessageBox.Show("xxx")

}

private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)

{

//MessageBox.Show("xxx")

}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

{

if (this.dataGridView1.SelectedRows.Count != 0)

{

this.textBox7.Text = this.dataGridView1.SelectedRows[0].Cells["id"].Value.ToString()

this.textBox8.Text = this.dataGridView1.SelectedRows[0].Cells["name"].Value.ToString()

this.textBox9.Text = this.dataGridView1.SelectedRows[0].Cells["address"].Value.ToString()

}

}

private void button3_Click(object sender, EventArgs e)

{

if (MessageBox.Show("你是否要更新数据?", "x", MessageBoxButtons.OKCancel) == DialogResult.OK)

{

SqlConnection con = new SqlConnection(this.textBox1.Text)

con.Open()

SqlCommand com = new SqlCommand("update test set name= '" + this.textBox8.Text + "' ,address= '" + this.textBox9.Text+"' where id="+this.textBox7.Text,con)

com.ExecuteNonQuery()

MessageBox.Show("更新成功!")

button5_Click(null, null)

}

}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

}

private void button2_Click(object sender, EventArgs e)

{

if (MessageBox.Show("你是否要删除数据?", "x", MessageBoxButtons.OKCancel) == DialogResult.OK)

{

SqlConnection con = new SqlConnection(this.textBox1.Text)

con.Open()

SqlCommand com = new SqlCommand("delete from test where id='"+textBox7.Text+"'",con)

com.ExecuteNonQuery()

MessageBox.Show("删除成功!")

button5_Click(null, null)

}

}

private void button1_Click(object sender, EventArgs e)

{

if (this.textBox3.Text != null &&this.textBox4.Text != null &&this.textBox5.Text != null)

{

SqlConnection con = new SqlConnection(this.textBox1.Text)

con.Open()

SqlCommand com = new SqlCommand("insert test (id,name,address) values('"+this.textBox3.Text+"','"+this.textBox4.Text+"','"+this.textBox5.Text+"')",con)

com.ExecuteNonQuery()

MessageBox.Show("添加成功!")

button5_Click(null, null)

}

else

{

MessageBox.Show("请完整填写信息!")

}

}

private void button4_Click(object sender, EventArgs e)

{

if (this.radioButton1.Checked || this.radioButton2.Checked)

{

if (this.textBox2.Text != null)

{

if (this.radioButton1.Checked)

{

SqlConnection con = new SqlConnection(this.textBox1.Text)

con.Open()

SqlDataAdapter sqlda = new SqlDataAdapter("select * from test where id='"+this.textBox2.Text+"'", con)

DataTable dt = new DataTable()

sqlda.Fill(dt)

this.dataGridView1.DataSource = dt

}

else

{

SqlConnection con = new SqlConnection(this.textBox1.Text)

con.Open()

SqlDataAdapter sqlda = new SqlDataAdapter("select * from test where name='" + this.textBox2.Text + "'", con)

DataTable dt = new DataTable()

sqlda.Fill(dt)

this.dataGridView1.DataSource = dt

}

}

else

{

MessageBox.Show("请输入查询内容!")

this.textBox2.Focus()

}

}

else

{

MessageBox.Show("请选择查询方式!")

this.radioButton1.Checked = true

}

}

}

}

Form1.Designer.cs的代码:

namespace WindowsApplication4

{

partial class Form1

{

/// <summary>

/// 必需的设计器变量。

/// </summary>

private System.ComponentModel.IContainer components = null

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

protected override void Dispose(bool disposing)

{

if (disposing &&(components != null))

{

components.Dispose()

}

base.Dispose(disposing)

}

#region Windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button()

this.button2 = new System.Windows.Forms.Button()

this.button4 = new System.Windows.Forms.Button()

this.textBox1 = new System.Windows.Forms.TextBox()

this.label1 = new System.Windows.Forms.Label()

this.button5 = new System.Windows.Forms.Button()

this.textBox2 = new System.Windows.Forms.TextBox()

this.radioButton1 = new System.Windows.Forms.RadioButton()

this.radioButton2 = new System.Windows.Forms.RadioButton()

this.textBox3 = new System.Windows.Forms.TextBox()

this.textBox4 = new System.Windows.Forms.TextBox()

this.textBox5 = new System.Windows.Forms.TextBox()

this.dataGridView1 = new System.Windows.Forms.DataGridView()

this.id = new System.Windows.Forms.DataGridViewTextBoxColumn()

this.name = new System.Windows.Forms.DataGridViewTextBoxColumn()

this.address = new System.Windows.Forms.DataGridViewTextBoxColumn()

this.textBox7 = new System.Windows.Forms.TextBox()

this.textBox8 = new System.Windows.Forms.TextBox()

this.textBox9 = new System.Windows.Forms.TextBox()

this.button3 = new System.Windows.Forms.Button()

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit()

this.SuspendLayout()

//

// button1

//

this.button1.Location = new System.Drawing.Point(460, 301)

this.button1.Name = "button1"

this.button1.Size = new System.Drawing.Size(75, 23)

this.button1.TabIndex = 0

this.button1.Text = "添加"

this.button1.UseVisualStyleBackColor = true

this.button1.Click += new System.EventHandler(this.button1_Click)

//

// button2

//

this.button2.Location = new System.Drawing.Point(460, 359)

this.button2.Name = "button2"

this.button2.Size = new System.Drawing.Size(75, 23)

this.button2.TabIndex = 1

this.button2.Text = "删除"

this.button2.UseVisualStyleBackColor = true

this.button2.Click += new System.EventHandler(this.button2_Click)

//

// button4

//

this.button4.Location = new System.Drawing.Point(460, 272)

this.button4.Name = "button4"

this.button4.Size = new System.Drawing.Size(75, 23)

this.button4.TabIndex = 3

this.button4.Text = "查找"

this.button4.UseVisualStyleBackColor = true

this.button4.Click += new System.EventHandler(this.button4_Click)

//

// textBox1

//

this.textBox1.Location = new System.Drawing.Point(83, 12)

this.textBox1.Name = "textBox1"

this.textBox1.Size = new System.Drawing.Size(371, 21)

this.textBox1.TabIndex = 4

//

// label1

//

this.label1.AutoSize = true

this.label1.Location = new System.Drawing.Point(12, 15)

this.label1.Name = "label1"

this.label1.Size = new System.Drawing.Size(65, 12)

this.label1.TabIndex = 5

this.label1.Text = "连接字符串"

//

// button5

//

this.button5.Location = new System.Drawing.Point(460, 12)

this.button5.Name = "button5"

this.button5.Size = new System.Drawing.Size(75, 23)

this.button5.TabIndex = 6

this.button5.Text = "连接数据库"

this.button5.UseVisualStyleBackColor = true

this.button5.Click += new System.EventHandler(this.button5_Click)

//

// textBox2

//

this.textBox2.Location = new System.Drawing.Point(14, 272)

this.textBox2.Name = "textBox2"

this.textBox2.Size = new System.Drawing.Size(184, 21)

this.textBox2.TabIndex = 8

//

// radioButton1

//

this.radioButton1.AutoSize = true

this.radioButton1.Location = new System.Drawing.Point(234, 276)

this.radioButton1.Name = "radioButton1"

this.radioButton1.Size = new System.Drawing.Size(71, 16)

this.radioButton1.TabIndex = 9

this.radioButton1.TabStop = true

this.radioButton1.Text = "按ID查找"

this.radioButton1.UseVisualStyleBackColor = true

//

// radioButton2

//

this.radioButton2.AutoSize = true

this.radioButton2.Location = new System.Drawing.Point(336, 278)

this.radioButton2.Name = "radioButton2"

this.radioButton2.Size = new System.Drawing.Size(83, 16)

this.radioButton2.TabIndex = 10

this.radioButton2.TabStop = true

this.radioButton2.Text = "按Name查找"

this.radioButton2.UseVisualStyleBackColor = true

//

// textBox3

//

this.textBox3.Location = new System.Drawing.Point(14, 302)

this.textBox3.Name = "textBox3"

this.textBox3.Size = new System.Drawing.Size(98, 21)

this.textBox3.TabIndex = 11

//

// textBox4

//

this.textBox4.Location = new System.Drawing.Point(166, 303)

this.textBox4.Name = "textBox4"

this.textBox4.Size = new System.Drawing.Size(100, 21)

this.textBox4.TabIndex = 12

//

// textBox5

//

this.textBox5.Location = new System.Drawing.Point(331, 303)

this.textBox5.Name = "textBox5"

this.textBox5.Size = new System.Drawing.Size(100, 21)

this.textBox5.TabIndex = 13

//

// dataGridView1

//

this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize

this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {

this.id,

this.name,

this.address})

this.dataGridView1.Location = new System.Drawing.Point(14, 39)

this.dataGridView1.Name = "dataGridView1"

this.dataGridView1.RowTemplate.Height = 23

this.dataGridView1.Size = new System.Drawing.Size(440, 227)

this.dataGridView1.TabIndex = 18

this.dataGridView1.UserAddedRow += new System.Windows.Forms.DataGridViewRowEventHandler(this.dataGridView1_UserAddedRow)

this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick)

this.dataGridView1.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellEndEdit)

this.dataGridView1.SelectionChanged += new System.EventHandler(this.dataGridView1_SelectionChanged)

this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick)

//

// id

//

this.id.DataPropertyName = "id"

this.id.HeaderText = "id"

this.id.Name = "id"

this.id.ReadOnly = true

//

// name

//

this.name.DataPropertyName = "name"

this.name.HeaderText = "name"

this.name.Name = "name"

this.name.ReadOnly = true

//

// address

//

this.address.DataPropertyName = "address"

this.address.HeaderText = "address"

this.address.Name = "address"

//

// textBox7

//

this.textBox7.Location = new System.Drawing.Point(12, 342)

this.textBox7.Name = "textBox7"

this.textBox7.Size = new System.Drawing.Size(100, 21)

this.textBox7.TabIndex = 15

//

// textBox8

//

this.textBox8.Location = new System.Drawing.Point(166, 342)

this.textBox8.Name = "textBox8"

this.textBox8.Size = new System.Drawing.Size(100, 21)

this.textBox8.TabIndex = 16

//

// textBox9

//

this.textBox9.Location = new System.Drawing.Point(331, 342)

this.textBox9.Name = "textBox9"

this.textBox9.Size = new System.Drawing.Size(100, 21)

this.textBox9.TabIndex = 17

//

// button3

//

this.button3.Location = new System.Drawing.Point(460, 330)

this.button3.Name = "button3"

this.button3.Size = new System.Drawing.Size(75, 23)

this.button3.TabIndex = 2

this.button3.Text = "修改"

this.button3.UseVisualStyleBackColor = true

this.button3.Click += new System.EventHandler(this.button3_Click)

//

// Form1

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F)

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

this.ClientSize = new System.Drawing.Size(553, 387)

this.Controls.Add(this.dataGridView1)

this.Controls.Add(this.textBox9)

this.Controls.Add(this.textBox8)

this.Controls.Add(this.textBox7)

this.Controls.Add(this.textBox5)

this.Controls.Add(this.textBox4)

this.Controls.Add(this.textBox3)

this.Controls.Add(this.radioButton2)

this.Controls.Add(this.radioButton1)

this.Controls.Add(this.textBox2)

this.Controls.Add(this.button5)

this.Controls.Add(this.label1)

this.Controls.Add(this.textBox1)

this.Controls.Add(this.button4)

this.Controls.Add(this.button3)

this.Controls.Add(this.button2)

this.Controls.Add(this.button1)

this.Name = "Form1"

this.Text = "数据增删改查"

this.Load += new System.EventHandler(this.数据增删改查_Load)

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit()

this.ResumeLayout(false)

this.PerformLayout()

}

#endregion

private System.Windows.Forms.Button button1

private System.Windows.Forms.Button button2

private System.Windows.Forms.Button button4

private System.Windows.Forms.TextBox textBox1

private System.Windows.Forms.Label label1

private System.Windows.Forms.Button button5

private System.Windows.Forms.TextBox textBox2

private System.Windows.Forms.RadioButton radioButton1

private System.Windows.Forms.RadioButton radioButton2

private System.Windows.Forms.TextBox textBox3

private System.Windows.Forms.TextBox textBox4

private System.Windows.Forms.TextBox textBox5

private System.Windows.Forms.DataGridView dataGridView1

private System.Windows.Forms.DataGridViewTextBoxColumn id

private System.Windows.Forms.DataGridViewTextBoxColumn name

private System.Windows.Forms.DataGridViewTextBoxColumn address

private System.Windows.Forms.TextBox textBox7

private System.Windows.Forms.TextBox textBox8

private System.Windows.Forms.TextBox textBox9

private System.Windows.Forms.Button button3

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11782058.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存