?
Solved

Textbox format vb 2005

Posted on 2009-04-24
24
Medium Priority
?
649 Views
Last Modified: 2013-12-26
Hello  i have  a textbox that needed to display the total .Now the total is apear  like (example) 110
i want to format it to be like $110.00  can someone give me the code/I
0
Comment
Question by:demoniumz
  • 14
  • 10
24 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24230107
You can use the following:

textBox1.Text = Strings.FormatCurrency(amount)
if you are on a computer where the language settings are american.
0
 
LVL 39

Expert Comment

by:abel
ID: 24230138
If you do not know for sure where your program will run, you can specify a culture. It becomes a bit more verbose:

textBox1.Text = amount.ToString("{0:C}", CultureInfo.CreateSpecificCulture("en-US"))

If you want to set that culture for your whole program, it depends whether you have a web application or whether you are using a winforms application. For a web application, it should best be set globally in web.config. For a desktop application it should be set using

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
somewhere in the beginning of your application.

-- Abel --


0
 
LVL 1

Author Comment

by:demoniumz
ID: 24230180
with the first comand in your firtsnpost    textbox1....(amount) the textbox apear like $0.00 witth these code?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 39

Expert Comment

by:abel
ID: 24230211
Then there's nothing inside amount? You need to pass in a variable that holds the correct amount and I assumed you had such a variable. I mean it like this:

Dim amount As Decimal = 110.0D
textBox1.Text = Strings.FormatCurrency(amount)
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24231455
i mean my freind i  want to have something like this
OrdersDetailsDataGridView.Columns(5).DefaultCellStyle.Format = "¬0.00"

These is for datagrid view total row i want to display "¬0.00"  when a new peoduct added to  update the amount
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24231459
i mean my freind i  want to have something like this
OrdersDetailsDataGridView.Columns(5).DefaultCellStyle.Format = "$0.00"
These is for datagrid view total row.Me want i  want is for Textbox

i want to display "$0.00"  when a new product added to  update the amount  not  remain $0.00

understand what i mean?
0
 
LVL 39

Expert Comment

by:abel
ID: 24231960
The format string for a currency is "{0:C}", but in the case of the DefaultCellStyle.Format, you should only use the currency formatter part (I know, it is always very unclear what syntax to use where in .NET when it comes to formatting):

OrdersDetailsDataGridView.Columns(5).DefaultCellStyle.Format = "c"
-- Abel --
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24237071
problem  why ehen in the form  lets say there is a product Quantity 8 and amount 1.66 give 8 euro
 the real result is 8.30 euro why that ?
0
 
LVL 39

Expert Comment

by:abel
ID: 24237131
That's on another page and I have no idea, please show the code that demonstrates that problem.
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24237180
Public Class Norder

    Private Sub Norder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Private Sub OrdersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.OrdersBindingSource.EndEdit()
        Me.OrdersTableAdapter.Update(Me.NicolaouDBDataSet.Orders)

    End Sub


   
    Private Sub Addpr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Addpr.Click
        Dim bsource As BindingSource = Me.OrdersDetailsDataGridView.DataSource
        Dim ds As DataSet = DirectCast(bsource.DataSource, DataSet)
        Dim orderdetailstable As New DataTable
        Dim dr As DataRow = ds.Tables("OrdersDetails").NewRow
        dr("OrderID") = OrderIDTextBox.Text
        dr("ProductID") = Productidcombobox.SelectedValue
        dr("UnitPrice") = Unitpricetextbox.Text
        dr("Quantity") = QuantityTextBox.Text
        dr("productname") = ProductNameTextBox.Text
        dr("Discount") = 0
        dr("Total records") = 0
        dr("Total") = Convert.ToDecimal(Unitpricetextbox.Text.ToString()) * Convert.ToDecimal(QuantityTextBox.Text.ToString())

        ds.Tables("OrdersDetails").Rows.Add(dr)

        TotalSum()
    End Sub
    Private Sub OrdersDetailsBindingSourceBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.OrdersDetailsBindingSource.EndEdit()
        Me.OrdersDetailsTableAdapter.Update(Me.NicolaouDBDataSet.OrdersDetails)

    End Sub


    Private Sub clearbu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delbut.Click

        OrdersDetailsDataGridView.Rows.Remove(OrdersDetailsDataGridView.CurrentRow)

        TotalSum()
    End Sub

    Private Sub TotalSum()
        Dim sum As Decimal = 0
        For Each row As DataGridViewRow In OrdersDetailsDataGridView.Rows
            row.Cells(5).Value = row.Cells(3).Value * row.Cells(4).Value
            sum += row.Cells(5).Value
        Next
        Me.TextBox1.Text = sum
        Me.TextBox1.Text = sum.ToString("¬0.00")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearbut.Click
        Me.NicolaouDBDataSet.OrdersDetails.Clear()
    End Sub

    Private Sub movenext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movenext.Click
        OrdersBindingSource.MoveNext()
    End Sub

    Private Sub moveprevius_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles moveprevius.Click
        OrdersBindingSource.MovePrevious()

    End Sub

    Private Sub movelast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movelast.Click
        OrdersBindingSource.MoveLast()

    End Sub

    Private Sub movefirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movefirst.Click
        OrdersBindingSource.MoveFirst()

    End Sub

    Private Sub addrec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addrec.Click
        Me.NicolaouDBDataSet.OrdersDetails.Clear()
        TextBox1.Text = String.Empty
        OrdersBindingSource.AddNew()

    End Sub

    Private Sub delrec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delrec.Click
        OrdersBindingSource.RemoveCurrent()

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Suppliers' table. You can move, or remove it, as needed.
        Me.SuppliersTableAdapter.Fill(Me.NicolaouDBDataSet.Suppliers)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Products' table. You can move, or remove it, as needed.
        Me.ProductsTableAdapter.Fill(Me.NicolaouDBDataSet.Products)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.OrdersDetails' table. You can move, or remove it, as needed.
        Me.OrdersDetailsTableAdapter.Fill(Me.NicolaouDBDataSet.OrdersDetails)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Suppliers' table. You can move, or remove it, as needed.
        Me.SuppliersTableAdapter.Fill(Me.NicolaouDBDataSet.Suppliers)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Orders' table. You can move, or remove it, as needed.
        Me.OrdersTableAdapter.Fill(Me.NicolaouDBDataSet.Orders)
        OrdersDetailsDataGridView.Columns(5).DefaultCellStyle.Format = "¬0.00"

        TotalSum()
    End Sub

    Private Sub email_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles email.Click
        Nemail.Show()
    End Sub

    Private Sub callendar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles callendar.Click
        Ncalendar.Show()
    End Sub

    Private Sub touch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles touch.Click
        Process.Start("osk.exe")
    End Sub

    Private Sub infos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles infos.Click
        NicolaouInfos.Show()
    End Sub
End Class
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24237188
  Private Sub TotalSum()
        Dim sum As Decimal = 0
        For Each row As DataGridViewRow In OrdersDetailsDataGridView.Rows
            row.Cells(5).Value = row.Cells(3).Value * row.Cells(4).Value
            sum += row.Cells(5).Value
        Next
        Me.TextBox1.Text = sum
        Me.TextBox1.Text = sum.ToString("$0.00") The total column (textbox)
    End Sub

 
 OrdersDetailsDataGridView.Columns(5).DefaultCellStyle.Format = "$0.00"

the total of row
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24237317
so?
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24237613
abel:can u help
0
 
LVL 39

Expert Comment

by:abel
ID: 24239526
Sorry, I went to bed... (it was midnight on this side of earth) Remember, we're all volunteers, and we do this in our sparse free time, so pushing is not going to help.

I see from the code above that you didn't apply the suggestions I gave you. You write, last line above, the following:

OrdersDetailsDataGridView.Columns(5).DefaultCellStyle.Format = "$0.00"
but you should make it this:

OrdersDetailsDataGridView.Columns(5).DefaultCellStyle.Format = "$0.00"
You have other places where you try sum.ToString("¬0.00"), try instead, sum.ToString("{0:C"}. Also, as suggested earlier.
 
You seem to suggest that your total is not correct. I cannot judge that, because I have not idea what's in the columns that you are calculating. But it is a common mistake that people think that Cell(1) is the very first cell. It isn't, the first cell is Cell(0). Same accounts for Column and Row. Might that be where your problem is?

If I'm wrong here, please post your code again (this time in the code snippet section please!) and comment on what you expect where and what the current wrong values are. You can find that by stepping through your code with F10 after you hit a breakpoint.

-- Abel --
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24239587
Your sugested before is tried giving the same result
0
 
LVL 39

Expert Comment

by:abel
ID: 24239812
I see that I made a copy and paste error in that last comment. The bold line should have been

OrdersDetailsDataGridView.Columns(5).DefaultCellStyle.Format = "c"
You are not really giving me the information I need to help you with this issue. Let's do this step by step. Please try the following and give me the results from the debug window:

Dim dblTest As Double = 12.34Debug.WriteLine("test double as amount: " & dblTest.ToString("c"))

0
 
LVL 1

Author Comment

by:demoniumz
ID: 24239858
<System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
     System.ComponentModel.BrowsableAttribute(true),  _
     System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)>  _
    Public Overrides Property SchemaSerializationMode() As System.Data.SchemaSerializationMode
        Get
            Return Me._schemaSerializationMode
        End Get
        Set
            Me._schemaSerializationMode = value
        End Set
    End Property

i get these with a yellow thing in front
0
 
LVL 39

Expert Comment

by:abel
ID: 24239880
Now you got me totally lost. This is about a serialization mode for a schema, I don't see what this has to do with a textbox. And I don't see anything yellow (use a screenshot and upload it if you want to show something visually). Can you please elaborate on what you are after? And can you please answer my last comment after trying that out?
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24239923
i  am totaly confused me 2:)
here is my database can u  check if the total column in table order details is in corect format
NicolaouDB.mdb
0
 
LVL 39

Expert Comment

by:abel
ID: 24240331
I don't see what this has to do with your original request on how to format a format a textbox. I'm not an Access guru. But the Total column is clearly of some Integer type, it cannot accept decimals. Why are you asking? What is the relation?

If that is the field that you want to display in a textbox, the result will be something like $11.00, where the "00" is always "00".
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24240504
ok   iask  you sonmething  now  in order to  not be  alwayss 00 what i  must put?To  give me the corect cents
0
 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24240522
Well, first you should change the database so that if you are inside the database you see the correct values.

Then, we can go back to your code and try the yourTotalFromDbHere.ToString("c") again, which should then "just work".
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24240566
ok  i try
0
 
LVL 1

Author Closing Comment

by:demoniumz
ID: 31576172
thnks abel and cool down men:)
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question