-1

Problem:

I want to change the size of the panel and save it so the next time I open the program it will keep the size of the panel. Because of this I manage to use my.settings with type of system.drawing.size

However I am having a problem on converting it, so I can initially output the default size of my my.settings

My.Settings.pnl_service_size = Tb_Pnl_Service_Size.Text

Error 1 Value of type 'System.Drawing.Size' cannot be converted to 'String'.

I am aware on how to use my.settings, I am just having a hard time on converting this so i can output it to my textbox.text.

Edit:

How I populate the textbox is in OnLoad event

Tb_Pnl_Service_Size.Text = My.Settings.pnl_service_size.ToString 'I populate the textbox
Pnl_Service.Size = New Size(My.Settings.pnl_service_size) 'The panel that I want to change the size

How I save the textbox is by pressing the OnClicked button

My.Settings.pnl_service_size = Tb_Pnl_Service_Size.Text.ToString
My.Settings.Save()
Cary Bondoc
  • 2,923
  • 4
  • 37
  • 60
  • Its wanting a string, not a size. So give it what it wants... Also your code shows assigning a string to a Size, where do you assign the Size to the string? – Jeremy Thompson Sep 14 '15 at 08:57
  • @JeremyThompson ah yes. I am assigning the size on form load using this code `Pnl_Service.Size = New Size(My.Settings.pnl_service_size)` and yes I also have `Tb_Pnl_Service_Size.Text = My.Settings.pnl_service_size` – Cary Bondoc Sep 14 '15 at 09:00
  • Just put a **ToString()** eg, ....Text =My.Settings.pnl_service_size.ToString() – Jeremy Thompson Sep 14 '15 at 09:02
  • I also tried converting it to string like this `CStr(My.Settings.pnl_service_size) = Tb_Pnl_Service_Size.Text`. I don't understand. – Cary Bondoc Sep 14 '15 at 09:02
  • Oh, yes. Thank you so much. It works, but I am having a problem on `Cstr(My.Settings.pnl_service_size) = Tb_Pnl_Service_Size.Text` I also tried `My.Settings.pnl_service_size.tostring = Tb_Pnl_Service_Size.Text` – Cary Bondoc Sep 14 '15 at 09:05
  • You only cast (eg cstr) variables you're assigning - in your comment you cstr the value your assigning to - that's wrong. Please provide the full code including the line the error occurs on – Jeremy Thompson Sep 14 '15 at 09:07
  • Done Sir, thank you so much for your assistance. I would love to learn why it doesn't work and how can I solve it. – Cary Bondoc Sep 14 '15 at 09:14

2 Answers2

1

A key piece of info is what type the variable is for the value in My.Settings.

My.Settings.pnl_service_size = Tb_Pnl_Service_Size.Text.ToString

This makes it look like it is string. The problem is that this will save something like: {Width=237, Height=133} which will easily not convert back to a Size variable.

If the Type used in Settings, is System.Drawing.Size then you do not need to convert to string to save it:

My.Settings.pnl_service_size = Pnl_Service.Size

Reset the size next time:

Pnl_Service.Size = My.Settings.pnl_service_size

You can show the value using .ToString() but since the result is String, it cannot by used as a Size.

TextBox1.Text = My.Settings.pnl_service_size.ToString()
user3697824
  • 538
  • 4
  • 15
  • Thanks, but how can I set a new size on `my.settings.pnl_service_size`? This code is my problem `My.Settings.pnl_service_size = Tb_Pnl_Service_Size.Text`. Yes, I declared the settings as `system.drawing.size`. – Cary Bondoc Sep 15 '15 at 00:38
  • `Tb_Pnl_Service_Size.Text` is a string, it is not a size. The first sentence in your question is about saving and restoring the size of a panel. So save it from the Panel as I show. Passing the size thru a Textbox changes it to a string so you get the error you. You can show it in a TB but you cannot use it as a size from there. Maybe you could explain why the Panel.Size needs to come from a TextBox – user3697824 Sep 15 '15 at 01:06
  • Thanks! Because of you I realized that I'm doing it the hard way, please see my answer. I now used the `string` as type of my `my.settings` instead of `system.drawing.size`. – Cary Bondoc Sep 15 '15 at 01:17
0

It seems that it is not possible to use the textbox to indicate the size of certain object.

I changed the type of my.settings from system.drawing.size to string the this is how i used it.

Populating the textbox during OnLoad event

'I populate the textbox
Tb_Pnl_Service_Size.Text = My.Settings.pnl_service_size 

'The panel that I want to change the size
Dim pnl_size As Array = Split(My.Settings.pnl_service_size, ",", 2)
Pnl_Service.Size = New Size(pnl_size(0), pnl_size(1)) 

Saving the textbox to my.settings by pressing the OnClicked button

My.Settings.pnl_service_size = Tb_Pnl_Service_Size.Text
My.Settings.Save()
Cary Bondoc
  • 2,923
  • 4
  • 37
  • 60