其他私人方法中的按钮单击结果重用 [英] Button Click outcome reuse in other private methode

查看:51
本文介绍了其他私人方法中的按钮单击结果重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在下面使用此代码将文件转换为Base64.这段代码

Hi,

I use this code below for converting files into Base64. This piece of code

textBoxDescribeIncident.Text = FileToBase64String(openDlg.FileName);

将文件转换为Base64.我如何在另一个私有的void button_Click中使用此结果?

在此先感谢!

convert files into Base64. How can I use this outcome in an other private void button_Click ???

Thanks in advance!!

private void buttonBrowse_Click(object sender, EventArgs e) {
    OpenFileDialog openDlg = new OpenFileDialog();
    openDlg.Filter = "All Supported Files (All Files (*.*)|*.*";
    if (openDlg.ShowDialog(this) == DialogResult.OK) {
        Cursor = Cursors.WaitCursor;
        textBoxAttachFile.Text = openDlg.FileName;
        try {
            textBoxDescribeIncident.Text = FileToBase64String(openDlg.FileName);
        } catch (Exception) {
            MessageBox.Show(this, string.Format("An error occurred whilst converting to base-64."),
                Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        Cursor = Cursors.Default;
    }
}





private void buttonCreate_Click(object sender, EventArgs e) {
     
     1. First I select a files, this file will directly converted to Base64
     2. I fillin all my other fields
     3. When I click the button Create, I need the Base64 hashcode in this private methode

}

推荐答案

当您按下浏览"按钮时,您将创建Base64字符串,并将其保存到(根据名称)文本框中:
When you press your "Browse" button, you create the Base64 string, and save it to what is (from the name) a text box:
private void buttonBrowse_Click(object sender, EventArgs e) {
    OpenFileDialog openDlg = new OpenFileDialog();
    openDlg.Filter = "All Supported Files (All Files (*.*)|*.*";
    if (openDlg.ShowDialog(this) == DialogResult.OK) {
        Cursor = Cursors.WaitCursor;
        textBoxAttachFile.Text = openDlg.FileName;
        try {
            textBoxDescribeIncident.Text = FileToBase64String(openDlg.FileName); //<<<---HERE!
        } catch (Exception) {
            MessageBox.Show(this, string.Format("An error occurred whilst converting to base-64."),
                Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        Cursor = Cursors.Default;
    }
}

您可以通过其他按钮(只要它们具有相同的格式)进行访问:

You can access that from your other buttons, provided they are on the same form:

private void buttonCreate_Click(object sender, EventArgs e) 
   {
   if (!string.IsNullOrEmpty(textBoxDescribeIncident.Text))
      {
      MessageBox.Show("Here it is: " + textBoxDescribeIncident.Text);
      }
   }

如果您不想在文本框中显示信息,请声明一个私有字符串,然后将其放在那里.
如果您需要以其他形式使用它,那是另一回事-易于解决,但又有所不同!

If you don''t want to display the info in a text box, then declare a private string and put it there instead.
If you need it on a different form, then that''s another matter - easy to solve, but different!


您已经在textBoxDescribeIncident.Text上进行了Base64转换,所以为什么不这样做呢?在您的buttonCreate_Click上使用该值.

如果这两个按钮位于不同的类上,则让我们说Class1上的buttonBrowse_Click和Class2上的buttonCreate_Click.您可以在Class2上添加一个公共属性,该属性将由Class1上的buttonBrowse_Click设置.
You have your Base64 conversion on your textBoxDescribeIncident.Text already, so why not use that value on your buttonCreate_Click.

If the two buttons were on different class, lets say buttonBrowse_Click on Class1 and buttonCreate_Click on Class2. You can add a public property on your Class2 that will be set by buttonBrowse_Click on Class1.


这篇关于其他私人方法中的按钮单击结果重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆