iT邦幫忙

0

檔名是輸入textbox的值 c#

  • 分享至 

  • xImage

小弟有兩個form form2為主要執行程式 執行form2按確認後會執行form1會縮小化 我想要做在form2輸入後按確認後 可以產出檔案是 textbox值+日期.csv檔 但我日期.csv檔是寫在form1 想請問要怎樣把form2之textbox值call過去 並可以產出我要的檔案 目前可以產出日期.csv檔
再麻煩各位大神指導 真的非常感謝
form1:
https://ithelp.ithome.com.tw/upload/images/20220727/20150773CpcpYEBbZ2.jpg
form2:
https://ithelp.ithome.com.tw/upload/images/20220727/2015077333IN83v2lO.jpg
我想要的:
https://ithelp.ithome.com.tw/upload/images/20220727/201507730gBQLao1x2.jpg
並可以產出:
https://ithelp.ithome.com.tw/upload/images/20220727/20150773GXiX9uyMTy.jpg
但目前:
https://ithelp.ithome.com.tw/upload/images/20220727/20150773PkTvrfVeK3.jpg

看更多先前的討論...收起先前的討論...
圖啊~~~原諒我跳過了。
你是卡在產生csv檔案嗎?
照著寫就可以了
https://stackoverflow.com/questions/18757097/writing-data-into-csv-file-in-c-sharp
franx0722 iT邦新手 2 級 ‧ 2022-07-27 08:21:27 檢舉
斜向方向 @"d:\"
然後path中的斜線 應該"\\" 或 @"\"
然後你有一個 "-" 沒雙引號
Aqurius87 iT邦新手 5 級 ‧ 2022-07-27 10:50:36 檢舉
恩恩感謝 但不是沒有產出csv檔 是我想要在textbox輸入值後按確認 會產出 值+日期.csv檔 不太了解要怎樣做 感謝!
建議你把問題講清楚,是不會產生檔案?不會組合字串?不會取日期?
組字串跟取日期你的附圖中就有了
但圖中沒有產生檔案的程式碼,你卻又說 不是沒有產出 csv 檔,那就是你知道怎麼產生檔案,那問題到底是什麼?
franx0722 iT邦新手 2 級 ‧ 2022-07-27 12:08:50 檢舉
沒看到你用file
且string path那行很明顯光編譯就會錯 因為"\" 不會過 + - + 不是+ "-" +
Aqurius87 iT邦新手 5 級 ‧ 2022-07-27 13:27:23 檢舉
抱歉兩位大神 錯誤已更改 並附上最新的程式碼 小弟真的是新手真不好意思 可能描述上比較欠缺經驗
powerc iT邦新手 1 級 ‧ 2022-07-27 13:32:47 檢舉
早上看你的圖片中還有做取得text的內容阿 怎麼現在又沒了
Aqurius87 iT邦新手 5 級 ‧ 2022-07-27 13:42:29 檢舉
抱歉 我在整理一下在提問好了 感謝
Aqurius87 iT邦新手 5 級 ‧ 2022-07-27 14:00:53 檢舉
問題已補上 有不清楚的我可以在解釋...真的感謝
水無痕 iT邦新手 3 級 ‧ 2022-07-29 09:03:32 檢舉
"在form1 想請問要怎樣把form2之textbox值call過去"

我的解釋會是:

form2 宣告一個 Public EventHandler<string>
form1 加上上述的 Event 觸發的動作 (假設叫 Form2TextBoxTextChanges)

當 form2 滿足特定條件時,就觸發該 EventHandler ,然後就會觸發 form1 Form2TextBoxTextChanges !

在 Inoke EventHandler 給定變更後的值,就可以將值傳給 form1 了 !
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2022-07-27 09:41:32

參考官方範例

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;

public class Example
{
   public static void Main(string[] args)
   {
      Console.WriteLine($"Current directory is '{Environment.CurrentDirectory}'");
      Console.WriteLine("Setting current directory to 'C:\\'");

      Directory.SetCurrentDirectory(@"C:\");
      string path = Path.GetFullPath(@"D:\FY2018");
      Console.WriteLine($"'D:\\FY2018' resolves to {path}");
      path = Path.GetFullPath(@"D:FY2018");
      Console.WriteLine($"'D:FY2018' resolves to {path}");

      Console.WriteLine("Setting current directory to 'D:\\Docs'");
      Directory.SetCurrentDirectory(@"D:\Docs");

      path = Path.GetFullPath(@"D:\FY2018");
      Console.WriteLine($"'D:\\FY2018' resolves to {path}");
      path = Path.GetFullPath(@"D:FY2018");

      // This will be "D:\Docs\FY2018" as it happens to match the drive of the current directory
      Console.WriteLine($"'D:FY2018' resolves to {path}");

      Console.WriteLine("Setting current directory to 'C:\\'");
      Directory.SetCurrentDirectory(@"C:\");

      path = Path.GetFullPath(@"D:\FY2018");
      Console.WriteLine($"'D:\\FY2018' resolves to {path}");

      // This will be either "D:\FY2018" or "D:\FY2018\FY2018" in the subprocess. In the sub process,
      // the command prompt set the current directory before launch of our application, which
      // sets a hidden environment variable that is considered.
      path = Path.GetFullPath(@"D:FY2018");
      Console.WriteLine($"'D:FY2018' resolves to {path}");

      if (args.Length < 1)
      {
         Console.WriteLine(@"Launching again, after setting current directory to D:\FY2018");
         Uri currentExe = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase, UriKind.Absolute);
         string commandLine = $"/C cd D:\\FY2018 & \"{currentExe.LocalPath}\" stop";
         ProcessStartInfo psi = new ProcessStartInfo("cmd", commandLine); ;
         Process.Start(psi).WaitForExit();

         Console.WriteLine("Sub process returned:");
         path = Path.GetFullPath(@"D:\FY2018");
         Console.WriteLine($"'D:\\FY2018' resolves to {path}");
         path = Path.GetFullPath(@"D:FY2018");
         Console.WriteLine($"'D:FY2018' resolves to {path}");
      }
      Console.WriteLine("Press any key to continue... ");
      Console.ReadKey();
   }
}
// The example displays the following output:
//      Current directory is 'C:\Programs\file-paths'
//      Setting current directory to 'C:\'
//      'D:\FY2018' resolves to D:\FY2018
//      'D:FY2018' resolves to d:\FY2018
//      Setting current directory to 'D:\Docs'
//      'D:\FY2018' resolves to D:\FY2018
//      'D:FY2018' resolves to D:\Docs\FY2018
//      Setting current directory to 'C:\'
//      'D:\FY2018' resolves to D:\FY2018
//      'D:FY2018' resolves to d:\FY2018
//      Launching again, after setting current directory to D:\FY2018
//      Sub process returned:
//      'D:\FY2018' resolves to D:\FY2018
//      'D:FY2018' resolves to d:\FY2018
// The subprocess displays the following output:
//      Current directory is 'C:\'
//      Setting current directory to 'C:\'
//      'D:\FY2018' resolves to D:\FY2018
//      'D:FY2018' resolves to D:\FY2018\FY2018
//      Setting current directory to 'D:\Docs'
//      'D:\FY2018' resolves to D:\FY2018
//      'D:FY2018' resolves to D:\Docs\FY2018
//      Setting current directory to 'C:\'
//      'D:\FY2018' resolves to D:\FY2018
//      'D:FY2018' resolves to D:\FY2018\FY2018

有空請看完整官方文章

0
Juro十六
iT邦新手 3 級 ‧ 2022-07-27 14:40:18

有個挺有趣的寫法可以試試看
在Properties.Settings.Default裡面新增一個String
在呼叫另一個form的時候把csv的日期寫入到Properties.Settings.Default的string裡面
form2輸出的時候就是

//假設我設定string叫做_date
exportName = textbox1.text + Properties.Settings.Default._date
Aqurius87 iT邦新手 5 級 ‧ 2022-07-27 21:41:40 檢舉

抱歉我不太懂 Properties.Settings.Default這個是在哪

對你的專案按下右鍵>屬性(接著會跳出一個視窗)>左邊的菜單選擇設定(會出現可以設定4個值分別為名稱.類型.範圍.值)
名稱那邊就是我說的變數命名,他其實可以就算是一種全域變數,類型依你需求選擇string,範圍預設使用者就可以,值可以留空

Xuannnn

如果不需要每次啟動時都讓他記住最後一次設定的值的話可以直接用運算式表達
如果要讓它儲存成為下次啟動時一樣的值的話記得要使用到

Properties.Settings.Default.Save();

他才會儲存

我要發表回答

立即登入回答