Click here to Skip to main content
15,896,526 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

i have the following code, and I want to make the selected item bold or checked. My compiler gives an error which tells me it is read-only. Is there some sort of workaround here?

Thanks!

C#
private void addprinter(string printername)
{

    ToolStripItem addItem = selectPrinterToolStripMenuItem1.DropDownItems.Add(printername);
    addItem.Click += new EventHandler(printerName_Click);


}
private void printerName_Click(object sender, EventArgs e)
{
    ToolStripItem item = (ToolStripItem)sender;
    SelectedPrinterName = item.Text;
    item.Font.Bold = true; // <-- error is here
    item.Checked = true; // <--and here
    MessageBox.Show(SelectedPrinterName, "SystemInformation #208", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Posted

1 solution

I'm not sure if this solution will work for you, but here goes:

The problem is that the properties inside the Font are read-only. The solution is to create a new font, like this:
C#
btn1.DropDown.Font = new Font(btn1.DropDown.Font, FontStyle.Bold);

Source[^]

Another potential way to do this is: This One[^]. You could perhaps use the concepts in this solution to apply against your selected item.
 
Share this answer
 
v2
Comments
ridoy 28-Sep-12 12:26pm    
+5 Marcus..great:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900