|
Hi,
I haven't done any ComboBox.DrawItem myself, however I use ListBopx.DrawItem a lot, and they are documented very similarly. Never had any problems. Is your CB double-buffered? is your Form? Mine normally are.
You haven't told in any detail what is wrong with the result.
here are some suggestions:
- if the comboBox.Items are strings, no need to call ToString()
- use the entire e.Bounds in DrawString, not just X and Y
- if the visual result does not provide a clue, I would add logging code to figure for which items DrawItem gets called/doesn't get called assuming some of the items are not repainted, hence show previous content.
BTW: the MSDN example on CB.DrawItem creates and doesn't dispose of its SolidBrush and Font!
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Ok. Well here is the info I have.
I changed the code quite a bit. Created a custom combobox at runtime. Form and combobox are doublebuffered.
It also creates a log file showing index, bounds, and time of DrawItem method being executed.
To describe the problem. It would appear that the Background is not being drawn correctly (but this only describes the appearance-I don't think this is the cause). When I quickly scroll the entire list, to the end, the majority of the items appear as a "blob" of black as if all of the items were drawn over top of eachother. The odd thing is this is intermittent. Out of the 20 items shown in the drop down maybe 3 or 4 will be displayed correctly.
The log file shows that when quickly dragging through a list, the items are only being drawn on either the last or second to the last visible item bounds. When dragging up the items are only drawn on either the top or second visible item bounds. Since this is the case, internally, either Windows or the .NET framework, would have to be storing the item graphics and then be moving the previous item graphics up or down.
But if you click on the scroll bar to get the next or previous "page" all the items will be redrawn from top to bottom...
I uploaded the log file to my website http://www.hoveytech.com/drawitemlog.txt
Not sure where to go from here.
Thanks for your quick answers and continued help!
Public Class Form1
Private m_fBold As Font
Private WithEvents m_cboCustom1 As CustomCombo
Private Sub m_cboCustom1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles m_cboCustom1.DrawItem
If e.Index = -1 Then
Exit Sub
End If
e.DrawBackground()
If e.Index Mod 4 = 0 Then
e.Graphics.DrawString(m_cboCustom1.Items(e.Index).ToString(), m_fBold, Brushes.Black, e.Bounds)
Else
e.Graphics.DrawString(m_cboCustom1.Items(e.Index).ToString(), e.Font, Brushes.Black, e.Bounds)
End If
LogDrawItem(e.Index, e.State, e.Bounds)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.DoubleBuffered = True
m_cboCustom1 = New CustomCombo
m_cboCustom1.Location = New Point(30, 30)
m_cboCustom1.DropDownStyle = ComboBoxStyle.DropDownList
m_cboCustom1.DrawMode = DrawMode.OwnerDrawVariable
m_cboCustom1.MaxDropDownItems = 20
Me.Controls.Add(m_cboCustom1)
m_fBold = New Font(m_cboCustom1.Font, FontStyle.Bold)
Dim i As Integer
For i = 1 To 200
m_cboCustom1.Items.Add(i)
Next
End Sub
Private Sub LogDrawItem(ByVal iIndexDrawn As Integer, ByVal iState As System.Windows.Forms.DrawItemState, ByVal rect1 As Rectangle)
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(Application.StartupPath & "\drawitemlog.txt", True)
sw.WriteLine("DrawItem method ran for index: " & iIndexDrawn.ToString() & vbTab & "bounds: " & rect1.ToString() & vbTab & "at " & Now().ToLongDateString & " " & Now().ToLongTimeString())
sw.Close()
End Sub
End Class
Public Class CustomCombo
Inherits System.Windows.Forms.ComboBox
Public Sub New()
Me.DoubleBuffered = True
End Sub
End Class
modified on Sunday, July 19, 2009 5:58 PM
|
|
|
|
|
Hi David,
I noticed you have chosen OwnerDrawVariable, which means each item has its own height, and before DrawItem fires a MeasureItem event gets fired, asking you for the height of the item.
Suggestion:
- try OwnerDrawFixed;
- or provide a MeasureItem handler.
With this fixed it works fine for me (also remove the logging in DrawItem for good responsiveness).
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Thanks for your help!
It works now!!
|
|
|
|
|
im wanting to click on a item in a listbox and load the txt file into a richtextbox from the folder my main program resides in
can anyone show me how to do this in c#?
modified on Saturday, July 18, 2009 3:01 PM
|
|
|
|
|
What hzve you done so far? Which part of Pete's earlier and fine reply are you having trouble with?
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem == (object)"New DVD")
{
TextBox1. <---- i want to load a text file here from this programs Data folder
modified on Saturday, July 18, 2009 3:19 PM
|
|
|
|
|
deadcell_6 wrote: private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
that is OK
deadcell_6 wrote: if (listBox1.SelectedItem == (object)"New DVD")
that is terrible. Why would you cast to an object?
why would you include a literal string?
I guess your ListBox is showing filenames? if so, get the filename from the selected item.
Could be as easy as
if (listBox1.SelectedIndex>=0) {
string filename=listBox1.Items[listBox1.SelectedIndex];
string s=File.ReadAllText(filename);
myTextBox.Text=s;
}
It probably could use some error handling and some refinement, but that is how it goes.
If all of this is new to you, I strongly recommend you this advice[^]. And do consider reading some CodeProject articles as well!
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
thank you
now say i have 10 text files and my code is
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem == (object)"DVD 1")
{
TextBox1 <--- and i want to display dvd 1.txt
}
else if (listBox1.SelectedItem == (object)"DVD 2")
TextBox1 <---- and i want to display dvd 2.txt
else if (listBox1.SelectedItem == (object)"DVD 3")
TextBox1
else if (listBox1.SelectedItem == (object)"DVD 4")
TextBox1
......
sry im very new
this is what i have now and i think it would be easier to update the txt files as i get more dvds
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem == (object)"DVD 1")
{
TextBox1.Text = "horror" +Environment.NewLine + "actors:";
}
|
|
|
|
|
deadcell_6 wrote: private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem == (object)"DVD 1")
{
TextBox1 <--- and i want to display dvd 1.txt
}
else if (listBox1.SelectedItem == (object)"DVD 2")
TextBox1 <---- and i want to display dvd 2.txt
else if (listBox1.SelectedItem == (object)"DVD 3")
...
Eeek!!
Seriously, you need to learn some more of the basics of how to organise and manage data first.
How do you think an application that handles hundreds , or thousands, of pieces of information works? It certainly isn't by having a seaprate "if..." for each one.
Find out about structures, arrays, and file handling (then eventually maybe databases) and think about what information you have and how you want to organise it.
There are three kinds of people in the world - those who can count and those who can't...
|
|
|
|
|
I develop the application using C#.net in windows xp (english version)
when I run the application the display (image) still good
but when I deploy to another computer which using windows xp (chinese version)
the display (image) become not good
does anyone knows why it happen and how to solve it?
regards,
andy
|
|
|
|
|
eyeshield21 wrote: the display (image) become not good
sure, that must be because your code is not good.
read my sig!
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
|
can we make printable grid of simple dot net grid, and can we print our form objects with grid? I am trying to create my own report builder for C# forms application
|
|
|
|
|
Does this[^] article help?
/ravi
|
|
|
|
|
Hello friends,
I waana create a set for my window based apllication.I should include some com components like crystal report,ms word-2005.Can u explain me in detail.Thank u.
Cheers,
MAHESH
|
|
|
|
|
|
Thank u for your help.It's really Useful for me.
|
|
|
|
|
Nice to know its help you.
|
|
|
|
|
i want to have a listbox with a bunch of dvds i own, and when i click a dvd i want it to display info about the movie in a richtextbox in C#.net
|
|
|
|
|
deadcell_6 wrote: i want to have a listbox with a bunch of dvds i own, and when i click a dvd i want it to display info about the movie in a richtextbox in C#.net
So retrieve your data from a database, bind it to the listbox and react to the selection changed index.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I have an expandable property, TextLength, with six properties.
TextLength.All = 0
TextLength.ACCAComment = 60
TextLength.AxisDescription = 60
TextLength.Footer = 60
TextLength.GraphicsComment = 60
TextLength.Title = 60
VB.NET 2.0 displays the properties sorted as:
TextLength.ACCAComment = 60
TextLength.All = 0
TextLength.AxisDescription = 60
TextLength.Footer = 60
TextLength.GraphicsComment = 60
TextLength.Title = 60
How can I set the order as:
TextLength.All = 0
TextLength.ACCAComment = 60
TextLength.AxisDescription = 60
TextLength.Footer = 60
TextLength.GraphicsComment = 60
TextLength.Title = 60
In VB.Net 3.5, we can use PropertyOrder(number)
Thanks
|
|
|
|
|
Hi,
As the feature is new in 3.5 it is not available in 2.0
the only thing related is PropertySort, but that won't solve your specific wish.
Switch to 3.5 or get over it.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
I m working on Accounting Project and i m using DataGridview to display General and Ledger Data(Reports) but using DataGridview which is not displaying Data in Proper Layout and Format(Required by General) but show data in simple tabular form i want to show output with the exact formatting of General so how can i do If some example then plz share
|
|
|
|
|
No one Replied me yet
|
|
|
|