I have a requirement where I need to do Find and Replace in datagridview for multiple pages - it's working fine. The problem is, after I find a specific word and replace it and again if I click on Find and Replace button, the earlier find and replace values are gone.
How to work on it?
The code is provided below:
public string toFind = "";
public string toReplace = "";
private void btnFindandReplace_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.cmbColumnCombo.DataSource = cmbList;
f.ShowDialog();
toFind = f.txtfind.Text;
toReplace = f.txtreplace.Text;
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
if (dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().ToLower().Contains(f.txtfind.Text.ToLower()))
{
if (!string.IsNullOrEmpty(f.txtfind.Text))
{
dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value =
dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().Replace(f.txtfind.Text, f.txtreplace.Text);
}
}
}
}
Try to initialize your toFind and toReplace variables within the btnFindandReplace_Click method.
Declare your Form object outside of the button click event handler. This will make your form global and operate on the same object.
Related
My instructions are: "Create a form that will display a running total of numbers a user enters." - to do this I've created a form with two text boxes (one for the number of values in the array and the other for the values in the array), a button to display it, and a label for it all to be displayed it. The issue is, is that my values aren't showing up - at all. My code is as below:
(** NOTE: I'm attempting to get the array to display in my label. txtInput is the inputted values and txtArrayValues is the number of elements.)
namespace Running_Total
{
public partial class frmEnter : Form
{
public frmEnter()
{
InitializeComponent();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
int intNumber = Convert.ToInt32(txtArrayValues.Text);
string[] strArray;
strArray = new string[intNumber];
int i;
string j = "";
for (i = 0; i < intNumber; i++)
{
j = Convert.ToString(txtInput.Text);
strArray[i] += j;
}
lblDisplay.Text = strArray + " ";
}
}
}
Before, when I'd put lblDisplay.Text += j + " ";, it showed up in the label, but didn't pay any attention to the amount of elements the code was supposed to have. (Edit: this no longer works in my code.) (As is indicated in the title, I'm working with C# through Microsoft Visual Studio.)
It strongly depends on the fashion how the user inputs the numbers.
1) If he fills the textbox once with numbers and then presses the button to display them in the other box, it would suffice to use a string array catch the input and add it to the textbox or label that displays it. If he deletes the numbers in the input box and types new ones you could just repeat this step
namespace Running_Total
{
public partial class frmEnter : Form
{
// declare your Array here
string [] array = new string[1000];
int count = 0;
public frmEnter()
{
InitializeComponent();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
// save input
array[count] = inputTextBox.Text;
count++;
// display whole input
string output = "";
for(int i = 0;i < count; i++)
{
output += array[i];
}
// write it the texbox
outputTextBox.Text = output;
}
}
Does that answer your question or do you have another input pattern in mind?
Looking at your code, I realized that you want to display same number enteted in txtInput text repeatedly up to as many times as a number entered in a txtArrayValues.Text. So for example txtArrayValues. Text = "5" and txtInput.Text="2", your code will yield result "2,2,2,2,2". If that is what you want then the following code will achieve that.
using System.Linq;
namespace Running_Total
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
int len, num;
if (int.TryParse(txtArrayValues.Text, out len) &&
int.TryParse(txtInput.Text, out num))
{
lblDisplay.Text = string.Join(",", new string[len].Select(x => txtInput.Text));
}
}
}
}
Good Morning;
Actually have 2 questions. My first is what is this called? A Program? A Module?
WhatIsThisCalled()
{
//workToBeDone
}
I'm trying to create dynamic checkbox(s) from each entry in a text file. I'm trying to reuse the code so I have tried to create the module in a logic file. I feel like I've done this correctly, but I can't test it. I can not figure out how to reference
this.Controls.Add(chk[I]);
to the winForm I want to call it on. The error I get is about it being illegal in a static method. I'm only trying to clear the error (last one) so I can see if it will actually put the checkboxes onto the correct winForm Permissions.cs. Here is my Logic.cs module.
public static void getPermText()
{
Stream fileStream = File.Open(dataFolder + PermFile, FileMode.Open);
StreamReader reader = new StreamReader(fileStream);
string line = null;
do
{
line = reader.ReadLine();
if (line == null)
{
break;
}
string[] parts = line.Split('\n');
try
{
int userCount;
userCount = parts.Length;
CheckBox[] chk = new CheckBox[userCount];
int height = 1;
int padding = 10;
for (int i = 0; i <= userCount; i++)
{
chk[i] = new CheckBox();
chk[i].Name = parts.ToString();
chk[i].Text = parts.ToString();
chk[i].TabIndex = i;
chk[i].AutoCheck = true;
chk[i].Bounds = new Rectangle(15, 30 + padding + height, 150, 22);
this.Controls.Add(chk[i]);
height += 22;
}
}
catch
{
}
} while (true);
}
There is one global int userCount = 0; above that module. On Permissions.cs I have this code (with no errors).
public Permissions()
{
InitializeComponent();
}
private void Permissions_Load(object sender, EventArgs e)
{
WidgetLogic.getPermText();
}
Can anyone please direct me as to how, or if it is possible, to replace Permissions with this in a dynamic format?? I think??
Thank you very much in advance for all that look or help. I really appreciate it. Have a Great Day!! :)
I tired very hard to understand what you wanted to say. I think you just want to reference the form where the CheckBoxes should create.
So you should better pass the reference the of the form on which you want to create the controls:
public static void getPermText(System.Windows.Forms.Form targetForm)
{
//code
targetForm.Controls.Add(chk[i]); //changed "this" to "targetForm"
To call the method:
WidgetLogic.getPermText(this); //here "this" refers to the current form
Now where ever you will call this method it will create the controls on your form (the one you're passing as the parameter).
Notify me if I got your question wrong.
I'm trying to implement a custom text-editor in C# using the ScintillaNET component. I've got most of it right until now, but stuck at one point. I want to give the user the ability to block comment/uncomment the selected text. I tried a lot, but cannot find any examples online. The only thing I seem to get from the control's Selection object are the Start and End positions, but that isn't much help
private void commentBlockToolStripMenuItem_Click(object sender, EventArgs e)
{
if (txtSQL.Selection.Text.Length > 0)
{
String start = txtSQL.Selection.Start.ToString();
String end = txtSQL.Selection.End.ToString();
MessageBox.Show(start + "::" + end);
}
}
Were any of you able to successfully implement this using the ScintillaNET control?
EDIT:
After some improvization, I'm able to do it somehow, but after block is commented, last line moves out of selection!
private void commentBlockToolStripMenuItem_Click(object sender, EventArgs e)
{
if (txtSQL.Selection.Text.Length > 0)
{
Range range = txtSQL.Selection.Range;
int f = range.StartingLine.Number;
int t = range.EndingLine.Number;
int endpos = txtSQL.Selection.End;
for (int i = f; i <= t; i++)
{
//txtSQL.GoTo.Line(i);
string tstr = txtSQL.Lines[i].Text.Replace(Environment.NewLine, "");
txtSQL.Lines[i].Text = "--" + tstr;
}
}
}
After a bit of experimentation, I found a way to accomplish this. Though I doubt if it is the most elegant of solutions!
private void commentBlockToolStripMenuItem_Click(object sender, EventArgs e)
{
if (txtSQL.Selection.Text.Length > 0)
{
Range range = txtSQL.Selection.Range;
int f = range.StartingLine.Number;
int t = range.EndingLine.Number;
for (int i = f; i <= t; i++)
{
txtSQL.InsertText(txtSQL.Lines[i].StartPosition,"--");
}
txtSQL.Selection.Start = txtSQL.Lines[f].StartPosition;
txtSQL.Selection.End = txtSQL.Lines[t].EndPosition;
}
}
Actually I found a very simple solution to this. To block comment do
scintilla1.Lexing.LineComment();
And to block uncomment do
scintilla1.Lexing.LineUncomment();
I have managed to replace all the strings in the datagridview at run-time.Now i want to replace the string one by one on the click of the button.This is the code for the replacement of all the strings on a single button click.
private void button9_Click_1(object sender, EventArgs e)
{
var original = ((DataTable)dataGridView1.DataSource);
var clone = original.Clone();
var ordinal = original.Columns["Stringtext"].Ordinal;
var tra = original.Columns[6].Ordinal;
var che = original.Columns[10].Ordinal;
for (int i = 0; i < original.Rows.Count; i++)
{
var values = original.Rows[i].ItemArray;
if (Convert.ToBoolean(values[tra].ToString()) && Convert.ToBoolean(values[che].ToString()))
{
values[ordinal] = ((values[ordinal].ToString()).ToLower())
.Replace(textBox6.Text.ToLower(), textBox7.Text);
clone.Rows.Add(values);
}
else
{
values[ordinal] = values[ordinal];
clone.Rows.Add(values);
}
}
dataGridView1.DataSource = clone;
string filterBy;
filterBy = "Stringtext Like '%" + textBox7.Text + "%'";
((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = filterBy;
}
I want to replace a single string in a row on the click of a button then on the next button click the next string in the row is replaced.etc.any ideas?
the easy way to do what you want is to keep a counter on how many times did the user pressed the button. that way you can tell this is the 1st, 2end or 3rd time the user clicked the button and by so replace the needed cell
private int counter = 0;
private void button1_Click(object sender, EventArgs e)
{
// change the cell = counter
counter++;
}
This may seem like a dumb question. I have a textbox that can be used to add items to a checkedlistbox at runtime on a windows form. I'm using c#. It works perfectly fine at runtime. The item gets added and stuff, when the form is open. But, when I close and open the form again, I don't see the added item in the checkedlistbox list. Note, I don't use a datasource and don't want to. I wouldn't want to hardcode anything and would prefer to use a textbox input on the form as a variable to feed into the collections list. I couldn't figure out a way to expand my checkedlistbox options. Any assistance would be appreciated.
How are you opening the form? Is it something like:
FormName form = new FormName();
form.Show()
The only reason I can think that's happening is that you're instantiating a new form instance every time you show it, instead of reusing the same form.
Have your Form take a ref List<string> values as parameter. Then make this as BindingSource for the CheckedListBox.
Here is the code:
class MyForm : Form {
List<string> values;
BindingSource source;
public MyForm()
{
InitializeComponent();
}
public MyForm(ref List<string> values):this()
{
if (values == null)
values = new List<string>();
this.values = values;
checkedListBox1.DisplayMember = "Value";
checkedListBox1.ValueMember = "Value";
source = new BindingSource(this.values, null);
checkedListBox1.DataSource = source;
}
private void AddItemButton_Click(object sender, EventArgs e)
{
this.source.Add(textBox1.Text);
textBox1.Text = string.Empty;
}
}
private void frmMain_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Properties.Settings.Default.CheckedItems))
{
string[] checkedIndicies = Properties.Settings.Default.CheckedItems.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for (int i1 = 0; i1 < checkedIndicies.Length; i1++)
{
int idx;
if ((int.TryParse(checkedIndicies[i1], out idx)) && (checkedListBox1.Items.Count >= (idx+1)))
{
checkedListBox1.SetItemChecked(idx, true);
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
textBox1.MaxLength = 15;
// Change all text entered to be lowercase.
textBox1.CharacterCasing = CharacterCasing.Lower;
if (checkedListBox1.Items.Contains(textBox1.Text) == false)
{
checkedListBox1.Items.Add(textBox1.Text, CheckState.Checked);
textBox1.Text = "";
MessageBox.Show("Added! Click Move to see List Box");
}
else
{
MessageBox.Show("Already There!");
textBox1.Text = "";
}
}
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
string idx = string.Empty;
for (int i1 = 0; i1 < checkedListBox1.CheckedIndices.Count; i1++)
idx += (string.IsNullOrEmpty(idx) ? string.Empty : ",") + Convert.ToString(checkedListBox1.CheckedIndices[i1]);
Properties.Settings.Default.CheckedItems = idx;
Properties.Settings.Default.Save();
}