4 Replies - 1423 Views - Last Post: 31 May 2012 - 01:42 PM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Access Selection Font For a Rich Text Box Created At Runtime

Posted 30 May 2012 - 10:54 PM

So I'm having an issue and I might have to think of another way to do this. Essentially in a program I'm writing I allow a user to create a new tabpage within a tab control and then a new rich textbox within that tab. All of this is done and added at runtime and it works fine. The only thing is I want to allow users to be able to change the selection font for the current richtext in the currently selected tabpage. Getting the current tabpage is easy but since I use a global variable for naming these new objects like the Rich Text Box

Since it's created dynamically you can't just type the object name like rchtxtbox_main.SelectionFont

I want the user to be able to do this whenever he or she wants.

How can you then access that objects selection font since it's being created dynamically at runtime for that Rich Text Box? Complicated. Stuff Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Access Selection Font For a Rich Text Box Created At Runtime

#2 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Re: Access Selection Font For a Rich Text Box Created At Runtime

Posted 31 May 2012 - 12:35 AM

Never mind I bypassed the problem considering::: Sorry. Here's what I did for this tab system multi tab so the user can open a new tab with my text edit problem simply set a global variable

public RichTextBox rtb;

Then whenever a user changed a tab this is set

private void tb_control_main_Selecting(object sender, TabControlCancelEventArgs e)
        {
            //update variable
            foreach (RichTextBox r in e.TabPage.Controls)
            {
                this.rtb = r;
            }
        }




Then when you want to change font on the fly

 using (FontDialog f = new FontDialog())
                    {
                        f.ShowDialog();
                        this.rtb.SelectionFont = f.Font;                      
                    }


You can do suck with the variable that's automatically updated when the user changes a tab :).

This also works with my algorithm for searching for text too.

The only thing that makes me a little bit angry is that foreach loop. Sure it works but is there any faster way or better way using linq or something?

This post has been edited by adn258: 31 May 2012 - 12:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Access Selection Font For a Rich Text Box Created At Runtime

Posted 31 May 2012 - 01:21 AM

I like your solution.

There's no need to replace a tiny Foreach loop like that with a LINQ query, it's so short it's not going to cut down lines much it'll just make the code harder to read.

I wasn't sure about performance, so I had a look. John Skeet suggests Foreach's are faster. (and the guy wrote one of my C# books)
Was This Post Helpful? 1
  • +
  • -

#4 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Re: Access Selection Font For a Rich Text Box Created At Runtime

Posted 31 May 2012 - 12:51 PM

Good point negligible. Thanks for your advice and input; I might just leave that the way it is then.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3449
  • View blog
  • Posts: 10,644
  • Joined: 05-May 12

Re: Access Selection Font For a Rich Text Box Created At Runtime

Posted 31 May 2012 - 01:42 PM

So that you don't have to keep searching, you can stash your reference to the RichEditBox in the Tag property of your TabPage. ( http://msdn.microsof...ontrol.tag.aspx )

// Code that dynamically creates the tab page and the rich text box
var tabPage = new TabPage()
var richTextBox = new RichTextBox()

:
tabPage.Controls.Add(richTextBox);
tabPage.Tag = richTextBox;
:



Then later when you need it:
using (FontDialog f = new FontDialog())
{
    if (f.ShowDialog() == DialogResult.OK)
    {
        var richTextBox = (RichTextBox) tb_control_main.SelectedTab.Tag;
        richTextBox.SelectionFont = f.Font;
    }
}



You won't have to worry about a global or class variable potentially getting into a race condition because the selection event hasn't completed yet.

Speaking of which, in the code above, it doesn't even have to be SelectedTab. As long as you can got a TabPage and get to it's Tag property, you can get your RichTextBox back easily.

This post has been edited by Skydiver: 31 May 2012 - 01:42 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1