.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Wall total and Clipboard c#

22 REPLIES 22
Reply
Message 1 of 23
k005
1115 Views, 22 Replies

Wall total and Clipboard c#

Hello friends

How can I collect and separate the data in the listbox and store it in memory?

 

 

 

listbox content :

15 luk duvar : 1.17 m²
15 luk duvar : 4.81 m²
15 luk duvar : 7.67 m²
15 luk duvar : 7.8 m²
15 luk duvar : 10.4 m²
15 luk duvar : 3.51 m²
15 luk duvar : 4.42 m²
15 luk duvar : 15.6 m²
20 luk duvar : 15.34 m²
20 luk duvar : 15.34 m²
20 luk duvar : 30.94 m²
20 luk duvar : 30.94 m²

 

 

desired result: ( Ctrl + C ... clipboard)

 

Toplam 15 luk duvar toplam = 55.38 m²
Toplam 20 luk duvar toplam = 92.56 m²

 

 

 

 

 string onbes = "15 luk Duvar : ";
                string yirmi = "20 luk Duvar : ";
                

               
                Regex.IsMatch(onbes, @"^\d+$");

                foreach (var record in data)
                {
                    var rgx = new Regex(@"(\d+):(\d+)");
                    var matches = rgx.Matches((string)record);
                    if (matches.Count < 1) continue;
                   onbes = double.Parse(matches[0].Groups[1].Value);
                   yirmi = double.Parse(matches[0].Groups[2].Value);
                    sum1 += (onbes * 1);
                }

                Clipboard.SetText("15 luk duvar toplam : " + onbes);
                Clipboard.SetText("20 luk duvar toplam : " + yirmi);

 

 

 

 

* maybe there is another size wall... (Toplam 10 luk Duvar )    😞

22 REPLIES 22
Message 2 of 23
k005
in reply to: k005

 

var rgx = new Regex(@": (\d+)");

 

 

How should this distinction in regex be?

I have to sum the dotted or undotted values before m².

Message 3 of 23
jtoverka
in reply to: k005

Tags (2)
Message 4 of 23
_gile
in reply to: k005

Hi,

Assuming your "listbox" is build by the program, the program should already know the numerical values and it should not need to use Regex / Parse to get them from strings.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 23
k005
in reply to: _gile

 

Hi

 

but there are texts in numeric values?

Message 6 of 23
_gile
in reply to: k005

How are constructed the strings of the "listbox" ?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 23
k005
in reply to: _gile

 

 

By selecting the dbtexts in the dwg file, to listbox.

Message 8 of 23
_gile
in reply to: _gile

As said by @norman.yuan in an other topic (which seems related to this one), this is not a good practice to deal with numerical values contained in strings.

 

Anyway, if, one more time, you want somebody else to write the code for you, this snippet seemed to work in the C# Interactive window.

> var list = new[] {
. "15 luk duvar : 1.17 m²",
. "15 luk duvar : 4.81 m²",
. "15 luk duvar : 7.67 m²",
. "15 luk duvar : 7.8 m²",
. "15 luk duvar : 10.4 m²",
. "15 luk duvar : 3.51 m²",
. "15 luk duvar : 4.42 m²",
. "15 luk duvar : 15.6 m²",
. "20 luk duvar : 15.34 m²",
. "20 luk duvar : 15.34 m²",
. "20 luk duvar : 30.94 m²",
. "20 luk duvar : 30.94 m" };
> var rgx = new Regex(@"^(\d+) luk duvar : (-?\d+([.]\d+)?) m²$");
> list.Select(s => rgx.Match(s).Groups)
.     .Where(g => 2 < g.Count)
.     .GroupBy(g => g[1].Value)
.     .Select(g => $"Toplam {g.Key} luk duvar toplam = {g.Sum(x => double.Parse(x[2].Value))} m²")
.     .ToArray()
string[2] { "Toplam 15 luk duvar toplam = 55.38 m²", "Toplam 20 luk duvar toplam = 61.62 m²" }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 23
k005
in reply to: _gile

 

I wrote this code. There is no problem for now.

I answered because you answered. This was asked a long time ago.

Also, it is not nice to say that someone else writes for you. Then wouldn't my efforts be in vain? 😉

Message 10 of 23
_gile
in reply to: k005

I'm sorry I didn't want to offend you.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 11 of 23
k005
in reply to: _gile

Hello @_gile 

I am giving the screen output of the result that I have coded.

* But in the code you sent, how will the wall totals of size 15 and 20 be separated? i didn't see him.

* Data is taken to Listbox, there is no problem here. so the list in your code did not come from listbox1.

How do we edit the code accordingly?

 

 

 

 

   var list = new[] { listBox1.Items };
                var rgx = new Regex(@"^(\d+) luk duvar : (-?\d+([.]\d+)?) m²$");
                list.Select(s => rgx.Match(s).Groups)
                .Where(g => 2 < g.Count)
                .GroupBy(g => g[1].Value)
                .Select(g => $"Toplam {g.Key} luk duvar toplam = {g.Sum(x => double.Parse(x[2].Value))} m²")
                .ToArray();

 

 

 

Message 12 of 23
_gile
in reply to: k005

The code I provided uses Linq with the IEnumerable<T> extension methods. You should study these methods, specifically Select and GroupBy.

You can convert the ObjectCollection of listBox.Items into and IEnumerable<string> with the Cast extension method.

 

listBox.Items
.Cast<string>()
.Select(s => rgx.Match(s).Groups)
.Where(g => 2 < g.Count)
.GroupBy(g => g[1].Value)
.Select(g => $"Toplam {g.Key} luk duvar toplam = {g.Sum(x => double.Parse(x[2].Value))} m²")
.ToArray();

 

 

Anyway, since you don't seem to be comfortable with Linq, you can do the same thing in an imperative way.

 

var results = new Dictionary<string, double>();
foreach (string s in listBox.Items)
{
    var groups = rgx.Match(s).Groups;
    if (2 < groups.Count)
    {
        if (results.ContainsKey(groups[1].Value))
            results[groups[1].Value] += double.Parse(groups[2].Value);
        else
            results[groups[1].Value] = double.Parse(groups[2].Value);
    }
}
var resultStrings = new List<string>();
foreach (KeyValuePair<string, double> pair in results)
{
    resultStrings.Add($"Toplam {pair.Key} luk duvar toplam = {pair.Value} m²");
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 13 of 23
k005
in reply to: _gile

@_gile 

 

This work will be a different example for me. I'll compile the codes tonight and try.

Thank you. Thanks to you, I started to progress very quickly in C #.

Message 14 of 23
k005
in reply to: _gile

@_gile 

 

I've looked into the IEnumerator issue. tutor. I watched.

Now these two codes are separate from each other? Or should they run at the same time?

Can you add a message box for the codes or the output to the code so that I can understand this much better?

Message 15 of 23
_gile
in reply to: k005

These two codes are two different ways to do the same thing (as said in the upper message).

You should have been able to see that and you should be able to display the result of both in a message box.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 16 of 23
k005
in reply to: _gile

 

OK. I ask him how?

Message 17 of 23
k005
in reply to: _gile

@_gile 

 

listBox.Items
.Cast<string>()
.Select(s => rgx.Match(s).Groups)
.Where(g => 2 < g.Count)
.GroupBy(g => g[1].Value)
.Select(g => $"Toplam {g.Key} luk duvar toplam = {g.Sum(x => double.Parse(x[2].Value))} m²")
.ToArray();

Now how can I get this code result in the message box?

Message 18 of 23
_gile
in reply to: k005

What have you tried so far ?

Look at the String.Join() method.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 19 of 23
k005
in reply to: _gile

I typed in the message box and called g.

It's okay if you give it as an alternative. but i don't understand this code...

Message 20 of 23
k005
in reply to: _gile

@_gile 

 

Are you really going to help on this platform?

I don't need theoretical knowledge... I'm just looking for an answer to my question. For this reason, if you are not going to help, do not write in the answer.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


AutoCAD Beta