开发者

Using data from one class in another

开发者 https://www.devze.com 2023-02-16 11:25 出处:网络
I tried to find answer to this question, but no luck, so, if a开发者_C百科nyone knows, thanks. Ok, i have two classes, in one class there are some radio buttons that could be checked, and in another c

I tried to find answer to this question, but no luck, so, if a开发者_C百科nyone knows, thanks. Ok, i have two classes, in one class there are some radio buttons that could be checked, and in another class i want a information that radio buttons state is changed, here is the part of the code of second class:

    static void client_Download(object sender, DownloadStringCompletedEventArgs e)
            {
                MainPage mng = new MainPage();//(i tried this, but no luck, is this ok?)

                if (e.Error != null)
                {

                    if (mng.radioButton1.IsChecked == true)
                    {
                        MessageBox.Show("Message1");
                        return;
                    }

                    else
                        if (mng.radioButton2.IsChecked == true) ;
                    {
                        MessageBox.Show("Message2");
                        return;
                    }
                    else
                        if (mng.radioButton3.IsChecked == true) ;
                    {
                        MessageBox.Show("Message3");
                        return;
                    }

                } 

.............

in first class there is only some method where this radio buttons are changed and theirs conditions, here is the code, this is in MainPage:

private void button1_Click(object sender, RoutedEventArgs e)
        {

            if (radioButton1.IsChecked == true)
            {
                MessageBox.Show(RSS_citac.JezikEng.Message);
                ApplicationTitle.Text = RSS_citac.JezikEng.ApplicationTitle;
                PageTitle.Text = RSS_citac.JezikEng.PageTitle;
                textBlock1.Text = RSS_citac.JezikEng.textblock;
                button1.Content = RSS_citac.JezikEng.buttonOk;
                button2.Content = RSS_citac.JezikEng.buttonCancel;
                radioButton1.Content = RSS_citac.JezikEng.radioButton1;
                radioButton2.Content = RSS_citac.JezikEng.radioButton2;

                reloadFeeds.Text = RSS_citac.JezikEng.reloadFeeds;
                manageFeeds.Text = RSS_citac.JezikEng.manageFeeds;
                changeLang.Text = RSS_citac.JezikEng.changeLanguage;

                buttonRead.Content = RSS_citac.JezikEng.buttonRead;
                buttonCancel.Content = RSS_citac.JezikEng.buttonCancelNaUnosu;


                stackPanel1.Visibility = Visibility.Collapsed;
                border1.Visibility = Visibility.Collapsed;
                grid1.Visibility = Visibility.Collapsed;

            }
            else
                if (radioButton2.IsChecked == true)
                {


                    MessageBox.Show(RSS_citac.JezikSrb.Message);
                    ApplicationTitle.Text = RSS_citac.JezikSrb.ApplicationTitle;
                    PageTitle.Text = RSS_citac.JezikSrb.PageTitle;
                    textBlock1.Text = RSS_citac.JezikSrb.textblock;
                    button1.Content = RSS_citac.JezikSrb.buttonOk;
                    button2.Content = RSS_citac.JezikSrb.buttonCancel;
                    radioButton1.Content = RSS_citac.JezikSrb.radioButton1;
                    radioButton2.Content = RSS_citac.JezikSrb.radioButton2;
                    radioButton3.Content = RSS_citac.JezikSrb.radioButton3;


                    reloadFeeds.Text = RSS_citac.JezikSrb.reloadFeeds;
                    manageFeeds.Text = RSS_citac.JezikSrb.manageFeeds;
                    changeLang.Text = RSS_citac.JezikSrb.changeLanguage;

                    buttonRead.Content = RSS_citac.JezikSrb.buttonRead;
                    buttonCancel.Content = RSS_citac.JezikSrb.buttonCancelNaUnosu;

                    stackPanel1.Visibility = Visibility.Collapsed;
                    border1.Visibility = Visibility.Collapsed;
                    grid1.Visibility = Visibility.Collapsed;
                 }
                else
                    if (radioButton3.IsChecked == true)
                    {

                        MessageBox.Show(RSS_citac.JezikGer.Message);
                        ApplicationTitle.Text = RSS_citac.JezikGer.ApplicationTitle;
                        PageTitle.Text = RSS_citac.JezikGer.PageTitle;
                        textBlock1.Text = RSS_citac.JezikGer.textblock;
                        button1.Content = RSS_citac.JezikGer.buttonOk;
                        button2.Content = RSS_citac.JezikGer.buttonCancel;
                        radioButton1.Content = RSS_citac.JezikGer.radioButton1;
                        radioButton2.Content = RSS_citac.JezikGer.radioButton2;
                        radioButton3.Content = RSS_citac.JezikGer.radioButton3;

                        reloadFeeds.Text = RSS_citac.JezikGer.reloadFeeds;
                        manageFeeds.Text = RSS_citac.JezikGer.manageFeeds;
                        changeLang.Text = RSS_citac.JezikGer.changeLanguage;

                        buttonRead.Content = RSS_citac.JezikGer.buttonRead;
                        buttonCancel.Content = RSS_citac.JezikGer.buttonCancelNaUnosu;

                        stackPanel1.Visibility = Visibility.Collapsed;
                        border1.Visibility = Visibility.Collapsed;
                        grid1.Visibility = Visibility.Collapsed;
                    }

            }


I suppose, you can use events. Let the class with radiobuttons to fire an event when check-state is changed. The second class, that should know about it, can subscribe to this events.

Or is there some difficulty about it?


In the main() function of your application (which will be in some static class) you will see a line of code that says

Application.Run(new Main());

This line of code instantiates (creates) an instance of the main form and then runs it. In your second class you need to have a reference to this main form in order to access any data (Anything actually) that has been stored in that instance of the form class. You can create and use a reference to this main form by changing thecode above to:

var myMainForm = new Main(); Application.Run(myMainForm);

You can pass thsi reference to the second form in many ways. You could make this variable a static variable in whatever the static class is that the main function is in. Then, (it that static class is named static class Program, you can access it from anywhere in your application by typing: `Program.myMainForm '

Another way is: Somewhere in the main form, there is probably a place where you are instantiating the second class. In that place you can pass a reference to the first form to the second form by passing it in the second forms constructor, or you can add a property on the second form that can be passed the reference to the first form. In both cases, since the code would be executing inside the MainForm itself, you would simply pass the this variable to the second form (either in the constructor or to the property.)

In any case, once you have a reference to the first form that is accessible on the second one, you need to have some public property on the first form that can be called from the second form, (through the reference) that will return the data you want. In your example, on the first form, just add a property like this

public bool radioButtonChecked  { get { return radiobutton1.Checked; } } 

You can call this property using the reference to the first form, and it will return a boolean indicatiung wheter the radio button is currently checked

0

精彩评论

暂无评论...
验证码 换一张
取 消