我有一个名为class 1
的班级,其功能如下所示,form1
类似照片form1 design
我想知道如何在文本框中放置值并将这些值发送到此类中的此函数,以提供我想要在form1
public class class1
{
public double func(string A, double b, double c, double d, double e)
{
if (A == "c")
{
res= b-c/d-e;
}
else if (A == "p")
{
res = b+c/d+e;
}
return res;
}
}
答案 0 :(得分:1)
首先,您需要为按钮创建一个事件。如果您使用的是WinForms,只需双击按钮,系统就会自动为您创建一个新的Click事件。
将以下代码粘贴到内部并根据您的变量进行更改。
string a = TextBox_A.Text;
int b = Convert.ToDouble(TextBox_B.Text);
int c = Convert.ToDouble(TextBox_C.Text);
int d = Convert.ToDouble(TextBox_D.Text);
int e = Convert.ToDouble(TextBox_E.Text);
YourTextBox.Text = func(a,b,c,d,e).ToString();
最终它应该是这样的:
private void ButtonSubmit_Click(object sender, Eventargs e)
{
string a = TextBox_A.Text;
int b = Convert.ToDouble(TextBox_B.Text);
int c = Convert.ToDouble(TextBox_C.Text);
int d = Convert.ToDouble(TextBox_D.Text);
int e = Convert.ToDouble(TextBox_E.Text);
YourTextBox.Text = func(a,b,c,d,e).ToString();
}
答案 1 :(得分:0)
在结果按钮的onClick eventHandler中添加 onClick 事件方法:
private void SubmitButton_Click(object sender, EventArgs e){
if (A== "c")
res= b-c/d-e;
else if (A== "p")
res= b+c/d+e;
textBox_result.Text = res;
)
textBox_result 是您要在其中显示结果的文本框的名称。
答案 2 :(得分:0)
为什么要访问其他类的文本值?通常,您不应该从外部类访问文本框,因为您还看到文本框是受保护的字段,因此无法从外部类访问。我建议你从Form1类中这样做(在SubmitButton上创建一个click事件):
Sub AlphaTest()
Dim MyPath As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
Dim SaveDriveDir As String
Dim FName As Variant
Dim FirstCell As String
Dim sName As String
' Set application properties.
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
SaveDriveDir = CurDir
' Change this to the path\folder location of the files.
ChDirNet "Z:\"
FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", _
MultiSelect:=True)
If IsArray(FName) Then
' Add a new workbook with one sheet.
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum = 1
' Loop through all files in the myFiles array.
For FNum = LBound(FName) To UBound(FName)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(FName(FNum), ReadOnly:=True)
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
'If ActiveWorkbook.Worksheets.Name Like "*Debtors*" Then
' sName = ActiveWorkbook.Worksheets.Name
'Else
' sName = "0"
'End If
With mybook.Worksheets("Alpha")
FirstCell = "A6"
Set sourceRange = .Range(FirstCell & ":" & RDB_Last(3, .Cells))
' Test if the row of the last cell is equal to or greater than the row of the first cell.
If RDB_Last(1, .Cells) < .Range(FirstCell).Row Then
Set sourceRange = Nothing
End If
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
' If the source range uses all columns then
' skip this file.
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0
If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.Rows.Count
If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "There are not enough rows in the target worksheet."
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else
' Copy the file name in column A.
With sourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = FName(FNum)
End With
' Set the destination range.
Set destrange = BaseWks.Range("C" & rnum)
' Copy the values from the source range
' to the destination range.
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value
rnum = rnum + SourceRcount + 1
End If
End If
mybook.Close savechanges:=False
End If
Next FNum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
' Restore the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
ChDirNet SaveDriveDir
End Sub
如果你真的想要改变另一个类的值,你应该向Form1添加一个公共函数来获取和设置textBox的值。你可以做的只是打电话给你的班级,所以你从Form1开始设置你的TextBoxes,从Class开始计算你的结果。
在form1中:
private void SubmitButton_Click(object sender, EventArgs e)
{
double res = 0;
string a = TextBoxA.Text;
double b = GetTextBoxValueAsDouble(TextBoxB.Text);
double c = GetTextBoxValueAsDouble(TextBoxC.Text);
double d = GetTextBoxValueAsDouble(TextBoxD.Text);
double e = GetTextBoxValueAsDouble(TextBoxE.Text);
// Use .equals to compare strings!
if (a.Equals("c"))
{
// Are you sure this shouldn't be (b-c)/(d-e)?
res= b-c/d-e;
}
else if (a.Equals("p"))
{
// Are you sure this shouldn't be (b+c)/(d+e)?
res= b+c/d+e;
}
TextboxResult.Text = res.ToString();
}
private double GetTextBoxValueAsDouble(string value)
{
// Ideally you should check whether the parsing has succeeded!
double.TryParse(value, out double doubleValue);
return doubleValue;
}
在第1课:
private void SubmitButton_Click(object sender, EventArgs e)
{
double res = 0;
string a = TextBoxA.Text;
double b = GetTextBoxValueAsDouble(TextBoxB.Text);
double c = GetTextBoxValueAsDouble(TextBoxC.Text);
double d = GetTextBoxValueAsDouble(TextBoxD.Text);
double e = GetTextBoxValueAsDouble(TextBoxE.Text);
double res = class1.Func(a, b, c, d, e);
TextboxResult.Text = res.ToString();
}