2016-07-22 7 views
0

私はまだC#とプログラミング一般には比較的新しいので、私はこの質問を正しく聞くことさえできるかどうかはわかりませんが、ここに行きます。私は現在、Visual Studioを使用してC#のクラスをとり、教師が提供したWPFアプリケーションを使用してアプリケーションを正しく実行できるようにコードを入力する.csファイルを作成します。私はコンソールアプリケーションの作成に成功しているだけでなく、VBを使用するWPFアプリケーションも作成しましたが、これら2つのコンセプトを互いにどのように連携させるかは完全にはわかりません。.csを使用したWPFアプリケーション

次のようにこれまでのところ私のコードが見えます:

MainWindow.xaml(インストラクターが提供):

<Window x:Class="CreateClassesObjs.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:CreateClassesObjs" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> 
<Grid> 
    <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="66,37,0,0" VerticalAlignment="Top" Width="164" IsDropDownOpen="True"/> 
    <Button x:Name="button" Content="Select this course" HorizontalAlignment="Left" Margin="283,39,0,0" VerticalAlignment="Top" Width="166" Click="button_Click"/> 
    <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="69" Margin="66,233,0,0" VerticalAlignment="Top" Width="164"/> 
    <Label x:Name="label" Content="Please select a course " HorizontalAlignment="Left" Margin="66,7,0,0" VerticalAlignment="Top" Width="383"/> 
    <Label x:Name="label1" Content="You have selected these courses:" HorizontalAlignment="Left" Margin="66,202,0,0" VerticalAlignment="Top" Width="176"/> 

</Grid> 

MainWindow.xaml.cs(インストラクターが提供):

using System; 
using System.Collections.Generic; 
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.Navigation; 
using System.Windows.Shapes; 

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

    Course choice; 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     Course course1 = new Course(); 
     Course course2 = new Course(); 
     Course course3 = new Course(); 
     Course course4 = new Course(); 
     Course course5 = new Course(); 
     Course course6 = new Course(); 
     Course course7 = new Course(); 

     course1.setName("IT 145"); 
     course2.setName("IT 200"); 
     course3.setName("IT 201"); 
     course4.setName("IT 270"); 
     course5.setName("IT 315"); 
     course6.setName("IT 328"); 
     course7.setName("IT 330"); 



     this.comboBox.Items.Add(course1); 
     this.comboBox.Items.Add(course2); 
     this.comboBox.Items.Add(course3); 
     this.comboBox.Items.Add(course4); 
     this.comboBox.Items.Add(course5); 
     this.comboBox.Items.Add(course6); 
     this.comboBox.Items.Add(course7); 
    } 

    private void button_Click(object sender, RoutedEventArgs e) 
    { 
     choice = (Course)(this.comboBox.SelectedItem); 
     this.listBox.Items.Add(choice); 
    } 

} 
} 

Course.cs(私が作業を開始したコード):

#region Using directives 
using System; 
using System.Collections.Generic; 
using System.Text; 
#endregion 
namespace CreateClassesObjs 
{ 
public partial class Course :MainWindow 
{ 
    //Field 
    private string courseName; 

    //Method to set courseName to string value 
    public void setName(string newName) 
    { 
     courseName = newName; 
    } 

    //overrides string ToString 
    /*public override string ToString() 
    { 
     // this method returns the name field 
     Course course1 = new Course(); 
     Console.WriteLine(course1.courseName); 

    }*/ 
} 
} 

私はこのチュートリアルから何かをまとめようとしてきましたが、ちょっと混乱しているような気がします。私は完全な答えを探しているわけではありませんが、正しい方向に微妙なものを探しています。前もって感謝します!

+3

ご質問はありますか? – SLaks

+2

'Course'が' MainWindow'を継承するのはなぜですか? – SLaks

+1

私はこのコースが誰にもWPFを教えることを意図していないことを願っています...インストラクターのコードはちょうど私が内部で死ぬようにしました。 – Guttsy

答えて

0

これは本当に機能しています。 ToStringメソッドを変更するだけです。私はあなたのコースオブジェクトの新しいは.csファイルを作成する代わりにお勧めしたい、あなたがコンパイルすることはできませんので、あなたがこれを見ていない場合は

example

:私はあなたが現在、このような何かを見ている推測していますそれをあなたのMainWindowクラスに入れました。ここにはの理由はありません。クラスのがここにあります。

現在、WPFはコンボボックスにコースを表示する方法を知らない。 通常の WPFアプリケーションでは、ComboBoxにコースを提示する方法を伝えるDataTemplateを定義します。あなたのインストラクターが作成した嫌悪感のおかげで、私たちはその贅沢はありません。代わりに、それを和らげるためにあなたのToStringメソッドで何かを返す必要があります。私はそれが何であるかを理解させます。

(ご注意:。。これも、入門レベルでは、WPFをしない方法の典型的な例ではありませんあなたのせいでは任意の手段によって)

0

私はこのことができますが、ここで私はできたかであると思いますプログラムを機能させる。ゲッターとセッターが定義され、新しいコースが必要です。

using System; 
using System.Collections.Generic; 
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.Navigation; 
using System.Windows.Shapes; 

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

     Course choice; 

     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void Window_Loaded(object sender, RoutedEventArgs e) 
     { 
      Course course1 = new Course("IT 145"); 
      Course course2 = new Course("IT 200"); 
      Course course3 = new Course("IT 201"); 
      Course course4 = new Course("IT 270"); 
      Course course5 = new Course("IT 315"); 
      Course course6 = new Course("IT 328"); 
      Course course7 = new Course("IT 330"); 

      course1.setName("IT 145"); 
      course2.setName("IT 200"); 
      course3.setName("IT 201"); 
      course4.setName("IT 270"); 
      course5.setName("IT 315"); 
      course6.setName("IT 328"); 
      course7.setName("IT 330"); 

      this.comboBox.Items.Add(course1); 
      this.comboBox.Items.Add(course2); 
      this.comboBox.Items.Add(course3); 
      this.comboBox.Items.Add(course4); 
      this.comboBox.Items.Add(course5); 
      this.comboBox.Items.Add(course6); 
      this.comboBox.Items.Add(course7); 
     } 

     private void button_Click(object sender, RoutedEventArgs e) 
     { 
      choice = (Course)(this.comboBox.SelectedItem); 
      this.listBox.Items.Add(choice); 
     } 

     class Course 
     { 
      private string name = ""; 


      public Course(string name) 
      { 
       this.name = name; 
      } 

      public void setName(string name) 
      { 
       this.name = name; 
      } 

      public string getName() 
      { 
       return name; 
      } 

      public override string ToString() 
      { 
       return getName(); 
      } 
     } 
    } 
} 
関連する問題