-2

With the following code, I delete line by line in a TextBox (0, 39). Now there is on the last place a money amount (1 any Articel 10.00) which I want to deduct from the total amount. For that, I use the Substring. But there I get errors, as probably the empty spaces are not interpreted. Is there a simple solution to this? Thanks

private void btnDelete_Click(object sender, EventArgs e)
{            
    if (TextBox1.Text.Length > 0)            
    {
        txtTotal.Text = (Double.Parse(txtTotal.Text) - Double.Parse(TextBox1.Text.Substring(8, 2))).ToString("0.00");
        TextBox1.Text = TextBox1.Text.Remove(0, 39);               
    }

    if (TextBox1.Text.Length == 0)
    {
        MessageBox.Show("The cart is empty");
JaDoLo
  • 111
  • 9
  • Do you get an exception? Which the line the error occurs? – Chayim Friedman Aug 20 '18 at 14:17
  • 4
    Have you considered using some kind of grid / table instead of a textbox for entering these values? IMHO a multi-line textbox seems like kind of a strange choice for that... – bassfader Aug 20 '18 at 14:17
  • 3
    Sorry if I'm being blunt, but if you need to consider something like that for an application like in your screenshot, you should reconsider your approach. The views in the text boxes should be "read only" and always generated from the contents of your actual shopping cart object. – AKX Aug 20 '18 at 14:17
  • What's your error? – Md. Abdul Alim Aug 20 '18 at 14:19
  • Nimm bitte ein geeignetes Element! ListView or DataGridView.. – TaW Aug 20 '18 at 14:19
  • @TaW And in English? We don't undestand German! (me, at least). – Chayim Friedman Aug 20 '18 at 14:23
  • I can not use a grid for that. There is already a lot going on in this TextBox. For everything else I have found solutions. That's the last open point. – JaDoLo Aug 20 '18 at 14:26
  • 2
    @ChayimFriedman He wrote "Please use an appropiate control! ListView or DataGridView". – ckuri Aug 20 '18 at 14:27
  • 1
    @ChayimFriedman: I just highlighted the text & did Bing Translate : _"Please take a suitable ..."_ (though I had guessed pretty much that anyway) – PaulF Aug 20 '18 at 14:28
  • Can substring not be used from the other side? `(1 any Articel 10.00) <==` – JaDoLo Aug 20 '18 at 14:30
  • @PaulF I did it by Google Translate, how I did know that this is German? But we shouldn't do this! – Chayim Friedman Aug 20 '18 at 14:30
  • 2
    _There is already a lot going on in this TextBox._ Umso schlimmer. (All for the worse). _That's the last open point._ Um, that's just what you believe; this terrible desing will blow up at any moment.. (@ Chayim (The whole screenshot was in german, so I tried to bring home the point directly. .No problem in a comment, imo)) – TaW Aug 20 '18 at 14:30
  • @JaLo: Two persons have askes which line bring which errors. Why can't you tell? – TaW Aug 20 '18 at 14:35
  • `@TaW` I get no errors, the problem is that the area because of the blank lines can not be set to the right place. then I get (out of range) logical. The question is, what possibilities exist. – JaDoLo Aug 20 '18 at 14:43
  • As suggested - most possibilities involve using an appropriate control. Exactly why can't you use a grid or other suitable control. – PaulF Aug 20 '18 at 14:44
  • A ListView would be feasible. There should be no grid structures. Then maybe I'll rebuild that thing. Unless I find a reasonable solution to the problem. Anyway, thank you all for the answers. – JaDoLo Aug 20 '18 at 14:52
  • 1
    *"There should be no grid structures"* >> Why? This really makes no sense to me... Why you cannot or refuse to use a grid? You're trying to display data in a tabular way / in a tabular structure, but then you say that *"there should be no grid structures"*, but this is exactly what grids are made for... – bassfader Aug 20 '18 at 14:56
  • I guarantee you that I have very clear reasons for this. I asked a damn question. This is not about whether I use a grid or not, it is all about the quested thing. then I'll find a solution myself. Everyone who clicks on the negative button should ask questions his self. "Yogibären, können eine klare Frage nicht von einem bereits bestehenden Lösungsansatz unterscheiden. – JaDoLo Aug 20 '18 at 15:09
  • People here have tried to help you by giving reasonable advice. We have asked why you want to avoid using appropriate controls but you have not given reasons. You say _"For that, I use the Substring. But there I get errors, as probably the empty spaces are not interpreted. Is there a simple solution to this?"_ and do not tell us what errors you are getting - giving details of that may allow us to help you use your (bad) solution. One thing you could do is to use the debugger - look at what the SubString returns - that may give you an idea why you get an error. – PaulF Aug 20 '18 at 15:22
  • The question does not refer to mistakes, since it is logical where they come from. It's about solutions. I have found the solution in the meantime. – JaDoLo Aug 20 '18 at 18:44

1 Answers1

1

Few things you can do to make your life easier (assuming you have to keep a TextBox as you have stated to others.)

Before I get into the details however, the issue seems to be you're having trouble parsing text that represents lines of data, data which contains amounts which you want to act on. If this is an incorrect assumption, disregard this answer.

Ok, back to it...

  1. Rather than trying to work with the text directly in the TextBox, start by reading in your entire string as a list of lines (i.e. List<String>). You can do that with the Split function or with RegEx expressions. See here

  2. Use RegEx expressions for each line to identify not just the type of line it is (an 'item' line or the 'All' line at the bottom) as well as the various parts of those lines. For instance, you can use a RegEx that starts at the end of the line and goes backwards looking for a number (in the form of a string.) Use the result of that for your Parse method to get the actual numeric value.

Finally, if you still need to remove the lines of text (I'm not sure if you're removing the text just for your logic or if you need to display it) simply remove them from your list of strings for the lines. If it needs to be displayed back in the UI (doubtful as it seems it should be blank at the end of processing) just use Join to convert the lines back to a string, then set that back to the TextBox.Text property.

Hope this helps!

Mark

P.S. To (try and) avoid comments such as the ones you got about your design, it may help to start your question by saying something like 'Unfortunately I'm restricted to using a TextBox due to issues outside of this question, hence I'm looking for an answer here.' At least that should cut back on those responses telling you to 'Do it differently!' instead of answering your question.

Community
  • 1
  • 1
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • `@MarqueIV` thank you for your helpful support. In some cases, larger pictures are also inserted, so that grid structures restrict my solution. In the beginning I tested everything and found that a TextBox gives the best results, although the development is a bit more complicated. You often do not even have the time to respond to all the critics, it hails already negative entries before you can even say anything, just because many think, Ohh, he does not know what he does !! – JaDoLo Aug 21 '18 at 06:14
  • 1
    Glad I could help. While I personally don't quite understand your approach, I've written enough questions here to know that sometimes you have to do something crazy, and you need an answer for that specific thing and aren't asking for design help. Still, people will try and help you find a "better" solution whether you want it or not. Sometimes they're right. Not always, but sometimes. In the case where they aren't and you really, *really* have to do something that seems crazy, it can be really frustrating to get people to answer your question. Happens to me all the time. :) – Mark A. Donohoe Aug 21 '18 at 06:22
  • Yes, to explain what I really want to do with everything would go beyond the scope. There should be more of your kind of developer. My experience shows that if the posted code is too complicated then nobody answers. But if the code is simple, everyone believes you are a beginner. Then suddenly everyone answers, even those who have less idea than you selv. – JaDoLo Aug 21 '18 at 06:46
  • 1
    One thing I do is start the questions with a TLDR block that explains what I'm trying to do in the simplest of terms. Then I write the full 'Now for the details...' parts. Still doesn't always help, but you have to keep trying, right? I also hope SO implements on-site chatting or something too so ppl can discuss questions and answers without cluttering up the comments. – Mark A. Donohoe Aug 21 '18 at 06:57