我还是新手,在我去的时候学习C#。我的问题是通用的,所以如果我能以任何方式更具体,请告诉我。此外,我还在寻找指导/示例,甚至是一个简单的解决方案来构建。
我有一个当前正在运行的类库,但我想向用户显示一个窗口。我的课程是一个名为Solidworks PDM的程序,是一个菜单命令。在中间,我需要用户查看并选择" OK"或"取消"。
基本上,我如何获得一个包含两个按钮的窗口" OK"和"取消"在我的班级图书馆?提前谢谢!
using System;
using System.IO;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using EdmLib;
using Microsoft.VisualBasic;
using System.Windows.Forms;
namespace ClassLibrary2
{
public class Class1 : IEdmAddIn5
{
public void GetAddInInfo(ref EdmAddInInfo poInfo, IEdmVault5 poVault, IEdmCmdMgr5 poCmdMgr)
{
//Specify information to display in the add-in's Properties dialog box
poInfo.mbsAddInName = "Menu command sample";
poInfo.mbsCompany = "SOLIDWORKS Corporation";
poInfo.mbsDescription = "Adds menu command items";
poInfo.mlAddInVersion = 1;
//Specify the minimum required version of SOLIDWORKS Enterprise PDM
poInfo.mlRequiredVersionMajor = 10;
poInfo.mlRequiredVersionMinor = 0;
//Register a menu command
poCmdMgr.AddCmd(100, "Inspection Request (New C#)", (int)EdmMenuFlags.EdmMenu_OnlySingleSelection);
}
public void OnCmd(ref EdmCmd poCmd, ref Array ppoData)
{
if (poCmd.meCmdType == EdmCmdType.EdmCmd_Menu) //This makes sure the addin was called on a menu right click
{
if (poCmd.mlCmdID == 100) //This makes sure the menu command ID was 100, which means your addin was the one called
{
//Let's get moving, it's our turn!
IEdmVault12 myVault = (IEdmVault12)poCmd.mpoVault; //Cast the vault to an Iedmvault12 object for latest functionality
string Mymessage = null;//instantiate a string message
//string StrID = null;//instantiate the ID of the object
if ((((EdmCmdData)ppoData.GetValue(0)).mlObjectID1) != 0) //mlObjectID1 will be 0 if a folder was selected and the ID of the file if a file was selected during a menu command
{
//Mymessage = Mymessage + "File: (ID=";
IEdmFile5 myfile = (IEdmFile10)myVault.GetObject(EdmObjectType.EdmObject_File, (((EdmCmdData)ppoData.GetValue(0)).mlObjectID1));//Get the file object
string fileNoExt = Path.GetFileNameWithoutExtension(myfile.Name);
IEdmEnumeratorVariable10 myvars = default(IEdmEnumeratorVariable10); //Set the enumeratorvariable object
myvars = (IEdmEnumeratorVariable10)myfile.GetEnumeratorVariable(); //Cast the enumeratorvariable object to the file
object CreatedBy = null; //Create a variable to hold the model no.
object Desc1 = null;
object Desc2 = null;
object Rev = null;
myvars.GetVar("CreatedBy", "@", out CreatedBy); //Use the GetVar method
myvars.GetVar("CAD_Description", "@", out Desc1);
myvars.GetVar("CAD_Description1", "@", out Desc2);
myvars.GetVar("CAD_Revision", "@", out Rev);
myvars.CloseFile(false); //Always close the file when done accessing card variables to avoid runtime errors
//Mymessage = Mymessage + "File ID: " + myfile.ID + System.Environment.NewLine; //Create a message with the File ID
Mymessage = Mymessage + "CreatedBy: " + CreatedBy + System.Environment.NewLine; //Add the model no. variable
Mymessage = Mymessage + "Description: " + Desc1 + System.Environment.NewLine;
Mymessage = Mymessage + "Description1: " + Desc2 + System.Environment.NewLine;
Mymessage = Mymessage + "Revision: " + Rev + System.Environment.NewLine;
MessageBox.Show(Mymessage); //Show the message box!
MessageBox.Show("Get template path");
string templPath = @"C:\SeanVault\Templates\template.item.cvd";
string newPath = @"C:\SeanVault\Inspection Files";
string filename = fileNoExt + " Rev " + Rev.ToString() + ".insp";
IEdmFile5 templateFile = default(IEdmFile5);
IEdmFolder5 srcFolder = null;
templateFile = myVault.GetFileFromPath(templPath, out srcFolder);
MessageBox.Show("Get destination path");
IEdmFolder5 destFolder = default(IEdmFolder5);
destFolder = myVault.GetFolderFromPath(newPath);
if (destFolder == null)
return;
int fileID = 0;
fileID = destFolder.CopyFile(templateFile.ID, srcFolder.ID, 0, filename, (int)EdmCopyFlag.EdmCpy_Simple);
Interaction.MsgBox("Copied file successfully to new file with ID, " + fileID);
IEdmFile5 newFile = default(IEdmFile5);
IEdmFile5 itemFile = default(IEdmFile5);
newPath = newPath + "\\" + filename;
//Search for file with .item extension
IEdmSearch5 Search = default(IEdmSearch5);
Search = (IEdmSearch5)myVault.CreateUtility(EdmUtility.EdmUtil_Search);
Search.FindFiles = true;
//Search.StartFolderID = 102; //Regular Items ID
Search.FileName = fileNoExt + ".item";
IEdmSearchResult5 Result = default(IEdmSearchResult5);
Result = Search.GetFirstResult();
object StockingType = null; //Create a variable to hold the model no.
if (Result != null)
{
MessageBox.Show("Result exists. Begin! Path = " + Result.Path);
itemFile = myVault.GetFileFromPath(Result.Path, out srcFolder);
IEdmEnumeratorVariable10 itemVars = default(IEdmEnumeratorVariable10); //Set the enumeratorvariable object
itemVars = (IEdmEnumeratorVariable10)itemFile.GetEnumeratorVariable(); //Cast the enumeratorvariable object to the file
//breaks
itemVars.GetVar("ITEM_StockingType", "", out StockingType); //Use the GetVar method
//itemVars.GetVar("CAD_Description", "@", out Desc1);
//itemVars.GetVar("CAD_Description1", "@", out Desc2);
//itemVars.GetVar("CAD_Revision", "@", out Rev);
MessageBox.Show("5");
myvars.CloseFile(false); //Always close the file when done accessing card variables to avoid runtime errors
//Mymessage = Mymessage + "File ID: " + myfile.ID + System.Environment.NewLine; //Create a message with the File ID
}
newFile = myVault.GetFileFromPath(newPath, out srcFolder);
//update variables
IEdmEnumeratorVariable10 myNewVars = default(IEdmEnumeratorVariable10); //Set the enumeratorvariable object
myNewVars = (IEdmEnumeratorVariable10)newFile.GetEnumeratorVariable(); //Cast the enumeratorvariable object to the file
MessageBox.Show(Rev.ToString() + "," + StockingType.ToString());
myNewVars.SetVar("ITEM_RevisionNumber", "", Rev.ToString());
myNewVars.SetVar("ITEM_DescriptionLine1", "", Desc1.ToString());
myNewVars.SetVar("ITEM_DescriptionLine2", "", Desc2.ToString());
if (StockingType.ToString() != null)
{
myNewVars.SetVar("ITEM_StockingType", "", StockingType.ToString());
}
myvars.CloseFile(false); //Always close the file when done accessing card variables to avoid runtime errors
//Mymessage = Mymessage + "File ID: " + myfile.ID + System.Environment.NewLine; //Create a message with the File ID
//Check-in new file
newFile.UnlockFile(0,"Created and checked-in with Sean's API.");
//myvars.SetVar("CAD_Weight","@", "This Worked");
//Mymessage = Mymessage + "File name: " + ((EdmCmdData)ppoData.GetValue(0)).mbsStrData1 + System.Environment.NewLine; //Add the file name
}
}
}
}
}
}
答案 0 :(得分:0)
您必须创建一个具有输入选项的表单,供用户进行交互以获取所需的文件路径。根据你给出的代码,我继续编写我认为你需要的代码。它应该与您实际使用的非常接近,但以此为基础,您应该能够非常轻松地编辑它以满足您的需求。
带有文件选择器和文件夹选择器的表单,带有确定和取消按钮......
public partial class RequestInfo : Form
{
private Action<string, string> _sendValues = null;
public RequestInfo(Action<string, string> retrieveValues)
{
InitializeComponent();
_sendValues = retrieveValues;
}
private void fileBrowseButton_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
filePath.Text = openFileDialog.FileName;
}
private void folderBrowseButton_Click(object sender, EventArgs e)
{
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
folderPath.Text = folderBrowserDialog.SelectedPath;
}
private void okButton_Click(object sender, EventArgs e)
{
_sendValues(filePath.Text, folderPath.Text);
this.DialogResult = DialogResult.OK;
this.Close();
}
private void cancelButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}
使用RequestInfo表单的示例...
public static partial class Program
{
private static string _filePath = "";
private static string _folderPath = "";
[STAThread]
static void Main(string[] args)
{
RequestInfo infoForm = new RequestInfo(assignValues);
DialogResult result = infoForm.ShowDialog();
if (result.Equals(DialogResult.OK))
{
string templPath = _filePath;
string newPath = _folderPath;
}
Console.ReadKey(true);
}
private static void assignValues(string filePath, string folderPath)
{
_filePath = filePath;
_folderPath = folderPath;
}
}
RequestInfo Designer代码...... 如果您是C#的新手,可能对此不熟悉,只需为RequestInfo创建一个Designer文件并粘贴它。
partial class RequestInfo
{
/// <summary>
/// Designer variable used to keep track of non-visual components.
/// </summary>
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.OpenFileDialog openFileDialog;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog;
private System.Windows.Forms.Label filePath;
private System.Windows.Forms.Button fileBrowseButton;
private System.Windows.Forms.Button folderBrowseButton;
private System.Windows.Forms.Label folderPath;
private System.Windows.Forms.Button cancelButton;
private System.Windows.Forms.Button okButton;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel2;
/// <summary>
/// Disposes resources used by the form.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent()
{
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
this.filePath = new System.Windows.Forms.Label();
this.fileBrowseButton = new System.Windows.Forms.Button();
this.folderBrowseButton = new System.Windows.Forms.Button();
this.folderPath = new System.Windows.Forms.Label();
this.cancelButton = new System.Windows.Forms.Button();
this.okButton = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.panel2 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// openFileDialog
//
this.openFileDialog.FileName = "openFileDialog";
this.openFileDialog.Filter = "CVD Files|*.cvd";
//
// filePath
//
this.filePath.AutoSize = true;
this.filePath.Location = new System.Drawing.Point(93, 17);
this.filePath.Name = "filePath";
this.filePath.Size = new System.Drawing.Size(85, 13);
this.filePath.TabIndex = 0;
this.filePath.Text = "No File Selected";
this.filePath.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// fileBrowseButton
//
this.fileBrowseButton.Location = new System.Drawing.Point(12, 12);
this.fileBrowseButton.Name = "fileBrowseButton";
this.fileBrowseButton.Size = new System.Drawing.Size(75, 23);
this.fileBrowseButton.TabIndex = 1;
this.fileBrowseButton.Text = "Browse";
this.fileBrowseButton.UseVisualStyleBackColor = true;
this.fileBrowseButton.Click += new System.EventHandler(this.fileBrowseButton_Click);
//
// folderBrowseButton
//
this.folderBrowseButton.Location = new System.Drawing.Point(12, 51);
this.folderBrowseButton.Name = "folderBrowseButton";
this.folderBrowseButton.Size = new System.Drawing.Size(75, 23);
this.folderBrowseButton.TabIndex = 2;
this.folderBrowseButton.Text = "Browse";
this.folderBrowseButton.UseVisualStyleBackColor = true;
this.folderBrowseButton.Click += new System.EventHandler(this.folderBrowseButton_Click);
//
// folderPath
//
this.folderPath.AutoSize = true;
this.folderPath.Location = new System.Drawing.Point(93, 56);
this.folderPath.Name = "folderPath";
this.folderPath.Size = new System.Drawing.Size(98, 13);
this.folderPath.TabIndex = 3;
this.folderPath.Text = "No Folder Selected";
this.folderPath.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// cancelButton
//
this.cancelButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.cancelButton.Location = new System.Drawing.Point(546, 10);
this.cancelButton.Margin = new System.Windows.Forms.Padding(0);
this.cancelButton.Name = "cancelButton";
this.cancelButton.Size = new System.Drawing.Size(75, 23);
this.cancelButton.TabIndex = 4;
this.cancelButton.Text = "Cancel";
this.cancelButton.UseVisualStyleBackColor = true;
this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
//
// okButton
//
this.okButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.okButton.Location = new System.Drawing.Point(465, 10);
this.okButton.Margin = new System.Windows.Forms.Padding(0);
this.okButton.Name = "okButton";
this.okButton.Size = new System.Drawing.Size(75, 23);
this.okButton.TabIndex = 5;
this.okButton.Text = "OK";
this.okButton.UseVisualStyleBackColor = true;
this.okButton.Click += new System.EventHandler(this.okButton_Click);
//
// panel1
//
this.panel1.AutoSize = true;
this.panel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.panel1.Controls.Add(this.okButton);
this.panel1.Controls.Add(this.cancelButton);
this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel1.Location = new System.Drawing.Point(0, 298);
this.panel1.Name = "panel1";
this.panel1.Padding = new System.Windows.Forms.Padding(0, 10, 0, 10);
this.panel1.Size = new System.Drawing.Size(633, 43);
this.panel1.TabIndex = 6;
//
// panel2
//
this.panel2.AutoSize = true;
this.panel2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.panel2.Controls.Add(this.fileBrowseButton);
this.panel2.Controls.Add(this.folderBrowseButton);
this.panel2.Controls.Add(this.folderPath);
this.panel2.Controls.Add(this.filePath);
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point(0, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(633, 298);
this.panel2.TabIndex = 7;
//
// RequestInfo
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.ClientSize = new System.Drawing.Size(633, 341);
this.ControlBox = false;
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(300, 200);
this.Name = "RequestInfo";
this.Text = "RequestInfo";
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
}
答案 1 :(得分:0)
我发现这个link给了我类库代码中的对话。我会尝试在此添加我需要的内容。
如果有人对此有任何意见,请告诉我。再次感谢您的帮助。