从子窗体获取值

时间:2019-06-09 11:46:08

标签: c# parameter-passing visual-studio-2019 subform

我知道这已经被问了无数次了,但是我无法从返回主窗体的子窗体中获取值。我是C#的新手,请耐心等待。

我也在此站点上尝试了许多解决方案,但是没有一个对我有用。当我使用给定的解决方案时,通常会给出错误信息。可能是因为我使用VS 2019,所以大多数解决方案都适用于以前的版本,或者可能是因为我不知道对于顽固分子来说很明显的事情(因此没有提及)。

主窗口的定义部分:

Base::func

主窗口的编码部分:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace mJingles
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>


    public partial class MainWindow : Window
    {
        public MainWindow()
        {

子窗体的主要部分:

private void Button_1_PL_Click(object sender, RoutedEventArgs e)
{
    Playlists pl = new Playlists();
    pl.WindowStartupLocation = WindowStartupLocation.Manual;
    pl.Left = 0;
    pl.Top = 110;
    pl.ShowDialog();

    Playlists plw = new Playlists();
    string plList = plw.GetPlSelected();
    string plColor = plw.GetColorSel();

    Console.WriteLine("Main Window Form");
    Console.WriteLine("Playlist 1");
    Console.WriteLine(plList);
    Console.WriteLine(plColor);
    Console.WriteLine("---------------");
    Console.WriteLine("");
}

最后是子表单的编码部分:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace mJingles
{
    /// <summary>
    /// Interaction logic for Playlists.xaml
    /// </summary>
    public partial class Playlists : Window

    {
        public string plSelected;
        public string ColorSel;

        public Playlists()
        {
            InitializeComponent();

输出窗口:

void Finished_Click(object sender, RoutedEventArgs e)
{
    if (plList.SelectedIndex != -1)
    {
        plSelected = plList.SelectedItem.ToString();
    }
    if (plColor.SelectedIndex != -1)
    {
        ColorSel = plColor.SelectedItem.ToString();
        int idx = ColorSel.LastIndexOf(":");
        ColorSel = ColorSel.Substring(idx + 2);
    }
    Console.WriteLine("Playlists form");
    Console.WriteLine(plSelected);
    Console.WriteLine(ColorSel);
    Console.WriteLine("---------------");

    this.Close();
}

我希望C#的老手可以在我的代码中找到问题,以便我可以将结果从子表单中重新获取到我的主表单中。

提前致谢,

Theo。

0 个答案:

没有答案