none
Win Form & WPF App RRS feed

  • Question

  • Hi

    I’m majid, I have a c# code it work’s in Win Form Application but Far from it  doesn’t work in WPF Application(Bold part).

    I don’t know, anyone can help me?

    private int countCharacters (string text)

            {

                return text.Length;

            }

            private int countWords (string text)

            {

                if (txtWords.Text == string.Empty)

                    return 0;

                string[] strWords = text.Split(' ');

                return strWords.Length;

            }

            private void updateDisplay()

            {

                if (radWords.Checked)

                    lblResult.Text = countWords(txtWords.Text) + " Word(s)";

                else

                    lblResult.Text = countCharacters(txtWords.Text) + " Character(s)";

            }

            private void txtWords_TextChanged(object sender, EventArgs e)

            {

                updateDisplay();

            }

            private void radWords_CheckedChanged(object sender, EventArgs e)

            {

                updateDisplay();

                txtWords.Focus();

            }

            private void btnClear_Click(object sender, EventArgs e)

            {

                txtWords.Text = "";

                txtWords.Focus();

           


    Friday, October 16, 2015 9:28 AM

Answers

  • Hi Majid,

    In WPF if you want to set label value you have to use label's Content property. For checking if checkbox is checked or not checkbox's IsChecked property is used Its WPF style. Hope this helps you.

    private void updateDisplay()
    {
        if (radWords.IsChecked == true)
           lblResult.Content = countWords(txtWords.Text) + " Word(s)";
        else
           lblResult.Content = countCharacters(txtWords.Text) + " Character(s)";
    }

    Thanks,

    Sabah Shariq



    Friday, October 16, 2015 10:57 AM
  • The WPF label control is rather different to the windows forms control.

    In fact the two are so different that it's kind of dangerous to use windows forms code in wpf. It often will not behave as you expect.

    The wpf TextBlock is closer to a winforms label than the wpf label and you should usually use TextBlock in wpf for read only text. The wpf label has a content property because it's sort of weird from a winforms viewpoint and allows you to place ui inside it. So EG you could have a button set as content.

    Anyhow, TextBlock has a text property.

    .

    If you're learning wpf I suggest you consider learning MVVM as early as possible.

    It is the pattern that almost everyone uses for WPF ( and any XAML ) development.

    One plus of doing this is that you are less likely to use winforms code in your wpf app.


    Hope that helps.

    Technet articles: WPF: MVVM Step 1; All my Technet Articles

    • Proposed as answer by Christopher84 Friday, October 16, 2015 12:05 PM
    • Marked as answer by Kristin Xie Monday, October 26, 2015 2:01 AM
    Friday, October 16, 2015 11:13 AM

All replies

  • Hi Majid,

    In WPF if you want to set label value you have to use label's Content property. For checking if checkbox is checked or not checkbox's IsChecked property is used Its WPF style. Hope this helps you.

    private void updateDisplay()
    {
        if (radWords.IsChecked == true)
           lblResult.Content = countWords(txtWords.Text) + " Word(s)";
        else
           lblResult.Content = countCharacters(txtWords.Text) + " Character(s)";
    }

    Thanks,

    Sabah Shariq



    Friday, October 16, 2015 10:57 AM
  • The WPF label control is rather different to the windows forms control.

    In fact the two are so different that it's kind of dangerous to use windows forms code in wpf. It often will not behave as you expect.

    The wpf TextBlock is closer to a winforms label than the wpf label and you should usually use TextBlock in wpf for read only text. The wpf label has a content property because it's sort of weird from a winforms viewpoint and allows you to place ui inside it. So EG you could have a button set as content.

    Anyhow, TextBlock has a text property.

    .

    If you're learning wpf I suggest you consider learning MVVM as early as possible.

    It is the pattern that almost everyone uses for WPF ( and any XAML ) development.

    One plus of doing this is that you are less likely to use winforms code in your wpf app.


    Hope that helps.

    Technet articles: WPF: MVVM Step 1; All my Technet Articles

    • Proposed as answer by Christopher84 Friday, October 16, 2015 12:05 PM
    • Marked as answer by Kristin Xie Monday, October 26, 2015 2:01 AM
    Friday, October 16, 2015 11:13 AM
  • when working in WPF Application, you should keep in mind some properties used in WPF are different from Win form. so that's why you getting an error, plz review the sabah's reply.
    private void updateDisplay()
    {
        if (radWords.IsChecked)
           lblResult.Content = countWords(txtWords.Text) + " Word(s)";
        else
           lblResult.Content = countCharacters(txtWords.Text) + " Character(s)";
    }

    Friday, October 16, 2015 11:22 AM