|
Setting BackColor for each item is another way (which you have used). I made a little test and forcing refresh didn't make the items to loose their color. In what situation you face the problem?
Test was (two buttons on a form and a listview):
private void button1_Click(object sender, EventArgs e) {
ListViewItem lvItem;
for (int i = 0; i < 10; i++) {
lvItem = new ListViewItem();
lvItem.Text = lvItem.GetHashCode().ToString();
lvItem.BackColor = Color.LightBlue;
listView1.Items.Add(lvItem);
}
lvItem = new ListViewItem();
lvItem.Text = lvItem.GetHashCode().ToString();
lvItem.BackColor = Color.Red;
listView1.Items.Add(lvItem);
}
private void button2_Click(object sender, EventArgs e) {
this.Refresh();
}
|
|
|
|
|
Hi,
I'll try to describe the situation.
The items in the listview control can have several states. The states are defined in the 3rd column.
Depending on that status text, i am giving the items a color. They do get the color but as soon as i select an item, the color is gone. And any other event on the listview control is making this happen too.
I am looping through a switch statement and assigning there the back color for the items. After that, i refresh the listview control.
|
|
|
|
|
I tested clicking and refreshing with the code I posted and no problems. Test case was:
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
foreach (ListViewItem lvItem in listView1.Items) {
lvItem.BackColor = Color.Purple;
}
listView1.Refresh();
}
If I understood correctly, this would be similar to your case
So I suspect that the problem is somehow in the loop where you define (and possibly reset) the colors for each item.
|
|
|
|
|
Hi,
I forgot to mention that i have to use delegates to access the listview control. I only assign the color at one place.
That shouldn't be the problem right?
|
|
|
|
|
Delegates themselves are not the problem. However if you receive events in a wrong order, that could cause the problem (event to set colors before event to reset colors etc).
Use debugger to confirm that your code is run in desired order. You could also use conditional breakpoints so that whenever a listviewitems background color changes, the debugger breaks. That could give you idea what's going wrong.
|
|
|
|
|
Hi,
The coloring of the items happens in another class. The listview control is passed by ref to that other class.
It shouldn't be a problem if i don't color the items in the Form class right?
The delegates run in the right order.
|
|
|
|
|
No, that isn't a problem as long as you work with the original ListViewItems (i.e. don't make copies).
Did you try to break on refresh or use conditional breakpoints. I believe they would clarify this greatly.
|
|
|
|
|
Hi,
I did both, the colors really get assigned. But they are just not visible. If i minimize the window and bringing it back to front, i can see the colors but disappear really fast again.
[EDIT 12:33h]
Hi,
It seems that i managed to fix this a bit. I created a paint event from the Form.
I do all the coloring stuff in there now. It seems that it keeps the colors, but if i select an item, it loses its color.
If i use the selectedIndexChanged event for the listview control, and i select an item, i lose all the colors on every item.
I need a way to call the Paint event inside the selectedIndexChanged event of the listview control. This might do the trick.
I tried this: this.frmMain_Paint(this.listviewcontrol, null);
But that didn't work...
[/Edit]
modified on Sunday, August 31, 2008 6:33 AM
|
|
|
|
|
For some reason the coloring is reset after you assign the color. Did the example I posted work corectly? If it did, I suggest that you comment code out so that you get painting correclty and then try to bring back code only in small pieces.
Also check the form's DoubleBuffered property. I believe it should be false.
|
|
|
|
|
i have this non rectangular window (spline-shaped) :
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace Guitar
{
class GuitarForm : Form
{
Bitmap bmp;
GuitarForm()
{
bmp = new Bitmap("C:\\ok.jpg");
GraphicsPath gp = new GraphicsPath();
Point[] curves = new Point[]{
new Point(93,99), new Point(200, 55),
new Point(328, 128),new Point(450,100),new Point(560,200),
};
gp.AddCurve(curves);
gp.AddLine(560,200, 560, 500);
gp.AddLine(560, 500, 93, 500);
gp.AddLine(93, 500, 93, 99);
gp.CloseFigure();
this.Region = new Region(gp);
}
protected override void OnPaint(PaintEventArgs ea)
{
base.OnPaint(ea);
Graphics g = ea.Graphics;
g.DrawImage(bmp, new Point(0, 0));
Rectangle scaled = new Rectangle(20, 20, 60, 60);
g.DrawImage(bmp,scaled);
g.DrawRectangle(Pens.Red, scaled);
Point[] pGram = new Point[]{
new Point(50, 10),
new Point(100, 30),
new Point(20, 100),
};
g.DrawImage(bmp, pGram);
}
public static void Main()
{
Application.Run(new GuitarForm());
}
private void InitializeComponent()
{
this.SuspendLayout();
this.ClientSize = new System.Drawing.Size(560, 560);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "GuitarForm";
this.ResumeLayout(false);
}
}}
the problem is that when i run it, only one part of the window appears and i have to drag to brders in order to see the whole shape . Is there any way it can run all-ready sized the way i want it ?
|
|
|
|
|
Could it be something as simple as this.Size = ...?
Mark Brock
"We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen
Click here to view my blog
|
|
|
|
|
Simple things always get me Thank you , Mark!
|
|
|
|
|
Hi ,
Is there any event for both the textchange and selectindexchange in combobox
{that event must fire when we made any change in the text as well as selectindexchange
|
|
|
|
|
There isn't, but just use both events and keep track of them yourself, like:
bool textChanged = false;
bool indexChanged = false;
void TextChanged()
{
if(!indexChanged)
textChanged = true;
else {
indexChanged = false
EventsFired();
}
}
void IndexChanged()
{
if(!textChanged)
indexChanged = true;
else {
textChanged = false
EventsFired();
}
}
void EventsFired()
{
}
My current favourite word is: I'm starting to run out of fav. words!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
there are no events for handle both events,
specify event handler like this:
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
this.comboBox1.TextChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
then you perfom actions in
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
this done because both events are apply from System.EventHandler
dhaim
programming is a hobby that make some money as side effect
|
|
|
|
|
how can i create new stencil bar in ms visio using c#.net and save, open, new file
tahir
|
|
|
|
|
|
I have a pre-printed invoices on dot matrix and i want to print the customers' invoices on it...
which method is best? printing to specefic point or use RDLC?
|
|
|
|
|
Hi,
You don't need a complicated algo. here, RDLC is more than enough.
good luck,
|
|
|
|
|
I binding the following to a datagrid:
private void get_all_vehicles()
{
dataVehicles.DataBindings.Clear();
data_table = new DataTable();
sql_connection = new SqlConnection("Data Source=.\\SQLEXPRESS;initial catalog=rent_a_car_company;integrated security=true");
sql_command = new SqlCommand("sp_populate_all_vehicles", sql_connection);
sql_command.CommandType = CommandType.StoredProcedure;
sql_adapter = new SqlDataAdapter(sql_command);
sql_adapter.Fill(data_table);
dataVehicles.DataSource = data_table;
dataVehicles.Columns["vehicle_id"].HeaderText = "ID";
dataVehicles.Columns["vehicle_id"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
dataVehicles.Columns["vehicle_id"].Width = 160;
dataVehicles.Columns["vehicle_id"].Visible = false;
dataVehicles.Columns["vehicle_photo"].HeaderText = "Photo";
dataVehicles.Columns["vehicle_photo"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
dataVehicles.Columns["vehicle_photo"].Width = 160;
dataVehicles.Columns["vehicle_make"].HeaderText = "Vehicle";
dataVehicles.Columns["vehicle_make"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
dataVehicles.Columns["vehicle_make"].Width = 160;
dataVehicles.Columns["vehicle_number"].HeaderText = "Number#";
dataVehicles.Columns["vehicle_number"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
dataVehicles.Columns["vehicle_number"].Width = 200;
dataVehicles.Columns["category_name"].HeaderText = "Category";
dataVehicles.Columns["category_name"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
dataVehicles.Columns["category_name"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
dataVehicles.Columns["category_name"].Width = 120;
dataVehicles.Columns["vehicle_shift"].HeaderText = "Shift";
dataVehicles.Columns["vehicle_shift"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
dataVehicles.Columns["vehicle_shift"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
dataVehicles.Columns["vehicle_shift"].Width = 120;
dataVehicles.Columns["vehicle_rent_daily"].HeaderText = "Daily Rent";
dataVehicles.Columns["vehicle_rent_daily"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
dataVehicles.Columns["vehicle_rent_daily"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
dataVehicles.Columns["vehicle_rent_daily"].DefaultCellStyle.Format = "#,###,###,##0.000";
dataVehicles.Columns["vehicle_rent_daily"].Width = 120;
sql_command.Dispose();
sql_adapter.Dispose();
sql_connection.Close();
}
also I am binding the following if the user clicked the vehicles out button:
private void get_vehicles_out()
{
dataVehicles.DataBindings.Clear();
data_table = new DataTable();
sql_connection = new SqlConnection("Data Source=.\\SQLEXPRESS;initial catalog=rent_a_car_company;integrated security=true");
sql_command = new SqlCommand("sp_populate_vehicles_out", sql_connection);
sql_command.CommandType = CommandType.StoredProcedure;
sql_adapter = new SqlDataAdapter(sql_command);
sql_adapter.Fill(data_table);
dataVehicles.DataSource = data_table;
dataVehicles.Columns["vehicle_id"].HeaderText = "ID";
dataVehicles.Columns["vehicle_id"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
dataVehicles.Columns["vehicle_id"].Width = 160;
dataVehicles.Columns["vehicle_id"].Visible = false;
dataVehicles.Columns["vehicle_photo"].HeaderText = "Photo";
dataVehicles.Columns["vehicle_photo"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
dataVehicles.Columns["vehicle_photo"].Width = 160;
dataVehicles.Columns["vehicle_photo"].Visible = false;
dataVehicles.Columns["out_category"].HeaderText = "Status";
dataVehicles.Columns["out_category"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
dataVehicles.Columns["out_category"].Width = 160;
dataVehicles.Columns["vehicle_make"].HeaderText = "Vehicle";
dataVehicles.Columns["vehicle_make"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
dataVehicles.Columns["vehicle_make"].Width = 160;
dataVehicles.Columns["vehicle_make"].HeaderText = "Vehicle";
dataVehicles.Columns["vehicle_make"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
dataVehicles.Columns["vehicle_make"].Width = 160;
dataVehicles.Columns["vehicle_number"].HeaderText = "Number#";
dataVehicles.Columns["vehicle_number"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
dataVehicles.Columns["vehicle_number"].Width = 200;
dataVehicles.Columns["category_name"].HeaderText = "Category";
dataVehicles.Columns["category_name"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
dataVehicles.Columns["category_name"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
dataVehicles.Columns["category_name"].Width = 120;
dataVehicles.Columns["category_name"].Visible = false;
dataVehicles.Columns["vehicle_shift"].HeaderText = "Shift";
dataVehicles.Columns["vehicle_shift"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
dataVehicles.Columns["vehicle_shift"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
dataVehicles.Columns["vehicle_shift"].Width = 120;
dataVehicles.Columns["vehicle_rent_daily"].HeaderText = "Daily Rent";
dataVehicles.Columns["vehicle_rent_daily"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
dataVehicles.Columns["vehicle_rent_daily"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
dataVehicles.Columns["vehicle_rent_daily"].DefaultCellStyle.Format = "#,###,###,##0.000";
dataVehicles.Columns["vehicle_rent_daily"].Width = 120;
dataVehicles.Columns["vehicle_rent_daily"].Visible = false;
sql_command.Dispose();
sql_adapter.Dispose();
sql_connection.Close();
}
and if you noticed, the only differnce is the column out_category which i am adding in the second code.
Now, the problem is: when the user clicks on the button displaying the first list then everything is fine but when the user clicks on the button invoking the second code it will just add the out_category column to the end which means it using the first codeand adding the out_category column when it should place the out_category column before the vehicle_make column.
so where is the problem?
|
|
|
|
|
You need to clear the columns too I think.
|
|
|
|
|
well, i only tried clearing the columns after facing this problem but it didn't solve it.
|
|
|
|
|
If I'm reading this correctly, you don't add columns on-the-fly, but use predefined columns and modify their properties (unless binding creates them). If that's the case, change the column order when adding columns to DataGridViewColumnCollection.
If the columns are added automatically by binding, change the order of columns in sp returning the result set or modify the order after population.
Hope this helps,
Mika
|
|
|
|
|
i hav already did that before posting this question.. here is my stored proceedure:
SELECT vehicles.vehicle_id, vehicles.vehicle_photo,
out_category = CASE vehicles.out_category
WHEN 'S' THEN 'STAFF'
WHEN 'C' THEN 'CUSTOMER'
WHEN 'R' THEN 'SERVICE'
WHEN 'W' THEN 'WORKSHOP'
END
, vehicle_makes.vehicle_make + ' ' + vehicle_brands.vehicle_brand + ' ' + LTRIM(STR(vehicles.vehicle_year)) AS vehicle_make, registration_types.registration_type + ' ' + vehicles.vehicle_registration_number AS vehicle_number, vehicle_category.category_name,
vehicle_shifts.vehicle_shift, vehicles.vehicle_rent_daily
FROM vehicles INNER JOIN
branches ON vehicles.branch_id = branches.branch_id INNER JOIN
vehicle_category ON vehicles.vehicle_category = vehicle_category.category_id INNER JOIN
vehicle_makes ON vehicles.vehicle_make = vehicle_makes.vehicle_make_id INNER JOIN
vehicle_brands ON vehicles.vehicle_brand = vehicle_brands.vehicle_brand_id INNER JOIN
registration_types ON vehicles.vehicle_registration_type = registration_types.registration_type_id INNER JOIN
vehicle_shifts ON vehicles.vehicle_shift = vehicle_shifts.vehicle_shift_id
WHERE vehicles.is_vehicle_out = 'True'
|
|
|
|
|
Ok, but most important: Are the columns already defined for dataVehicles before setting the DataSource?
|
|
|
|
|