11 Replies - 3828 Views - Last Post: 11 June 2012 - 11:34 PM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

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

Showing Currently Selected Text In Label Via Multi Tab Is This Ok?

Posted 08 June 2012 - 04:49 PM

So I've been working on code which seems to work fine but somehow these events scare me. I have a global variable rtb for the current rtb a user is in which is done when a user creates a new tab is a tabcontrol which places inside a new RTB Code Below:

  //create new tab page and add out Rich Text Box To It
        public void NewTabObject()
        {
            TabPage t = new TabPage();
            t.Name = "Text_Page:" + tabs.ToString();
            t.Text = "Text_Page:" + tabs.ToString();
            tb_control_main.Controls.Add(t);
            

            RichTextBox r = new RichTextBox();
            r.Size = new Size(884,464);
            r.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
            r.Name = t.Name + "r";
            r.Font = new Font(FontFamily.GenericSansSerif,12,FontStyle.Regular);
            t.Controls.Add(r);
            tabs++;

        }

        public void CloseTabObject()
        {
            if (tb_control_main.TabCount > 1)
            {
                tb_control_main.SelectedTab.Dispose();
            }
            else { MessageBox.Show("You Have Only One Tab Open Currently.  You Must Have At Least Two Tabs Open Before Closing One. I.E. You Can't Have Zero Tabs", "You Can't Have Zero Tabs", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
        }
        



These are the methods for creating new tabs::it works fine then you have the global variable in the beginning.

public uint tabs = 2;
        public RichTextBox rtb;



The tabs int is just used for naming each new tab and incrementally it like new tab 2 3 etc.,

 private void frm_main_Load(object sender, EventArgs e)
        {
            this.rtb = this.rch_1;
            fntfuncsdll.FontFuncs.UpdateLbl(this.rtb, this.tlstrp_topmain_lbl_justification_font);
            //load settings
            LoadSettings();
            rtb.Selectionchanged += new EventHandler(rtb_Selectionchanged);
        }



This assigns the rtb to the main RTB before or if the user creates new tabs which obviously have a RTB that looks the same. The fntfuncsdll.FontFuncs.UpdateLbl(this.rtb, this.tlstrp_topmain_lbl_justification_font); is what updates the label to the currently used font etc. and this changes automatically with the event when the user moves around in the RTB. The actual function I'm using for this is
 public static void UpdateLbl(RichTextBox r, ToolStripLabel l)
        {
            if (r.SelectedText.Length < 2)
            {
                l.Text = r.SelectionFont.OriginalFontName.ToString() + "|" + r.SelectionFont.Size.ToString() + "pt |" + r.SelectionFont.Style.ToString();
            }
        }

    }


Selection length is needed since if more is highlighted in the RTB it will cause a object instance exception.

The way to re-assign this variable to a new textbox if done when the user actually clicks the new tab which they have to do then I use this

private void tb_control_main_Selecting(object sender, TabControlCancelEventArgs e)
        {
            //update variable
            foreach (RichTextBox r in e.TabPage.Controls)
            {
                this.rtb = r;
            }
            //update event
            rtb.Selectionchanged -= new EventHandler(rtb_Selectionchanged);
            rtb.Selectionchanged += new EventHandler(rtb_Selectionchanged);
        }


This is the most important code. Essentially since there is only 1 RTB in the new tab page when a user creates once I know that to assign to rtb but the the old event issued at the form load is now void so I figured you have to first -= that event and then += creating the event over again which now works with the newly selected tab and RTB. If the user changes tabs this automatically occurs again.

Is there anything bad about this method? Can it lead memory leaks etc.? Advice?

Is This A Good Question/Topic? 0
  • +

Replies To: Showing Currently Selected Text In Label Via Multi Tab Is This Ok?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5505
  • View blog
  • Posts: 11,808
  • Joined: 02-June 10

Re: Showing Currently Selected Text In Label Via Multi Tab Is This Ok?

Posted 08 June 2012 - 06:25 PM

Quote

so I figured you have to first -= that event and then +=

Yes. Always a good practice and one most people overlook. Good job.

Quote

creating the event over again

This does not create an event. The event was always there and always gets raised. This just subscribes a handler to the event, so you can do something in response to when it is next raised.

Quote

I have a global variable rtb for the current rtb a user is in which is done when

Any time I hear 'global' I have my doubts about it being needed.

Are you aware that the (object sender) part of those method handlers *IS* the actual control that sent it? Its not there for decoration. It has a big vital purpose. If you click a button and have this handler:

void someButton_Click(object sender, eventargs e)
{
   // 'sender' is the actual button that was clicked on
}


Or in your case it would be the actual RichTextBox that raised an event. You don't have to micromanage the tracking of it. The handler will be told which R.T.B. raised the event as the sender object.

Which then leads to the realization you don't have to micromanage tracking of which tabpage is selected.

I see this kind of micromanagement from people that come from assembly coding or other non-OOP languages. Do you have a background in something like that?
Was This Post Helpful? 1
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

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

Re: Showing Currently Selected Text In Label Via Multi Tab Is This Ok?

Posted 10 June 2012 - 01:37 PM

View PosttlhIn`toq, on 08 June 2012 - 06:25 PM, said:

Quote

so I figured you have to first -= that event and then +=

Yes. Always a good practice and one most people overlook. Good job.

Quote

creating the event over again

This does not create an event. The event was always there and always gets raised. This just subscribes a handler to the event, so you can do something in response to when it is next raised.

Quote

I have a global variable rtb for the current rtb a user is in which is done when

Any time I hear 'global' I have my doubts about it being needed.

Are you aware that the (object sender) part of those method handlers *IS* the actual control that sent it? Its not there for decoration. It has a big vital purpose. If you click a button and have this handler:

void someButton_Click(object sender, eventargs e)
{
   // 'sender' is the actual button that was clicked on
}


Or in your case it would be the actual RichTextBox that raised an event. You don't have to micromanage the tracking of it. The handler will be told which R.T.B. raised the event as the sender object.

Which then leads to the realization you don't have to micromanage tracking of which tabpage is selected.

I see this kind of micromanagement from people that come from assembly coding or other non-OOP languages. Do you have a background in something like that?


Thanks man I get everything that you are saying. I also know about the object sender but I still am not seeing how that would work for something like like changing the font with a font dialog when someone clicks that button?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3566
  • View blog
  • Posts: 11,084
  • Joined: 05-May 12

Re: Showing Currently Selected Text In Label Via Multi Tab Is This Ok?

Posted 10 June 2012 - 05:03 PM

I refer you again to my post when you previously asked about this topic: http://www.dreaminco...ost__p__1635124

Basically take advantage of the Tag property that is part of every WinForms control. In your NewTabObject() method, when you dynamically create a new tab with a new RichTextBox, stash a copy of the RichTextBox reference into the TabPage's Tag property.

Later, when you handle the Font button click, you go query the TabControl for the current TabPage, and from there you query the Tag property. Cast that Tag to a RichTextBox and you are ready to rock.

void fontButton_Click(object sender, EventArgs e)
{
    using (FontDialog f = new FontDialog())
    {
        if (f.ShowDialog() == DialogResult.OK)
        {
            var richTextBox = (RichTextBox) mainTabControl.SelectedTab.Tag;
            richTextBox.SelectionFont = f.Font;
        }
    }
}


This post has been edited by Skydiver: 10 June 2012 - 05:06 PM

Was This Post Helpful? 1
  • +
  • -

#5 adn258  Icon User is offline

  • D.I.C Addict

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

Re: Showing Currently Selected Text In Label Via Multi Tab Is This Ok?

Posted 10 June 2012 - 10:45 PM

View PostSkydiver, on 10 June 2012 - 05:03 PM, said:

I refer you again to my post when you previously asked about this topic: http://www.dreaminco...ost__p__1635124

Basically take advantage of the Tag property that is part of every WinForms control. In your NewTabObject() method, when you dynamically create a new tab with a new RichTextBox, stash a copy of the RichTextBox reference into the TabPage's Tag property.

Later, when you handle the Font button click, you go query the TabControl for the current TabPage, and from there you query the Tag property. Cast that Tag to a RichTextBox and you are ready to rock.

void fontButton_Click(object sender, EventArgs e)
{
    using (FontDialog f = new FontDialog())
    {
        if (f.ShowDialog() == DialogResult.OK)
        {
            var richTextBox = (RichTextBox) mainTabControl.SelectedTab.Tag;
            richTextBox.SelectionFont = f.Font;
        }
    }
}



I see. That makes sense to me now but what is wrong per say with using the method I'm using to do this?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3566
  • View blog
  • Posts: 11,084
  • Joined: 05-May 12

Re: Showing Currently Selected Text In Label Via Multi Tab Is This Ok?

Posted 10 June 2012 - 11:51 PM

Nothing wrong with that approach per se. And if you are a big believer in micromanagement, then this will be even more your alley.

The advantages gained though are pretty big if you enjoy the "the fire and forget" nature afforded by the .NET framework. At the time you create the RTB, hook up the selection change event just once instead of doing all the hooking and unhooking at tab change time. Additionally, you don't have to look for the RTB at tab change time.

And to me, there is also one more advantage. This approach of using the sender and the Tag encourages loose coupling. Right now you are dependent on the tab change event happening before you do anything else with your rtb variable. The tab control has intimate knowledge of the containing form, as well as the contents of it's child tabs. You now assume all tabs must have a RTB. If you wanted to have a summary tab, you would have to put in a hidden RTB to keep the rtb variable from being null.

Additionally, this tight coupling, has influenced your UI, where you now force the user to have at least one tab so that rtb variable will never be null. If you allowed for a null rtb, then you'll always be checking for a null rtb all over the place. (But it looks like you'd rather not micro manage down to that extent.)

Sent from my T-Mobile G2 using Tapatalk 2
Was This Post Helpful? 1
  • +
  • -

#7 adn258  Icon User is offline

  • D.I.C Addict

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

Re: Showing Currently Selected Text In Label Via Multi Tab Is This Ok?

Posted 11 June 2012 - 02:35 AM

Good points skydiver...I will sleep on this. I appreciate your input. I tried your method by the way via casting and just naming new tabs Text_Page+ the current amount of tabs +1. I give this same string to the rtb tag. Later I get an error that I can't cast tag string to RTB. In any case I'll mess with your method more later for learning but I will probably stick with my method in the actual project.

This post has been edited by adn258: 11 June 2012 - 02:58 AM

Was This Post Helpful? 0
  • +
  • -

#8 adn258  Icon User is offline

  • D.I.C Addict

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

Re: Showing Currently Selected Text In Label Via Multi Tab Is This Ok?

Posted 11 June 2012 - 01:04 PM

View PostSkydiver, on 10 June 2012 - 05:03 PM, said:

I refer you again to my post when you previously asked about this topic: http://www.dreaminco...ost__p__1635124

Basically take advantage of the Tag property that is part of every WinForms control. In your NewTabObject() method, when you dynamically create a new tab with a new RichTextBox, stash a copy of the RichTextBox reference into the TabPage's Tag property.

Later, when you handle the Font button click, you go query the TabControl for the current TabPage, and from there you query the Tag property. Cast that Tag to a RichTextBox and you are ready to rock.

void fontButton_Click(object sender, EventArgs e)
{
    using (FontDialog f = new FontDialog())
    {
        if (f.ShowDialog() == DialogResult.OK)
        {
            var richTextBox = (RichTextBox) mainTabControl.SelectedTab.Tag;
            richTextBox.SelectionFont = f.Font;
        }
    }
}



I keep getting an unable to cast type when using this method. I also set the richtextbox name to the same name as the tag for the new tab page so this will always be the same string. (NOTE:) I'm not actually using this in my project I'm using the global variable idea, but because I love to learn new things and I want to know how to do this thus far this hasn't been working well. Any ideas? You can't seem to cast that down to RTB actually?
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3566
  • View blog
  • Posts: 11,084
  • Joined: 05-May 12

Re: Showing Currently Selected Text In Label Via Multi Tab Is This Ok?

Posted 11 June 2012 - 01:14 PM

What does your NewTabObject() code look like now?
Was This Post Helpful? 1
  • +
  • -

#10 adn258  Icon User is offline

  • D.I.C Addict

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

Re: Showing Currently Selected Text In Label Via Multi Tab Is This Ok?

Posted 11 June 2012 - 01:31 PM

I'm not using that method anymore friend. I just created a new program entirely just so I can learn this (no joke). I'm serious about learning everything I can so I created a new tab control and a method that's similar. The method works fine looks like so

 public void NewTabRTB()
        {
            TabPage t = new TabPage();
            t.Name = "tp" + tabControl1.TabCount.ToString();
            t.Size = new Size(611, 387);
            t.Tag = t.Name;

            RichTextBox r = new RichTextBox();
            r.Name = "tp" + tabControl1.TabCount.ToString();
            r.Size = new Size(611, 387);
            r.Tag = t.Name.ToString();
            t.Controls.Add(r);

            tabControl1.Controls.Add(t);
        }



Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3566
  • View blog
  • Posts: 11,084
  • Joined: 05-May 12

Re: Showing Currently Selected Text In Label Via Multi Tab Is This Ok?

Posted 11 June 2012 - 02:05 PM

Your line 6 is setting Tag to a string. This string cannot be casted to a RichTextBox by var richTextBox = (RichTextBox) tabControl1.SelectedTab.Tag;


If you change the code to below you should be a little more successful:
       public void NewTabRTB()
       {
           TabPage t = new TabPage();
           t.Name = "tp" + tabControl1.TabCount.ToString();
           t.Size = new Size(611, 387);

           RichTextBox r = new RichTextBox();
           r.Name = "tp" + tabControl1.TabCount.ToString();
           r.Size = new Size(611, 387);
           r.Tag = t.Name.ToString();
           t.Controls.Add(r);

           t.Tag = r;
           tabControl1.Controls.Add(t);
       }



I felt safe doing this change since you were originally just copying the Name over. If you needed to store other stuff in the tag, the technique is usually to create a class to hold the things and assign that class to the Tag.
Was This Post Helpful? 0
  • +
  • -

#12 adn258  Icon User is offline

  • D.I.C Addict

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

Re: Showing Currently Selected Text In Label Via Multi Tab Is This Ok?

Posted 11 June 2012 - 11:34 PM

Works thanks man. I see where my confusion was now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1