Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Complete newb with C#
#1
I am creating a GUI with C#. I have a textbox, "ProductName", with ProductName.MaxLength set to 55 (this is for eBay). I want the user to be able to know how many characters they have left to work with. In essence I want this: a dynamic piece of text that shows "55 - len(ProductName)". I'm not very good with C# or XAML, so what do you guys recommend? How do I go about accomplishing this?
Reply
#2
In the Designer Code:
this.ProductName.TextChanged += new System.EventHandler(this.ProductName_TextChanged);

Generally, you can automatically achieve this by double clicking the object in the GUI, and it will create the code for you in both the Designer and the Form.
The form code should look like
Code:
private void ProductName_TextChanged(object sender, EventArgs e)
{
    lblCharactersLeft.Text = (ProductName.MaxLength - ProductName.Text.Length).ToString();
}
This will execute any time the text is changed, hence the TextChanged, so it will automatically update the number.
Reply
#3
JoeTang Wrote:In the Designer Code:
this.ProductName.TextChanged += new System.EventHandler(this.ProductName_TextChanged);

Generally, you can automatically achieve this by double clicking the object in the GUI, and it will create the code for you in both the Designer and the Form.
The form code should look like
Code:
private void ProductName_TextChanged(object sender, EventArgs e)
{
    lblCharactersLeft.Text = (ProductName.MaxLength - ProductName.Text.Length).ToString();
}
This will execute any time the text is changed, hence the TextChanged, so it will automatically update the number.

Fiel mentioned XAML, so I assume he's using WPF and not WinForms. For WinForms, Joe's correct. I have no idea how to do it in WPF, I've never used WPF. Double-clicking the text box in the designer would be a good place to start though.
Reply
#4
Code:
private void ProductName_TextChanged(object sender, TextChangedEventArgs e)
{
    lblCharactersLeft.Content = (ProductName.MaxLength - ProductName.Text.Length).ToString();
}
Try this. Apparently in XAML, labels use .Content instead of .Text, and the event is TextChangedEventArgs e instead of a generic EventArgs e.

What compiler/editor are you using, Fiel?
Reply
#5
MSVC# 2008 Express.

Is WinForms that much easier to use? I wouldn't know
Reply
#6
Fiel Wrote:MSVC# 2008 Express.

Is WinForms that much easier to use? I wouldn't know

I don't know anything about WPF, like Spaz, except earlier when I checked how to do the above. I've just always used WinForms.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)