Hi
Im not a developer. Im trying to increase my understanding of the developers I work with to help us both do our jobs efficiently.
To do this I'm using the Sams Teach yourself C# in 24 hours (which so far has taken a lot longer).
An exercise in Hr 4 is eluding me.
The objective is to create a solution whereby a text box in a form populates with the new width of the form when the form is resized.
Ive created my form and text box and opened the resize event. Ive then input the following code:
namespace Form1_Event
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
txtSize.Text = "I dont know how to do this";
}
}
}
You'll notice ive put in the text "I dont know how to do this" as my previous attempts of "txtSize.Text = Form1.Width" (and other variations of this) have produced the following error:
"An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Control.Width.get'
Any ideas?
Many thanks
Tim
Loading
Richard BrennanPosted Apr 12, 2011, 3:24 PM
this.txtSize.Text = this.Width.ToString();Sam HobbsPosted Apr 12, 2011, 9:52 PM
I would like to add one more thing, although I am not looking for points. Note that there is a difference between the width of a form and the size of the area for the controls in the form. The area in the form for putting controls is called the ClientSize (it is a property of the form). There is not much difference between the ClientSize width and the form's width but there is more difference between the two heights. It might be interesting to use the techniques you have learned to explore the ClientSize too.
Note that you can use String.Format to combine numbers and strings (and other data types) together; the ToString methiod just does one number but String.Format can do more. I suggest exploring String.Format too. Note that String.Format is a static function; I hope the book explains what that means. Please don't blame me for the book; it was written (published?) by a different Sam.
Tim ParsonsPosted Apr 12, 2011, 6:16 PM
Thanks to both of you.
Richard BrennanPosted Apr 12, 2011, 4:00 PM
VulpesPosted Apr 12, 2011, 3:48 PM
I should have realized in my first post that the ToString() conversion would be needed but seem to have been half-asleep at the time :)
Richard BrennanPosted Apr 12, 2011, 3:35 PM
the tostring() converts the width (number) to a string (letters) so that it can be accepted by the .text property of the textbox.
Im sure Vupes will give a more technical explanation as he his more experienced in explaining things.
VulpesPosted Apr 12, 2011, 3:32 PM
In the case of a numeric type (such as int), this just converts the number to a string.
Tim ParsonsPosted Apr 12, 2011, 3:29 PM
What does the ToString() add to the code? What function does it serve?
VulpesPosted Apr 12, 2011, 3:24 PM
txtSize.Text = this.Width.ToString();
Tim ParsonsPosted Apr 12, 2011, 3:23 PM
I had also tried this but received the following error (for both solutions suggested):
" Cannot implicity convert type 'int' to 'string' "
VulpesPosted Apr 12, 2011, 3:20 PM
txtSize.Text = this.Width;
However, if you don't specify 'this' then the current Form instance is assumed. So this should also work:
txtSize.Text = Width;