调用表单列出值。 Ribbon1.cs和Form1.cs

时间:2019-10-25 13:15:06

标签: c# forms winforms vsto ribbon

我正在开发VSTO应用程序。我在Office Ribbon1.cs 上有一个按钮。和 Form1.cs 上的表单。我从创建新表单的按钮中调用表单:

Form1 MallideVorm = new form Form1 (); // Create a form from Form1
MallideVorm.Show (); // Show the form

我的Windows窗体中有一个列表。我在其中放了弦:

foreach (Microsoft.SharePoint.Client.File file in fileCol)
{
str = file.Name;

// File.AppendAllText (@ "C: \ install \ CSharp \ tulemus.txt", $ "File name: {str}" + Environment.NewLine); // for testing

foreach (form Form1 in System.Windows.Forms.Application.OpenForms)
{
if (frm.GetType () == typeof (Form1))
{
// RibbonDropDownItem ddItem1 = Globals.Factory.GetRibbonFactory (). CreateRibbonDropDownItem ();
//ddItem1.Label = $ "{str}";

Form1 frmTemp = (Form1) frm;
//ddList.Items.Add(ddItem1);
MallideVorm.addItemToListBoxFiles (file.Name);

}
}
}

所有这些操作都是通过 Ribbon1.cs -s按钮完成的。当我有一个带有列表的表单时,我想在我的 Form1.cs 中:(返回选择的值)

private void FormFilesBox_SelectedIndexChanged (object sender, EventArgs e)
{string selectedFile = FormFilesBox.SelectedItem.ToString ();}

但这不起作用。我认为问题在于,我只需单击一个按钮即可生成一个 Form A ,但尝试从另一个 Form B 获取值。 如何从Form1.cs中的Ribbon1.cs定义操作,反之亦然(FormFilesBox_SelectedIndexChanged)。或者,我该如何引用相同的表格。

关于辛迪建议的答案: 谢谢辛迪,但我不知道该怎么做。 Word.interop具有自己的全局变量Link?view = word-pia 因此,如果我要制作一些public static class Globals {Form1 MallideVorm = new Form1(); },那么我将遇到类似

的错误:
  

严重性代码描述项目文件行抑制状态   错误CS0117'Ribbon1.Globals'不包含以下定义   工厂的“ ThisAddIn” 470活动或定义...

在表单代码旁边:我在Form1.cs设计中具有Windows表单。 在Form1.cs中是:

public void addItemToListBoxFolder(string item)
        {
            FormFolderBox.Items.Add(item);
        }

        public void addItemToListBoxFiles(string item)
        {
            FormFilesBox.Items.Add(item);
        }


        private void FormFilesBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //_______________________________________________________Sharepoindist faili alla laadimine ja sealt avamine________________________________________

            string selectedFile = FormFilesBox.SelectedItem.ToString();
            //Here is the question, how to take value from other Form.
          //In Ribbon1.cs it is declared as Form1 MallideVorm = new Form1();  //Creating form from Form1
     //   MallideVorm.Show(); 

            var destinationFolder = @"C:\install\CSharp\TestkaustDoc";
            ClientContext ctx = new ClientContext("https://Some/Some");
            //Configure the handler that will add the header.
            ctx.ExecutingWebRequest +=
            new EventHandler<WebRequestEventArgs>(ctx_MixedAuthRequest);
            //Set the Windows credentials.
            ctx.AuthenticationMode = ClientAuthenticationMode.Default;
            ctx.Credentials = System.Net.CredentialCache.DefaultCredentials;
            // Get a reference to the SharePoint site
            var web = ctx.Web;

            // Get a reference to the document library
            var list = ctx.Web.Lists.GetByTitle("Documents");


            // Get the list of files you want to export. I'm using a query
            // to find all files where the "Status" column is marked as "Approved"
            var camlQuery = new CamlQuery
            {
                ViewXml = @"<View>
                            <Query>
                                <Where>
                                <Eq>
                                    <FieldRef Name=""FileLeafRef"" />
                                    <Value Type=""Text"">" + $"{selectedFile}" + @"</Value>
                                </Eq>
                                </Where>
                                </Query>
                            </View>"
            };
            camlQuery.FolderServerRelativeUrl = "/Some/Shared Documents/Test";
            // Retrieve the items matching the query
            var items = list.GetItems(camlQuery);

            // Make sure to load the File in the context otherwise you won't go far
            ctx.Load(items, items2 => items2.IncludeWithDefaultProperties
                (item => item.DisplayName, item => item.File));

            // Execute the query and actually populate the results
            ctx.ExecuteQuery();

            // Iterate through every file returned and save them
            foreach (var item in items)
            {

                using (FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, item.File.ServerRelativeUrl))
                {
                    // Combine destination folder with filename -- don't concatenate
                    // it's just wrong!
                    var filePath = Path.Combine(destinationFolder, item.File.Name);

                    // Erase existing files, cause that's how I roll
                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.File.Delete(filePath);
                    }

                    // Create the file
                    using (var fileStream = System.IO.File.Create(filePath))
                    {
                        fileInfo.Stream.CopyTo(fileStream);
                    }
                }

            }

我使用以下方法解决了该问题:

foreach (Form1 frm in System.Windows.Forms.Application.OpenForms)

喜欢:

Ribbon.cs 中,我正在创建一个新表单:

Form1 MallideVorm = new Form1();  //Creating form from Form1

          MallideVorm.Show(); // Showing form

然后使用Application.OpenForms。 (因为表单是一个)

foreach (Form1 frm in System.Windows.Forms.Application.OpenForms)
                            {
                                if (frm.GetType() == typeof(Form1))
                                {


                                    Form1 frmTemp = (Form1)frm;

                                   frm.addItemToListBoxFiles(file.Name);

然后在 Form1.cs 中以相同的方式从列表框中获取值。

foreach (Form1 frm in System.Windows.Forms.Application.OpenForms)

            {
                selectedFile =  FormFilesBox.SelectedItem.ToString();

            }

0 个答案:

没有答案