Link to home
Start Free TrialLog in
Avatar of demoniumz
demoniumz

asked on

Textbox format vb 2005

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
Avatar of abel
abel
Flag of Netherlands image

You can use the following:

textBox1.Text = Strings.FormatCurrency(amount)
if you are on a computer where the language settings are american.
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 --


Avatar of demoniumz
demoniumz

ASKER

with the first comand in your firtsnpost    textbox1....(amount) the textbox apear like $0.00 witth these code?
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)
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
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?
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 --
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 ?
That's on another page and I have no idea, please show the code that demonstrates that problem.
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
  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
so?
abel:can u help
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 --
Your sugested before is tried giving the same result
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"))

<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
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?
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
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".
ok   iask  you sonmething  now  in order to  not be  alwayss 00 what i  must put?To  give me the corect cents
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok  i try
thnks abel and cool down men:)