此应用程序是留言板类型的应用程序。您发布一条消息,该消息存储在Oracle数据库中。用户的照片,姓名,日期和消息显示在列表视图中。一切正常但是有一个字体颜色变化的功能请求进来了。现在测试下面的东西,我知道我可以将所有消息显示为某种颜色。
我要做的是在表格中添加“颜色”列。然后,如果用户选择他们的消息文本为“红色”,我将在该帖子的表的颜色列中存储红色的十六进制颜色。
所以我想弄清楚的是,当检索到该消息时,如何将此代码设置为'如果颜色列为空,则将消息发布为黑色,否则如果每个消息都存在十六进制颜色,那么特定消息那种颜色'不会像下面的代码那样将每条信息的颜色改为红色。
List<MsgBoard> Messages = MsgBoard.find_ActiveByBoardName(Convert.ToString(cmbgroup.SelectedItem));
int i = 0;
imageList1.Images.Clear();
foreach (MsgBoard m in Messages)
{
AddImages(m.EmpPic);
ListViewItem Message = new ListViewItem("", i);
if (m.AlertNo == 0) //Default Message Post (non Alert)
{
Message.UseItemStyleForSubItems = false;
Message.SubItems.Add(m.EmpName.First);
Message.SubItems.Add(m.MessageText + Environment.NewLine).ForeColor = Color.Red;
Message.SubItems.Add(m.PostDate.ToString());
Message.SubItems.Add(m.EmpName.EmpNo.ToString());
Message.SubItems.Add(m.EmpName.Name);
listView1.Items.Add(Message);
}
i++;
}
}
只是澄清一下。如果20个人发布了一条消息,并且所有20个人都选择了不同的颜色来发布他们的消息。那么在数据库中,所有20行在数据库列中都有不同的颜色,我需要根据他们的颜色分别在列表视图中显示这些消息张贴为。我目前只知道将单一颜色应用于listview中的所有消息,如上例所示。
感谢Stephan,一旦我添加了
Message.SubItems.Add(m.MessageText + Environment.NewLine).ForeColor = System.Drawing.ColorTranslator.FromHtml(m.Color);
然后我需要帮助调整我的查询,以便在颜色列添加颜色后添加颜色。这是代码。
如果DB管理员添加了MESSAGE_COLOR varchar列。
我将以下内容编辑为现有代码,用于发布时新创建的列MESSAGE_COLOR ..
public string Create()
{
try
{
OleDbDataReader result = Database.Conn.Execute(
"INSERT INTO MPCS.MEYER_BOARD (" +
"EMPLOYEE_ID, " +
"POST_DATE, " +
"BOARD_NAME," +
"ALERT_NO," +
"MESSAGE_TEXT," +
"MESSAGE_COLOR," +
"ACTIVE_FLAG" +
") VALUES (?,SYSDATE,?,?,?,?)",
new List<OleDbParameter> {
new OleDbParameter("EMPLOYEE_ID",EmpName.EmpNo),
new OleDbParameter("BOARD_NAME",BoardName),
new OleDbParameter("ALERT_NO",AlertNo),
new OleDbParameter("MESSAGE_TEXT",MessageText),
new OleDbParameter("MESSAGE_COLOR",MessageColor),
new OleDbParameter("ACTIVE_FLAG",ActiveFlag ? "Y" : "N")
}, Query.ReaderType.Reader);
result.Read();
result.Close();
return null;
}
catch (Exception ex)
{
return ex.Message;
}
}
然后我也用新列编辑DB读取。
public static MsgBoard DBRead(OleDbDataReader result,string alias=null)
{
return new MsgBoard
{
EmpName = Employee.DBRead(result, "EMPLOYEE"),
MessageText = result[alias + "MESSAGE_TEXT"].ToString(),
MessageColor = result[alias + "MESSAGE_COLOR"].ToString(),
BoardName = result[alias +"BOARD_NAME"].ToString(),
AlertNo = (int)(decimal)result[alias +"ALERT_NO"],
PostDate = (DateTime)result[alias +"POST_DATE"],
ActiveFlag = result[alias +"ACTIVE_FLAG"].ToString().ToString() == "Y",
EmpPic = ImageResource.DBRead(result, "IR")
};
}
但是我需要编辑我的build_query吗?
public static String build_query(String where, string OrderBy = null)
{
List<String> cols = new List<String>();
cols.AddRange(db_columns.ConvertAll(c => "MPCS.MEYER_BOARD." + c + " AS MBOARD_" + c));
cols.AddRange(Employee.db_columns.ConvertAll(c => "MPCS.EMPLOYEE." + c + " AS EMPLOYEE_" + c));
cols.AddRange(ImageResource.db_columns.ConvertAll(c => "MPCS.IMAGE_RESOURCE." + c + " AS IR_" + c));
String sql =
"SELECT " + String.Join(", ", cols) + " " +
"FROM MPCS.MEYER_BOARD " +
"LEFT OUTER JOIN MPCS.EMPLOYEE " +
"ON MPCS.MEYER_BOARD.EMPLOYEE_ID=MPCS.EMPLOYEE.EMPLOYEE_ID " +
"LEFT OUTER JOIN MPCS.IMAGE_RESOURCE " +
"ON MPCS.IMAGE_RESOURCE.IR_ID=MPCS.EMPLOYEE.IMAGE_RESOURCE_ID " +
"WHERE ";
sql += where;
sql += OrderBy;
return sql;
}
更新
我已经完成了所有编码,现在我正在向DB提交颜色。但是,当在每个帖子下方显示列表视图时,仍然只有默认的黑色文本颜色。而不是保存的颜色,例如“#FFF000”被转换和显示。
foreach (MsgBoard m in Messages)
{
AddImages(m.EmpPic);
ListViewItem Message = new ListViewItem("", i);
if (m.AlertNo == 0) //Default Message Post (non Alert)
{
Message.SubItems.Add(m.EmpName.First);
Message.SubItems.Add(m.MessageText + Environment.NewLine).ForeColor = System.Drawing.ColorTranslator.FromHtml(m.MessageColor);
Message.SubItems.Add(m.PostDate.ToString());
Message.SubItems.Add(m.EmpName.EmpNo.ToString());
Message.SubItems.Add(m.EmpName.Name); //Displayed only on 'Show Details'
listView1.Items.Add(Message);
}
最终更新
原来我遗失的最后一块是改变listview属性UseItemStyleForSubItems = false;
现在它的工作。
答案 0 :(得分:1)
You can convert your hex value (assuming that it is stored as a string in the format "#RRGGBB"
) into a color using ColorTranslator
Using this approach, you can also use named colors (like "Violet"
)
Message.SubItems.Add(m.MessageText + Environment.NewLine).ForeColor =
System.Drawing.ColorTranslator.FromHtml(m.Color); //sth. like "#FFCC66"
Furthermore you have to retrieve the value of the Color
column. It seems that you have to add "MESSAGE_COLOR"
(or whatever your column is called) to your db_columns
. When you got the values from the DB, you have to map the column to the property of your Message
:
return new MsgBoard
{
// ...
Color = System.Drawing.ColorTranslator.FromHtml([alias+"MESSAGE_COLOR"]),
// ...
}
(In other words: You already save it to db, now you also have to read it from DB)