从组合框到文本框C#删除id为100的数字 [英] Delete numbers where id is 100 from combobox to textbox C#
问题描述
有一种情况我从项目表下载到组合框:
private void InsertStatus_Load(object sender,EventArgs e)
{
connection = new MySqlConnection();
connection.ConnectionString =datasource = localhost; port = 3306; username = root; password =;
connection.Open();
string selectQuery5 =SELECT ID,PROJECT_NAME FROM projekt1.projects;
MySqlCommand command5 = new MySqlCommand(selectQuery5,connection);
MySqlDataReader reader5 = command5.ExecuteReader();
while(reader5.Read())
{
comboBox4.Items.Add(reader5.GetString(ID)++ reader5.GetString(PROJECT_NAME));
}
connection.Close();
}
将数据下载到组合框后,它会显示所有ID和PROJECT_NAME的
ID PROJECT_NAME
---- ------------
1 dasa
2 dsag3
3 S-BOX
。 。
。 。
。 。
97 s-BOX12
98 S-DIX
99 S-DIX
100 DAFDS
101 DAF
102 DSA
在ID为1到99的情况下,它可以解决显示全名的问题(在PROJECT_NAME专栏中):
private void comboBox4_SelectedIndexChanged(object sender,EventArgs e)
{
if(comboBox4.SelectedIndex!= -1)
{
string [] parts = comboBox4.Items [comboBox4.SelectedIndex] .ToString()。Split('');
textBox10.Text = parts [0];
textBox4.Text = comboBox4.Text.Remove(0,2).Trim();
}
}
到文本框将显示没有ID的全名,例如:
SBOX12,DSA,S-BOX,SBO $等
但是,如果ID至少为100,我担心它不会显示全名:
0 sbox,1 das ,2个S-BOX,
我不知道该怎么做。
我尝试过:
使用System;
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用MySql.Data.MySqlClient;
使用System.Text.RegularExpressions;
名称空间ControlDatabase
{
public partial class InsertStatus:Form
{
MySqlConnection connection;
MySqlCommand命令;
MySqlDataReader dr;
Form1 frm1 =(Form1)Application.OpenForms [Form1];
public InsertStatus()
{
InitializeComponent();
}
private void InsertStatus_Load(object sender,EventArgs e)
{
connection = new MySqlConnection();
connection.ConnectionString =datasource = localhost; port = 3306; username = root; password =;
connection.Open();
string selectQuery5 =SELECT ID,PROJECT_NAME FROM projekt1.projects;
MySqlCommand command5 = new MySqlCommand(selectQuery5,connection);
MySqlDataReader reader5 = command5.ExecuteReader();
while(reader5.Read())
{
comboBox4.Items.Add(reader5.GetString(ID)++ reader5.GetString(PROJECT_NAME));
}
connection.Close();
}
private void comboBox4_SelectedIndexChanged(object sender,EventArgs e)
{
if(comboBox4.SelectedIndex!= -1)
{
string [] parts = comboBox4.Items [comboBox4.SelectedIndex] .ToString()。Split('');
textBox10.Text = parts [0];
textBox4.Text = comboBox4.Text.Remove(0,2).Trim();
}
}
等
但是如果ID至少为100,我担心它不会显示全名:
0 sbox,1 das,2 S-BOX,
我不知道该怎么做。
我尝试过:
使用系统;
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用MySql.Data.MySqlClient;
使用System.Text.RegularExpressions;
名称空间ControlDatabase
{
public partial class InsertStatus:Form
{
MySqlConnection connection;
MySqlCommand命令;
MySqlDataReader dr;
Form1 frm1 =(Form1)Application.OpenForms [Form1];
public InsertStatus()
{
InitializeComponent();
}
private void InsertStatus_Load(object sender,EventArgs e)
{
connection = new MySqlConnection();
connection.ConnectionString =datasource = localhost; port = 3306; username = root; password =;
connection.Open();
string selectQuery5 =SELECT ID,PROJECT_NAME FROM projekt1.projects;
MySqlCommand command5 = new MySqlCommand(selectQuery5,connection);
MySqlDataReader reader5 = command5.ExecuteReader();
while(reader5.Read())
{
comboBox4.Items.Add(reader5.GetString(ID)++ reader5.GetString(PROJECT_NAME));
}
connection.Close();
}
private void comboBox4_SelectedIndexChanged(object sender,EventArgs e)
{
if(comboBox4.SelectedIndex!= -1)
{
string [] parts = comboBox4.Items [comboBox4.SelectedIndex] .ToString()。Split('');
textBox10.Text = parts [0];
textBox4.Text = comboBox4.Text.Remove(0,2).Trim();
}
}
确定问题是comboBox4.Text.Remove(0,2)。修剪();
如果你有字符串100字
并从第0位开始删除2个字符(删除10 然后你会得到0字
你已经将字符串分成2个部分,具体取决于文本中有多少个空格所以使用
there is a case which i download from projects table to combobox:
private void InsertStatus_Load(object sender, EventArgs e) { connection = new MySqlConnection(); connection.ConnectionString = "datasource=localhost;port=3306;username=root;password="; connection.Open(); string selectQuery5 = "SELECT ID, PROJECT_NAME FROM projekt1.projects"; MySqlCommand command5 = new MySqlCommand(selectQuery5, connection); MySqlDataReader reader5 = command5.ExecuteReader(); while (reader5.Read()) { comboBox4.Items.Add(reader5.GetString("ID") + " " + reader5.GetString("PROJECT_NAME")); } connection.Close(); }
After downloading data to combobox it displays all ID's and PROJECT_NAME's
ID PROJECT_NAME ---- ------------ 1 dasa 2 dsag3 3 S-BOX . . . . . . 97 s-BOX12 98 S-DIX 99 S-DIX 100 DAFDS 101 DAF 102 DSA
In the case which ID's were from 1 to 99 it would solve the problem with display full names (in PROJECT_NAME column):
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox4.SelectedIndex != -1) { string[] parts = comboBox4.Items[comboBox4.SelectedIndex].ToString().Split(' '); textBox10.Text = parts[0]; textBox4.Text = comboBox4.Text.Remove(0, 2).Trim(); } }
to text box will display full name without ID's for example:
SBOX12, DSA, S-BOX, SBO$, etc
But where the ID's are at least 100 I'm afraid that it won't display full names:
0 sbox, 1 das, 2 S-BOX,
I don't have an idea what i should do.
What I have tried:
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; using MySql.Data.MySqlClient; using System.Text.RegularExpressions; namespace ControlDatabase { public partial class InsertStatus : Form { MySqlConnection connection; MySqlCommand command; MySqlDataReader dr; Form1 frm1 = (Form1)Application.OpenForms["Form1"]; public InsertStatus() { InitializeComponent(); } private void InsertStatus_Load(object sender, EventArgs e) { connection = new MySqlConnection(); connection.ConnectionString = "datasource=localhost;port=3306;username=root;password="; connection.Open(); string selectQuery5 = "SELECT ID, PROJECT_NAME FROM projekt1.projects"; MySqlCommand command5 = new MySqlCommand(selectQuery5, connection); MySqlDataReader reader5 = command5.ExecuteReader(); while (reader5.Read()) { comboBox4.Items.Add(reader5.GetString("ID") + " " + reader5.GetString("PROJECT_NAME")); } connection.Close(); } private void comboBox4_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox4.SelectedIndex != -1) { string[] parts = comboBox4.Items[comboBox4.SelectedIndex].ToString().Split(' '); textBox10.Text = parts[0]; textBox4.Text = comboBox4.Text.Remove(0, 2).Trim(); } }
, etc
But where the ID's are at least 100 I'm afraid that it won't display full names:
0 sbox, 1 das, 2 S-BOX,
I don't have an idea what i should do.
What I have tried:
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; using MySql.Data.MySqlClient; using System.Text.RegularExpressions; namespace ControlDatabase { public partial class InsertStatus : Form { MySqlConnection connection; MySqlCommand command; MySqlDataReader dr; Form1 frm1 = (Form1)Application.OpenForms["Form1"]; public InsertStatus() { InitializeComponent(); } private void InsertStatus_Load(object sender, EventArgs e) { connection = new MySqlConnection(); connection.ConnectionString = "datasource=localhost;port=3306;username=root;password="; connection.Open(); string selectQuery5 = "SELECT ID, PROJECT_NAME FROM projekt1.projects"; MySqlCommand command5 = new MySqlCommand(selectQuery5, connection); MySqlDataReader reader5 = command5.ExecuteReader(); while (reader5.Read()) { comboBox4.Items.Add(reader5.GetString("ID") + " " + reader5.GetString("PROJECT_NAME")); } connection.Close(); } private void comboBox4_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox4.SelectedIndex != -1) { string[] parts = comboBox4.Items[comboBox4.SelectedIndex].ToString().Split(' '); textBox10.Text = parts[0]; textBox4.Text = comboBox4.Text.Remove(0, 2).Trim(); } }
ok the problem is thecomboBox4.Text.Remove(0, 2).Trim();
if you have the string "100 word"
and you remove 2 characters starting in the 0th place(removing "10" then you will get "0 word"
you have already split the string into 2 more pieces depending on how many spaces are in the text so use that
这篇关于从组合框到文本框C#删除id为100的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!