質問編集履歴

1

コード追加

2019/08/21 05:21

投稿

bonjin1234
bonjin1234

スコア5

test CHANGED
File without changes
test CHANGED
@@ -3,3 +3,87 @@
3
3
 
4
4
 
5
5
  C#ド素人です。タイトルの通りなのですが、例えばForm1であらかじめtextBox1に入力してある文字列中に、textBox2に入力した文字列がいくつあるかカウントしたものをForm2に表示するにはどのように組めばいいでしょうか。ご教授よろしくお願いします。
6
+
7
+
8
+
9
+ ```C#
10
+
11
+ using System;
12
+
13
+ using System.Collections.Generic;
14
+
15
+ using System.ComponentModel;
16
+
17
+ using System.Data;
18
+
19
+ using System.Drawing;
20
+
21
+ using System.Linq;
22
+
23
+ using System.Text;
24
+
25
+ using System.Windows.Forms;
26
+
27
+
28
+
29
+ namespace kadai10
30
+
31
+ {
32
+
33
+ public partial class Form1 : Form
34
+
35
+ {
36
+
37
+ public string sendText;
38
+
39
+ private void button1_Click(object sender, EventArgs e)
40
+
41
+ {
42
+
43
+ textBox1.Text = textBox1.Text.Replace(textBox2.Text, textBox3.Text);
44
+
45
+
46
+
47
+ //別ウィンドウを表示
48
+
49
+ Form2 formSub = new Form2();
50
+
51
+ formSub.Show();
52
+
53
+
54
+
55
+ }
56
+
57
+
58
+
59
+ private static int CountChar(string sendText, string c)
60
+
61
+ {
62
+
63
+ return sendText.Length - sendText.Replace(c.ToString(), "").Length;
64
+
65
+ }
66
+
67
+
68
+
69
+ static void notMain()
70
+
71
+ {
72
+
73
+ string tx1 = new textBox1.Text();
74
+
75
+ string tx2 = new textBox2.Text();
76
+
77
+ string sendText = CountChar(tx1, tx2)+"個の文字を置き換えました";
78
+
79
+ }
80
+
81
+ }
82
+
83
+ }
84
+
85
+ ```
86
+
87
+
88
+
89
+ 今このような感じです。実をいうとさらにtextBox3にtextBox2の文字列をどのように置き換えるのか入力し、textBox1の文字列を置き換える作業もあるのですが、こちらは自力でどうにかなっていると思います。 上のコードではtextBox1とtextBox2が'フィールド'なのに'型'のように使用されているとエラーが出ており、自分なりに解決策を調べてみましたがうまく見つかりませんでした。コード組むうえで根本的なことが間違っているかもしれません。よろしくお願いします。