11 Replies - 13084 Views - Last Post: 12 March 2011 - 09:33 AM Rate Topic: -----

#1 staindromeo   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 155
  • Joined: 21-October 10

Make the text in a textbox bold

Posted 10 March 2011 - 09:26 AM

I have the code in place to generate table rows & table cells based on the data. I just wanted to add a functionality in the textbox portion of the table cells. Whenever the text within the textbox, is there a way to make the textbox bold. I am toying with 2 options, one is whenever the textbox is clicked on to automatically set the font format as bold, or the 2nd option is to set it on input as font format bold. I just don't have any clue on how to go about this in C#. Can someone please help me with this, or suggest any ideas it would really help me out.

Is This A Good Question/Topic? 0
  • +

Replies To: Make the text in a textbox bold

#2 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Make the text in a textbox bold

Posted 10 March 2011 - 09:31 AM

Quote

I have the code in place to generate table rows & table cells based on the data.


You haven't told us what control (if any) you are using. Are you using a DataGridView? Some third party control? Manually creating TextBoxes in a grid pattern?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6536
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Make the text in a textbox bold

Posted 10 March 2011 - 09:32 AM

You need to at least make a good faith effort to write the code before DIC members will help you. That is a requirement of the site.

You sound like you have an understanding of setting the TextBox.Font property.

Give it a shot, and if you have code that isn't quite doing it - then post that along with the errors.
Was This Post Helpful? 0
  • +
  • -

#4 ragingben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: Make the text in a textbox bold

Posted 10 March 2011 - 09:35 AM

You have to respecify the Font property of the TextBox, where you do this is up to you. You know the family, and the size already by interrogating the Font property, and the FontStyle enumeration includes a Bold member.

Agreed with tlhIn'toq in hindsight, code removed.

This post has been edited by ragingben: 10 March 2011 - 09:46 AM

Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6536
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Make the text in a textbox bold

Posted 10 March 2011 - 09:44 AM

Ben it was really nice of you to give him the code... but at least give someone a couple hours since they have been asked to provide a good faith effort to do it themselves, in accordance with the DIC guidelines.

The reason we do that is to keep every newbie, lazy student and low-budge outsourcer from coming up here and begging for code without even trying.

As it was this question was borderline "gimme teh codez". I nearly hit the [Report] button on it for that reason. But for some reason I thought it might be more beneficial to the OP if we asked him to give it a try, since he at least was using verbiage indicating he understood that textbox has a .Font property that needs to be changed, and that he understood where in his chain of events to make it happen.
Was This Post Helpful? 0
  • +
  • -

#6 ragingben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: Make the text in a textbox bold

Posted 10 March 2011 - 09:48 AM

Yeah agreed, it was borderline. Sometimes I just can't resist spewing code :)
Was This Post Helpful? 0
  • +
  • -

#7 staindromeo   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 155
  • Joined: 21-October 10

Re: Make the text in a textbox bold

Posted 10 March 2011 - 10:13 AM

Thanks for your quick response guys, I usually input my code in my posts but this time I didn't. I appreciate all the help, and like I said, I didn't have a clue on how to do this portion of it in C# hence I asked for help. The part that I do have completed has nothing to do with my question, but here is my code anyways.

private void SetDessertsTable(DataSet ds)
    {
        TableRow tr;
        TableCell tc;
        TextBox tb;
        RangeValidator rv;

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            tr = new TableRow();

            tc = new TableCell();
            tc.CssClass = "DName";
            tc.Text = (string)ds.Tables[0].Rows[i]["Description"];
            tr.Cells.Add(tc);


            tc = new TableCell();
            tc.Text = "";
            if (ds.Tables[0].Rows[i]["Price"] != DBNull.Value)
            {
                tc.CssClass = "Dcurrent";
                tc.Text = String.Format("{0:c2}", Convert.ToDouble(ds.Tables[0].Rows[i]["IndividualPrice"]));
            }
            else
            {
                tc.Text = "Store";
                tc.ColumnSpan = 2;
                tc.CssClass = "DNameNew";
                tc.Width = new Unit(100, UnitType.Pixel);
            }
            tr.Cells.Add(tc);

  if (ds.Tables[0].Rows[i]["IndividualPrice"] != DBNull.Value)
            {
                tc = new TableCell();
                tc.CssClass = "tdSPDessertsNew";
                if (ds.Tables[0].Rows[i]["IndividualPrice"] != DBNull.Value)
                {
                    tb = new TextBox();
                    tb.ID = "tb_Desserts_Individual_" + Convert.ToInt32(ds.Tables[0].Rows[i]["ident"]).ToString();
                    tb.Width = new Unit(50, UnitType.Pixel);
                    tb.Style.Add("text-align", "right");
                    if (ds.Tables[0].Rows[i]["IndividualPriceInput"] != DBNull.Value)
                    {
                        tb.Text = Convert.ToDouble(ds.Tables[0].Rows[i]["IndividualPriceInput"]).ToString();
                    }
                    tb.TextChanged += new EventHandler(tb_PriceChanged);

                    rv = new RangeValidator();
                    rv.ID = "rv_Desserts_Individual_" + Convert.ToInt32(ds.Tables[0].Rows[i]["ident"]).ToString();
                    rv.Type = ValidationDataType.Currency;
                    rv.MinimumValue = "0";
                    rv.MaximumValue = "1000";
                    rv.ControlToValidate = tb.ID;
                    rv.Display = ValidatorDisplay.Dynamic;
                    rv.Font.Bold = true;
                    rv.ForeColor = Color.Red;
                    rv.SetFocusonerror = true;
                    rv.ErrorMessage = "Must be greater than or equal to 0!";

                    tc.Controls.Add(rv);
                    tc.Controls.Add(tb);
                }
                else
                {
                    tc.Text = "&nbsp;";
                }
                tr.Cells.Add(tc);
            }

Hope this helps
Was This Post Helpful? 0
  • +
  • -

#8 ragingben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: Make the text in a textbox bold

Posted 11 March 2011 - 02:36 AM

Quote

The part that I do have completed has nothing to do with my question, but here is my code anyways.


I don't want to sound like I'm ranting but, if it has nothing to do with the question then we don't really need to see it. I think what the guys were after was something showing that you had attempted to format the font, not some code unrelated from the question.

Anyway, back to the question :)

The class Control (which TextBox directly inherits from, has a property - Font. Font is a class, that describes, suprises suprise, a Font. This description includes properties of the font such as the font family (Courier, Arial, Times New Roman etc), the size (10, 11, 12 etc) and the fonts style (Bold, Normal, Italic etc).

The Font property of control has both a public getter and a setter, so you can set it to whatever you want.

One of the constructors for font takes these arguments: Font prototypeFont, FontStyle newStyle. This means that you can create a new font based on an old one, but just specify a new style, i.e make it bold, italis or whatever. That should help a bit...

Get to know MSDN, 90% of questions I see answered in this forum are already answered in the documentation there...
Was This Post Helpful? 0
  • +
  • -

#9 staindromeo   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 155
  • Joined: 21-October 10

Re: Make the text in a textbox bold

Posted 11 March 2011 - 12:49 PM

Alright I think what I said was misunderstood. I was just trying to display my code that was creating the rows, cells and textboxes. I know about the font properties, & I was trying to do this dynamically whenever a text is changed or on input, so that theory isn't going to help me. I tried inputting a grid view & editing the font weight in there to bold. But everything shows up as bold, hence I was trying to do this in C#.

Here's the gridview code: (which works but it doesn't let me do it dynamically)
<asp:TextBox id=txtOption style="font-weight:bold;" runat="server" Width="55px" text='<%# DataBinder.Eval(Container.DataItem, "Option1", "{0:#0.00}") %>'>
													</asp:TextBox>

Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Make the text in a textbox bold

Posted 11 March 2011 - 02:22 PM

OK, so maybe this is an ASP.NET question so I'll move it there.
Was This Post Helpful? 0
  • +
  • -

#11 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: Make the text in a textbox bold

Posted 11 March 2011 - 05:19 PM

How are you bringing the textboxes into the gridview?

Are you inserting them into the gridview at some point, or is this just when the gridview row is being edited? Can you post the entire code for your gridview?
Was This Post Helpful? 0
  • +
  • -

#12 staindromeo   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 155
  • Joined: 21-October 10

Re: Make the text in a textbox bold

Posted 12 March 2011 - 09:33 AM

View PostNakor, on 11 March 2011 - 05:19 PM, said:

How are you bringing the textboxes into the gridview?

Are you inserting them into the gridview at some point, or is this just when the gridview row is being edited? Can you post the entire code for your gridview?


I solved this, by making all the textboxes populate as blank on load & I just set the font property as bold. So the changes will be reflected in bold.

Thanks for your help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1