我的工作簿有五个带有文本框的工作表。 Sheet1,Sheet2,......和Sheet5。
下面的代码在每张纸上。
# models.py
from django.db import models
class Contact(models.Model):
name = models.CharField(max_length = 100, verbose_name = "Nombre")
email = models.CharField(max_length = 100, verbose_name = "Email")
issue = models.CharField(max_length = 200, verbose_name = "Asunto")
text = models.TextField(verbose_name = "Mensaje")
# forms.py
from django import forms
class ContactForm(forms.Form):
name = forms.CharField(max_length = 100, label = "Nombre")
email = forms.EmailField(label = "Correo electrónico")
issue = forms.CharField(max_length = 200, label = "Asunto")
text = forms.CharField(label = "Mensaje")
# views.py
from django.views.generic import TemplateView
from contact.forms import ContactForm
from django.shortcuts import render
class Contact(TemplateView):
template_name = 'contact/contact.html'
def get(self, request):
form = ContactForm
return render(request, self.template_name, {'form': form})
def post(self, request):
form = ContactForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['email']
issue = form.cleaned_data['issue']
text = form.cleaned_data['text']
form = ContactForm()
args = {
'form': form,
'name': name,
'email': email,
'issue': issue,
'text': text,
}
return render(request, self.template_name, args)
<!-- And this is the form -->
<div class="page-section contact-page">
<div class="contact-warp">
<div class="row">
<div class="col-xl-6 p-0">
<div class="contact-text">
<span>¡Hola!</span>
<h2>Contáctame</h2>
<form class="contact-form" method="post">
{% csrf_token %}
{{ form.as_p }}
<button class="site-btn">Enviar mensaje</button>
</form>
</div>
</div>
</div>
</div>
</div>
我想同步所有工作表上的Private Sub TextBox1_Change()
If Len(TextBox1.Value) = 0 Then
ActiveSheet.AutoFilterMode = False
Else
If ActiveSheet.AutoFilterMode = True Then
ActiveSheet.AutoFilterMode = False
End If
ActiveSheet.Range("A2:C" & Rows.Count).AutoFilter field:=1, Criteria1:="*" & TextBox1.Value & "*"
End If
End Sub
。
例如,如果我在Sheet1的TEXTBOXES
中键入文本,则将在所有其他表的TEXTBOX1
中输入相同的文本。
我还想知道如何一次清除每张纸上的TEXTBOX1
。
答案 0 :(得分:0)
您可以在您的项目中添加模块,然后在其中添加此Sub(如果需要,可以调整图纸和文本框的名称):
Sub SetText(txt As String)
Worksheets("Sheet1").TextBox1.Text = txt
Worksheets("Sheet2").TextBox1.Text = txt
Worksheets("Sheet3").TextBox1.Text = txt
Worksheets("Sheet4").TextBox1.Text = txt
Worksheets("Sheet5").TextBox1.Text = txt
End Sub
然后,在每个TextBox的Change事件中添加此
Private Sub TextBox1_Change()
SetText Me.TextBox1.Text
End Sub
要清除所有文本,您可以设置.TextBox1.Text = ""
答案 1 :(得分:0)
1)将以下代码放入您项目的新模块中
Public dontDoThat As Boolean ' a public variable, visible throughout all your project you'll use to give way to synchronizing activity
Option Explicit
Sub Synchronize(txt As String, shtName As String)
dontDoThat = True ' set your public variable to True and prevent subsequent TextBox1_Change() events to run it again
Dim sht As Variant
For Each sht In Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5")
If sht <> shtName Then Worksheets(sht).TextBox1.Text = txt
Next
dontDoThat = False ' set your public variable to False and allow subsequent TextBox1_Change() events to run it
End Sub
2)如下更改您所有工作表中的TextBox1_Change()
事件
Private Sub TextBox1_Change()
If Not dontDoThat Then Synchronize Me.TextBox1.Text, Me.Name 'call Synchronize() only if your public variable allows it to
...
(rest of your previous code follows)
...
End Sub
要清除所有文本框,只需清除其中一个