gpt4 book ai didi

C# - OOP - 我走在正确的轨道上吗?

转载 作者:行者123 更新时间:2023-11-30 22:42:46 25 4
gpt4 key购买 nike

第一次发帖,长期阅读。

我目前正在通过“Head First C#”学习 C#(我正在学习封装和获取/设置属性)

我正在为 friend 编写一个处理图片的小程序,我只是想知道我的 PictureController 类是否正在沿着正确的方向前进?我的主要问题是我在这个类中设置了很多表单项,并且在类中继续引用表单项感觉很不自然,我在下面粘贴我的代码,如果你可以让我知道我在做什么如果有任何问题,我将不胜感激 :)

非常感谢!

PictureController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace PictureController
{
class PictureController
{

private int arrayPosition = 0;
private int numFiles = 0;

private string[,] arrayPictures;

public PictureBox myPictureBox;
public RadioButton myCopyButton;
public RadioButton myDeleteButton;
public TextBox mySource;
public ComboBox myDestinations;

private FolderBrowserDialog sourceFolder;
private FolderBrowserDialog destFolder;

public void InitialisePicture()
{

if (arrayPictures != null && arrayPictures.Length > 0)
{
myPictureBox.ImageLocation = arrayPictures[arrayPosition, 0];
}
else
{
MessageBox.Show("The folder you have selected contains no pictures...");
myPictureBox.ImageLocation = null;
}
}

public void NavigatePicture(int direction)
{

if (arrayPosition + direction >= 0 && arrayPosition + direction < numFiles)
{
arrayPosition += direction;
myPictureBox.ImageLocation = arrayPictures[arrayPosition, 0];


myCopyButton.Checked = Convert.ToBoolean(arrayPictures[arrayPosition, 1]);
myDeleteButton.Checked = Convert.ToBoolean(arrayPictures[arrayPosition, 2]);

}
}

public void UpdateActions(bool copyChecked, bool deleteChecked)
{
if (arrayPictures != null)
{
arrayPictures[arrayPosition, 1] = copyChecked.ToString();
arrayPictures[arrayPosition, 2] = deleteChecked.ToString();
}

}


public void GetFiles()
{

sourceFolder = new FolderBrowserDialog();
sourceFolder.ShowDialog();

if (sourceFolder.SelectedPath != "")
{
string[] arrayTempFiles = Directory.GetFiles(sourceFolder.SelectedPath,"*.jpg");
numFiles = arrayTempFiles.Length;

arrayPictures = new string[arrayTempFiles.Length,3];

for (int i = 0; i < arrayTempFiles.Length; i++)
{
arrayPictures[i, 0] = arrayTempFiles[i];
arrayPictures[i, 1] = "false";
arrayPictures[i, 2] = "false";
}

mySource.Text = sourceFolder.SelectedPath;

InitialisePicture();
}

}

public void AddDestinationFolder()
{
destFolder = new FolderBrowserDialog();
destFolder.ShowDialog();

if (destFolder.SelectedPath != "")
{
myDestinations.Items.Add(destFolder.SelectedPath);
}

}





}
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PictureController
{

public partial class Form1 : Form
{

PictureController PicControl;

public Form1()
{
InitializeComponent();

PicControl = new PictureController() { myPictureBox = pbPhoto, myCopyButton = rbMove, myDeleteButton = rbDelete, mySource = tbSource, myDestinations = cbDestination };
}

private void btnPrev_Click(object sender, EventArgs e)
{
PicControl.NavigatePicture(-1);
}

private void btnNext_Click(object sender, EventArgs e)
{
PicControl.NavigatePicture(1);
}

private void rbMove_CheckedChanged(object sender, EventArgs e)
{
if (rbMove.Checked || rbDelete.Checked)
{
PicControl.UpdateActions(rbMove.Checked, rbDelete.Checked);
}
}

private void rbDelete_CheckedChanged(object sender, EventArgs e)
{
if (rbMove.Checked || rbDelete.Checked)
{
PicControl.UpdateActions(rbMove.Checked, rbDelete.Checked);
}
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.A:
PicControl.NavigatePicture(-1);
break;
case Keys.D:
PicControl.NavigatePicture(1);
break;
case Keys.W:
rbMove.Checked = true;
break;
case Keys.S:
rbDelete.Checked = true;
break;
}


}

private void btnGetFiles_Click(object sender, EventArgs e)
{
PicControl.GetFiles();
}

private void btnProcess_Click(object sender, EventArgs e)
{


}

private void btnAddDest_Click(object sender, EventArgs e)
{
PicControl.AddDestinationFolder();
}

}
}

最佳答案

我看不出在您的 PictureController 类中使用控件的原因。您应该只在那里使用非表单数据类型并处理您的 Form 中的交互,提供来自您的 PictureController 类的事件和方法以对其使用react和操作。

关于C# - OOP - 我走在正确的轨道上吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4251110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com