Assign value (numeric) to combox's items?

kyto

Member
Licensed User
Longtime User
Hi! I´m Newbie to Basic4ppc (Excelent tool).

My intention is to create a ComboBox that it contains Items (Product 1, Product 2, Product 3…) And on having selected, automatically there is assigned to him a numerical value (Cost) that appears in a Textbox.
Is it possible to do this?
Attachment sample file.
Thanks for his attention.
 

Attachments

  • Product.sbp
    606 bytes · Views: 261

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are several ways to achieve this.
One way is to save the prices in an ArrayList:
You will need to add an ArrayList from the designer.
B4X:
Sub Globals
    
End Sub

Sub App_Start
    Form1.Show
    ArrayList1.Add(10)
    ArrayList1.Add(20)
    ArrayList1.Add(30)
End Sub

Sub ComboBox18_SelectionChanged (Index, Value)
    TextBox1.Text = ArrayList1.Item(Index)
End Sub
 

kyto

Member
Licensed User
Longtime User
Thank you.

I will try it.
Thank you very much!
 

Cableguy

Expert
Licensed User
Longtime User
This kind of relation can be easily created using a ini file containing both product name and cost and add simultaneously ( to the same index ) the product name to a combobos and the cost to an array list...
 

kyto

Member
Licensed User
Longtime User
Thanks! It works, but another question...

It works, both! For now, I’m using Erel´s method.
Now I added another ComboBox (Tax) that it contains ítems ( A, B, C) whit another assigned cost (1,2,3).
Whit a button click, I calculate total cost of both items input previously.

Sub Button1_Click
Textbox3.Text=textbox1.Text+textbox2.Text
End Sub

It works perfectly, My question is: I can calculate and show automatically these result without the button?


B4X:
Sub Globals
    
End Sub

Sub App_Start
    Form1.Show
    ArrayList1.Add(10)
    ArrayList1.Add(20)
    ArrayList1.Add(30)
    ArrayList2.Add(1)
    ArrayList2.Add(2)
    ArrayList2.Add(3)   
End Sub

Sub ComboBox18_SelectionChanged (Index, Value)
    TextBox1.Text = ArrayList1.Item(Index)
End Sub

Sub ComboBox2_SelectionChanged (Index, Value)
    TextBox2.Text = ArrayList2.Item(Index)
End Sub

Sub Button1_Click
Textbox3.Text=textbox1.Text+textbox2.Text
End Sub

Thanks for his responses.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
What about this:
B4X:
Sub App_Start
    Form1.Show
    ArrayList1.Add(10)
    ArrayList1.Add(20)
    ArrayList1.Add(30)
     ArrayList2.Add(1)
    ArrayList2.Add(2)
    ArrayList2.Add(3)    
    ComboBox18.SelectedIndex = 0
    ComboBox2.SelectedIndex = 0
End Sub

Sub ComboBox18_SelectionChanged (Index, Value)
    TextBox1.Text = ArrayList1.Item(Index)
     Textbox3.Text=textbox1.Text+textbox2.Text
End Sub

Sub ComboBox2_SelectionChanged (Index, Value)
    TextBox2.Text = ArrayList2.Item(Index)
    Textbox3.Text=textbox1.Text+textbox2.Text
End Sub
BTW, I recommend you to change the controls names to a more meaningful name. This will help you once your project grows.
 

kyto

Member
Licensed User
Longtime User
Error: Input string was not in a correct format.

I will change the control names, thanks for recommendation.

I used the code
Sub App_Start
Form1.Show
ArrayList1.Add(10)
ArrayList1.Add(20)
ArrayList1.Add(30)
ArrayList2.Add(1)
ArrayList2.Add(2)
ArrayList2.Add(3)
ComboBox18.SelectedIndex = 0
ComboBox2.SelectedIndex = 0
End Sub

Sub ComboBox18_SelectionChanged (Index, Value)
TextBox1.Text = ArrayList1.Item(Index)
Textbox3.Text=textbox1.Text+textbox2.Text
End Sub

Sub ComboBox2_SelectionChanged (Index, Value)
TextBox2.Text = ArrayList2.Item(Index)
Textbox3.Text=textbox1.Text+textbox2.Text
End Sub

but it indicates me an error:
"An error occurred on sub combobox18 selectionchanged.
Line number: 14

Textbox3.Text= textbox1.Text+textbox2.Text
Error description:
Input string was not in a correct format.
Continue?...

Which is the error?
 

taximania

Well-Known Member
Licensed User
Longtime User
This kind of relation can be easily created using a ini file containing both product name and cost and add simultaneously ( to the same index ) the product name to a combobos and the cost to an array list...


:cool: Using the binary.dll ? you could create and save, 'then modify if you wish', a file on disk .dat .ini .ect so the information or value's you want are already set in the textbox? 'values'. Read the values of the file with your App_Start Sub

This is an addition I could have made to my port settings in my 'bad' GPS example. I didn't.

Perhaps I'll try and see if it works :sign0161:
 

Cableguy

Expert
Licensed User
Longtime User
I was not targeting the binary dll in my answer, only how to create and matain a valid relation beetween the combobox and the textboxes values...

Sorry but I just don't see were binary dll was mentioned in this thread...
 
Last edited:

kyto

Member
Licensed User
Longtime User
Solved.Thanks.

Solved!
I added:
TextBox2.Text = "0"
TextBox3.Text = "0"

It works.
Thanks!
 

kyto

Member
Licensed User
Longtime User
How to do it?

Now it works as i wanted it, but now I want to add another textbox (Class) that should indicate a letter (A, B, C) depending on the result of textbox3 (Total).
For example, if the Total is 2, textbox (Class) shows "A", if the result is 3, textbox (Class) shows "B".
I tried it, but I do not find the form.
Is it possible?

Sub App_Start
TextBox2.Text = "0"
TextBox3.Text = "0"
Form1.Show
ArrayList1.Add(1)
ArrayList1.Add(2)
ArrayList1.Add(3)
ArrayList2.Add(1)
ArrayList2.Add(2)
ArrayList2.Add(3)
ComboBox18.SelectedIndex = 0
ComboBox2.SelectedIndex = 0
End Sub

Sub ComboBox18_SelectionChanged (Index, Value)
TextBox1.Text = ArrayList1.Item(Index)
Textbox3.Text=textbox1.Text+textbox2.Text
End Sub

Sub ComboBox2_SelectionChanged (Index, Value)
TextBox2.Text = ArrayList2.Item(Index)
Textbox3.Text=textbox1.Text+textbox2.Text
End Sub

Erel: Thanks for your patience with this beginner.
 

Cableguy

Expert
Licensed User
Longtime User
You should try case select...See the help about Case....
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Kyto,

As CableGuy points out, you should use the Select keyword.
I've attached what I think it is that you require.

Regards,
RandomCoder.
 

kyto

Member
Licensed User
Longtime User
Something like this...

Thanks for his responses.

What I need is something similar; I need to develop some simple applications of use in medicine, to calculate some scales, for example a so-called Modified Child-Turcotte-Pugh classification for cirrhosis (Attached).
Depending on the chosen parameters, a score is obtained (Total numerical score), and from this one, there is obtained the class (Child-Turcotte-Pugh class):
“A” if total numerical score is 5-6 points.
“B” if total numerical score is 7-9 points.
“C” if total numerical score is 10-15 points.

I will try with his suggestions.

Regards.
 

Cableguy

Expert
Licensed User
Longtime User
I think this is what you wanted...
 

kyto

Member
Licensed User
Longtime User
Yes!

This is exactly what I want! Of this I will base for other applications, which have basically the same structure.

Thank you very much Cableguy!
:sign0060:
 

LineCutter

Active Member
Licensed User
Longtime User
What I need is something similar; I need to develop some simple applications of use in medicine, to calculate some scales
This is clearly a good way to learn some programming skills, but in the time between now & then end of your learning curve you might want to take a look at Medcalc (www.med-ia.ch)
Also, be VERY careful that your calculations give the correct answers under all circumstances if you ever plan on using them for real!
 
Top